你不能只使用一个git命令,但是你可以用一个bash行自动化它。
为了用一行安全地更新所有分支,我是这样做的:
git fetch --all && for branch in $(git branch | sed '/*/{$q;h;d};$G' | tr -d '*') ; do git checkout $branch && git merge --ff-only || break ; done
如果它不能快进一个分支或遇到错误,它将停止并将您留在该分支中,以便您可以收回控制权并手动合并。
如果所有分支都可以快进,它将以您当前所在的分支结束,使您停留在更新之前的位置。
解释:
为了更好的可读性,可以将它分成几行:
git fetch --all && \
for branch in $(git branch | sed '/*/{$q;h;d};$G' | tr -d '*')
do git checkout $branch && \
git merge --ff-only || break
done
git fetch --all && ... => Fetches all refs from all remotes and continue with the next command if there has been no error.
git branch | sed '/*/{$q;h;d};$G' | tr -d '*' => From the output of git branch, sed take the line with a * and move it to the end (so that the current branch will be updated last). Then tr simply remove the *.
for branch in $(...) ; do git checkout $branch && git merge --ff-only || break ; done = > For each branch name obtained from the previous command, checkout this branch and try to merge with a fast-forward. If it fails, break is called and the command stops here.
当然,如果你想要的话,你可以用git rebase替换git merge——ff-only。
最后,你可以把它作为一个别名放在bashrc中:
alias git-pull-all='git fetch --all && for branch in $(git branch | sed '\''/*/{$q;h;d};$G'\'' | tr -d "*") ; do git checkout $branch && git merge --ff-only || break ; done'
或者如果你害怕混淆' and ',或者你只是喜欢在编辑器中保持语法的可读性,你可以将它声明为一个函数:
git-pull-all()
{
git fetch --all && for branch in $(git branch | sed '/*/{$q;h;d};$G' | tr -d '*') ; do git checkout $branch && git merge --ff-only || break ; done
}
奖金:
对于那些想要sed '/*/{$q;h;d};$G'部分的解释:
/*/ =>查找带*的行。
{$q =>如果它在最后一行,退出(我们不需要做任何事情,因为当前分支已经是列表中的最后一个)。
d} =>否则,将该行保存在保持缓冲区中,并将其删除在当前列表位置。
$G =>当它到达最后一行时,追加保持缓冲区的内容。