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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:53:08  来源:igfitidea点击:

Git - Automatically fast forward all tracking branches on pull

gitbranchpull

提问by Chetan

I've set up tracking branches with the --trackoption, and when I do a git pullon master, it fetches all branches to origin/branchnamebut doesn't merge with the local tracking branches. This is extra annoying, because if I later do a git pushon 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 pullon 时master,它会将所有分支提取到origin/branchname但不与本地跟踪分支合并。这特别烦人,因为如果我稍后再执行git pushon master,它会说在跟踪分支上拒绝了非快进更新,因为它们没有在初始git pull.

My question is: How do I make it so that git pullwith fetch all branches and automatically fast-forward all the tracking branches?

我的问题是:如何做到这一点,以便git pull获取所有分支并自动快进所有跟踪分支?

Note: git pullused 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:

可是等等:

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-ffoption from the command line).
  • When set to only, only such fast-forward merges are allowed (equivalent to giving the --ff-onlyoption 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-onlyon 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 branchcommand. 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 | shto 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 .gitconfigso that git fftperforms 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:

该工具的优点:

  1. Supports multiple remotes in one repository. (hub syncdoesn't support multiple remotes at the moment.)
  2. Supports having different names on the local branch and the corresponding remote tracking branche.
  3. Much faster than other scripts that fetches remote for every single branch.
  4. No error-prone regex parsing/editing.
  1. 在一个存储库中支持多个遥控器。(hub sync目前不支持多个遥控器。)
  2. 支持在本地分支和对应的远程跟踪分支上使用不同的名称。
  3. 比为每个分支获取远程的其他脚本快得多。
  4. 没有容易出错的正则表达式解析/编辑。

回答by Mot

SmartGit, for example, has an option to automatically merge changes from the tracked branch if you switch to a branch. This should do what you want to achieve.

例如,SmartGit可以选择在您切换到分支时自动合并来自跟踪分支的更改。这应该做你想要实现的目标。