繁体   English   中英

使用别名在一行中运行多个 git 命令

[英]Running multiple git commands in one line with aliases

我有 2 个 git 的不同“脚本”,我有 2 个别名,我想在一行中运行它们。

第一个别名,我们称之为printStatus ,如下所示:

pushd . && clear && cd /c/dev/erdm-desktop && find . -name .git -type d -execdir pwd \; -execdir git status \; -prune && popd

它遍历我所有的 git 目录并打印出它们的状态。 但除此之外,我希望它删除它找到的所有分支。 该命令是clean ,如下所示:

git fetch -p origin && git branch -r --merged origin/develop | grep -v develop | grep -v \"release/\" | grep \"origin/\" | cut -d \"/\" -f 2- | xargs -i git push origin :{}

这两个都很好,但我似乎找不到将它们结合起来的方法。 您可以从别名中调用别名吗? 有人可以告诉我如何执行这两个命令吗?

当您运行find... -exec <command>时, <command>不会使用与初始 shell 相同的 shell 执行,并且它可能不知道您的别名 - 您可能在.bashrc中定义了这些别名。

如果您有某种动机将这些命令真正保留为bash 别名,您可以指示-exec运行您使用 bash 键入的命令并加载您的.bashrc

# I may be missing details on how to escape correctly the elements :
find ... -exec bash -i -c clean \;

但更简单的方法是:

  • 在放置在PATH上的脚本中编写操作序列:
# say you have your one liner in a script named 'clean', in '$HOME/bin' :
$ cat ~/bin/clean
#!/bin/bash
git fetch -p origin && git branch -r --merged origin/develop | ...

# that script should be executable :
$ git chmod u+x ~/bin/clean
# place $HOME/bin in your path (you can set this in your .bashrc) :
$ PATH=$HOME/bin:$PATH

# 'clean' is now an executable which can be called by any other process :
$ zsh -c clean
$ find ... -exec clean

您可能希望将这些衬里变成 git 别名,这将产生类似的效果: git将始终加载您的根配置文件( ~/.gitconfig )当你运行它:

# 'clean' is already a git command, so we'll use another alias :
$ git config --global alias.clean-repo '! git fetch -p origin && git branch -r ...'

# with this alias on, you can invoke it using :
$ git clean-repo

# this will also work from within 'find ... -exec <command>' :
$ find ... -exec git clean-repo

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM