运行 多个 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