Git - 拉动时自动快进所有跟踪分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4577874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git - Automatically fast forward all tracking branches on pull
提问by Chetan
I've set up tracking branches with the --track
option, and when I do a git pull
on master
, it fetches all branches to origin/branchname
but doesn't merge with the local tracking branches. This is extra annoying, because if I later do a git push
on master
, it says that non-fast-forward updates were rejected on the tracking branches, since they weren't fast-forwarded on the initial git pull
.
我已经使用该--track
选项设置了跟踪分支,当我执行git pull
on 时master
,它会将所有分支提取到origin/branchname
但不与本地跟踪分支合并。这特别烦人,因为如果我稍后再执行git push
on master
,它会说在跟踪分支上拒绝了非快进更新,因为它们没有在初始git pull
.
My question is: How do I make it so that git pull
with fetch all branches and automatically fast-forward all the tracking branches?
我的问题是:如何做到这一点,以便git pull
获取所有分支并自动快进所有跟踪分支?
Note: git pull
used to fast-forward all my tracking branches with my GitHub repos, but now that I've set up my own repos using Gitolite, this problem is cropping up.
注意:git pull
曾经使用我的 GitHub 存储库快进我所有的跟踪分支,但现在我已经使用 Gitolite 设置了我自己的存储库,这个问题突然出现。
采纳答案by VonC
But wait:
可是等等:
- git won't merge (the second step of
git pull
after thefetch
part) files unless the branch is checked out first. See "Can “git pull --all
” update all my local branches?" git pull
onmaster
will merge files onmaster
, meaning the next push will be a fast-forward one. A non fast-forward can only occur if a push to the remotemaster
from another repohas been done prior to yourpush.
- 除非先检出分支,否则git 不会合并(部分
git pull
之后的第二步fetch
)文件。参见“可以“git pull --all
”更新我所有的本地分支吗?” git pull
onmaster
将合并文件 onmaster
,这意味着下一次推送将是快进的。只能出现非快进,如果推到远程master
从另一个回购之前已经做了你的推。
Note: I suppose you have tracked all your remote branches as in "Track all remote git branches as local branches."
注:我想你已经跟踪所有远程分支机构中的“跟踪所有远程的Git分支为当地的分支机构。”
Note: Git 2.0 (Q2 2014) will introduce with commit b814da8a config push.ff:
注意:Git 2.0(2014 年第二季度)将在提交 b814da8 中引入配置 push.ff:
pull.ff::
By default, Git does not create an extra merge commit when merging a commit that is a descendant of the current commit. Instead, the tip of the current branch is fast-forwarded.
- When set to
false
, this variable tells Git to create an extra merge commit in such a case (equivalent to giving the--no-ff
option from the command line).- When set to
only
, only such fast-forward merges are allowed (equivalent to giving the--ff-only
option from the command line).
默认情况下,Git 在合并作为当前提交的后代的提交时不会创建额外的合并提交。相反,当前分支的尖端是快进的。
- 当设置为 时
false
,这个变量告诉 Git 在这种情况下创建一个额外的合并提交(相当于--no-ff
从命令行提供选项)。- 当设置为 时
only
,只允许这种快进合并(相当于--ff-only
从命令行提供选项)。
回答by qwertzguy
Shell script that fast-forwards all branches that have their upstream branch set to the matching origin/ branch without doing any checkouts
Shell 脚本可将所有上游分支设置为匹配的原点/分支的分支快进,而无需进行任何检出
it doesn't change your current branch at any time, no need to deal with working copy changes and time lost checking out
it only does fast-forwards, branches that cannot be fast-forwarded will show an error message and will be skipped
它不会随时更改您当前的分支,无需处理工作副本更改和检查时间浪费
它只做快进,不能快进的分支会显示错误信息并被跳过
Make sure all your branches' upstream branches are set correctly by running git branch -vv
. Set the upstream branch with git branch -u origin/yourbanchname
通过运行确保所有分支的上游分支都设置正确git branch -vv
。设置上游分支git branch -u origin/yourbanchname
Copy-paste into a file and chmod 755:
复制粘贴到文件和 chmod 755 中:
#!/bin/sh
curbranch=$(git rev-parse --abbrev-ref HEAD)
for branch in $(git for-each-ref refs/heads --format="%(refname:short)"); do
upbranch=$(git config --get branch.$branch.merge | sed 's:refs/heads/::');
if [ "$branch" = "$upbranch" ]; then
if [ "$branch" = "$curbranch" ]; then
echo Fast forwarding current branch $curbranch
git merge --ff-only origin/$upbranch
else
echo Fast forwarding $branch with origin/$upbranch
git fetch . origin/$upbranch:$branch
fi
fi
done;
回答by J?rn Hees
If you really want to fast forward all local branches that are tracking remote branches you might want to consider adding this as an alias to your ~/.gitconfig
:
如果您真的想快进所有跟踪远程分支的本地分支,您可能需要考虑将其添加为您的别名~/.gitconfig
:
[alias]
pull-all = !"for b in $(git for-each-ref refs/heads --format='%(refname)') ; do git checkout ${b#refs/heads/} ; git pull --ff-only ; done"
You can then run git pull-all
, it will iterate through your local branches and run a git pull --ff-only
on each.
然后您可以运行git pull-all
,它将遍历您的本地分支并git pull --ff-only
在每个分支上运行。
回答by krlmlr
The following one-liner fast-forwards all branches that have an upstream branch if possible, and prints an error otherwise:
如果可能,以下单行快进所有具有上游分支的分支,否则打印错误:
git branch \
--format "%(if)%(upstream:short)%(then)git push . %(upstream:short):%(refname:short)%(end)" |
sh
How does it work?
它是如何工作的?
It uses a custom format with the git branch
command. For each branch that has an upstream branch, it prints a line with the following pattern:
它在git branch
命令中使用自定义格式。对于具有上游分支的每个分支,它会打印一行具有以下模式的行:
git push . <remote-ref>:<branch>
This can be piped directly into sh
(assuming that the branch names are well-formed). Omit the | sh
to see what it's doing.
这可以直接输入sh
(假设分支名称格式正确)。省略| sh
看看它在做什么。
Caveat
警告
The currently checked-out branch will not be updated with a message like
当前签出的分支不会更新为类似的消息
! [remote rejected] origin/master -> master (branch is currently checked out)
For this, you can resort to regular git pull --ff-only
.
为此,您可以求助于常规git pull --ff-only
.
Alias
别名
Add the following to your .gitconfig
so that git fft
performs this command:
将以下内容添加到您的.gitconfig
以便git fft
执行此命令:
[alias]
fft = !sh -c 'git branch --format \"%(if)%(upstream:short)%(then)git push . %(upstream:short):%(refname:short)%(end)\" | sh' -
The alias is a shorthand to "fast-forward tracking (branches)".
别名是“快进跟踪(分支)”的简写。
回答by changyuheng
I just wrote a small tool to do so. https://github.com/changyuheng/git-fast-forward-all
我只是写了一个小工具来做到这一点。https://github.com/changyuheng/git-fast-forward-all
Advantages of this tool:
该工具的优点:
- Supports multiple remotes in one repository. (
hub sync
doesn't support multiple remotes at the moment.) - Supports having different names on the local branch and the corresponding remote tracking branche.
- Much faster than other scripts that fetches remote for every single branch.
- No error-prone regex parsing/editing.
- 在一个存储库中支持多个遥控器。(
hub sync
目前不支持多个遥控器。) - 支持在本地分支和对应的远程跟踪分支上使用不同的名称。
- 比为每个分支获取远程的其他脚本快得多。
- 没有容易出错的正则表达式解析/编辑。