【发布时间】:2016-04-29 03:18:06
【问题描述】:
假设我有一个名为 alice 的 git 遥控器。这个遥控器有数百个分支,但我只从中提取了几个分支,使用git fetch alice some-branch、git fetch alice another-branch 等。
现在,我只想从 alice 同步我已经拥有的分支 - 我不想获取所有分支。我该怎么做?
【问题讨论】:
假设我有一个名为 alice 的 git 遥控器。这个遥控器有数百个分支,但我只从中提取了几个分支,使用git fetch alice some-branch、git fetch alice another-branch 等。
现在,我只想从 alice 同步我已经拥有的分支 - 我不想获取所有分支。我该怎么做?
【问题讨论】:
我宁愿在我的 repo 的本地配置中设置 specific fetch refspecs,而不是依赖 bash 魔术解析。
git config remote.alice.fetch 'refs/heads/branch1/*:refs/remotes/origin/branch1/*'
git config --add remote.alice.fetch 'refs/heads/branch2/*:refs/remotes/origin/branch2/*'
git config --add remote.alice.fetch 'refs/heads/branch3/*:refs/remotes/origin/branch3/*'
这样,git fetch alice 只会获取指定的分支。
【讨论】:
我没有找到简单的解决方案,但这个对我有用:
git branch -ar | sed 's|^[ \t]*||' | grep -e '^alice/' | sed 's|alice/||' | xargs git fetch alice
解释:
git fetch
或者,作为一个通用的 bash 函数,可以添加到您的.bashrc:
# Usage:
# git-sync-remote <remote-name>
# Example:
# git-sync-remote alice
git-sync-remote(){
REMOTE_NAME="$1"
if [ -z "${REMOTE_NAME}" ] ; then
echo 'You need to provide remote name'
return
fi
git branch -ar | sed 's|^[ \t]*||' | grep -e "^${REMOTE_NAME}/" | sed "s|${REMOTE_NAME}/||" | xargs git fetch "${REMOTE_NAME}"
}
【讨论】: