我通常至少有3个远程分支:master、staging和production。我有3个本地分支来跟踪这些远程分支。

更新我所有的本地分支是乏味的:

git fetch --all
git rebase origin/master
git checkout staging
git rebase origin/staging
git checkout production
git rebase origin/production

我很想做一个“git pull -all”,但我还没能让它工作。它似乎做了一个“fetch -all”,然后更新(快进或合并)当前工作的分支,但不包括其他本地分支。

我仍然需要手动切换到每个本地分支并进行更新。


当前回答

我也遇到过同样的问题……

想知道自己,我做了一个小的别名函数在我的.bashrc文件:

gitPullAll() {
    for branch in `git branch | sed -E 's/^\*/ /' | awk '{print $1}'`; do
        git checkout $branch
        git pull -p
        printf "\n"
    done
    echo "Done"
}

为我工作过

其他回答

如果你在Windows上,你可以使用PyGitUp,它是Python的一个克隆版本。您可以使用pip和pip install -user git-up安装它,或者通过Scoop使用Scoop install git-up安装它

[

自动化并不难:

#!/bin/sh
# Usage: fetchall.sh branch ...

set -x
git fetch --all
for branch in "$@"; do
    git checkout "$branch"      || exit 1
    git rebase "origin/$branch" || exit 1
done

不知道这是否可以,但如果我想快进多个分支,我通常会调用

git pull origin master staging production

如果我想推送多个分支,我会调用

git push origin master staging production

但只有当所有提到的分支都不需要任何形式的合并时,两者才有效。

来自@larsmans的脚本,有一点改进:

#!/bin/sh

set -x
CURRENT=`git rev-parse --abbrev-ref HEAD`
git fetch --all
for branch in "$@"; do
  if ["$branch" -ne "$CURRENT"]; then
    git checkout "$branch" || exit 1
    git rebase "origin/$branch" || exit 1
  fi
done
git checkout "$CURRENT" || exit 1
git rebase "origin/$CURRENT" || exit 1

这样,在它完成之后,工作副本就会从调用脚本之前的同一个分支签出。

git拉版:

#!/bin/sh

set -x
CURRENT=`git rev-parse --abbrev-ref HEAD`
git fetch --all
for branch in "$@"; do
  if ["$branch" -ne "$CURRENT"]; then
    git checkout "$branch" || exit 1
    git pull || exit 1
  fi
done
git checkout "$CURRENT" || exit 1
git pull || exit 1

您为pull描述的行为——都完全符合预期,尽管不一定有用。该选项被传递给git fetch,然后从所有远程获取所有引用,而不仅仅是需要的一个;然后,Pull合并(或者在您的情况下,是重新创建)适当的单个分支。

如果你想查看其他分支机构,你就必须查看它们。是的,合并(和重基)绝对需要一个工作树,所以不检查其他分支就不能完成它们。如果愿意,您可以将所描述的步骤打包到脚本/别名中,不过我建议使用&&来连接命令,这样即使其中一个命令失败,它也不会尝试继续执行。