如何快进分支机构?


249

在分支上开发了很长一段时间后,我切换到了master。日志显示:

您的分支落后“起源/母版” 167次提交,并且可以快速转发。

我试过了:

git checkout HEAD

没有作用。这是因为我已检出master上的中间提交。

如何使主人保持头脑清醒?


6
git checkout HEAD从不做任何事情。HEAD已经意味着已签出的提交。
EmreTapcı18年

Answers:


244

正在做:

git checkout master
git pull origin

将获取并合并origin/master分支(您可能会说git pull默认为origin)。


52
我认为罗伯的答案更好。我通常会遇到这种情况,即我刚刚完成拉动,然后切换到另一个分支,该分支需要快速转发。如果我不得不再做一次(无操作)拉动并等待它完成,这对我来说很烦人。无论如何,执行仅本地操作会更快。
Schwartz男爵

353

尝试git merge origin/master。如果您想确保它只做一个快进,可以说git merge --ff-only origin/master


4
当您的遥控器有一些认证环可以跳过时,这很好用。当我进入一个分支时,我必须进行身份验证。然后,当我切换到另一个分支(即,选择我的更改)时,我更喜欢使用此merge命令,因此不必重新进行身份验证。
RustyTheBoyRobot 2013年

30
--ff-only非常有用。
路加福音

4
如果我不知道origin/master是必需的一部分,或者如果它合理的默认值,但我发现它有用,使快进的别名,所以我想肯定使上游分支是用来代替硬编码它到origin/masterff = merge --ff-only @{u}@{u}上游) 。
Thor84no 2015年

2
如果离线,那比建议的答案好得多
Jacek Pietal '16

1
您能解释一下为什么简单的拉动不会做同样的事情吗?此外,如果这样做,是否仍需要拉力?
Zuzu Corneliu

40

根据您的情况,git rebase也可以解决问题。由于您没有master所没有的任何更改,因此git会快速前进。如果您使用的是变基工作流程,则可能更可取,因为如果搞砸了,最终不会得到合并提交。

username@workstation:~/work$ git status
# On branch master
# Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
#   (use "git pull" to update your local branch)
#
nothing to commit, working directory clean
username@workstation:~/work$ git rebase
First, rewinding head to replay your work on top of it...
Fast-forwarded master to refs/remotes/origin/master.
# On branch master
nothing to commit, working directory clean

1
对我来说非常有用,因为我们不应该使用git pull!
Stefan 2014年

即使您有一些待处理的更改,您也可以随时隐藏和调整基准,我不知道这是否是“正确”的方法,但是会产生奇迹。
fn。

28
git checkout master
git pull

应该做的工作。

每次在不同于master的分支上工作时,都会收到“您的分支在后面”的消息,有人更改了master并进行了git pull。

(branch) $ //hack hack hack, while someone push the changes to origin/master
(branch) $ git pull   

现在原点/主基准拉,但你的主人是未合并与它

(branch) $ git checkout master
(master) $ 

现在,母版在原点/母版后面,可以快速转发

this will pull and merge (so merge also newer commits to origin/master)
(master) $ git pull 

this will just merge what you have already pulled
(master) $ git merge origin/master

现在您的主人和原籍/主人已经同步


13

如果您站在其他分支上,并且想签出最新版本的master,也可以执行

git checkout -B master origin/master


9

对于想要快速转发的任何人,如果不签出该分支,它们就不会进入另一个远程分支(包括自身),您可以执行以下操作:

git fetch origin master:other

这基本上快进的指数otherorigin/master,如果你不在other分支。您可以通过这种方式快速转发多个分支。

如果您在另一个分支上工作了一段时间,并且想要将陈旧的分支从远程更新到各自的头部:

git fetch origin master:master other:other etc:etc

2

无需复杂性,只需站在您的分支机构并执行git pull 就可以了

或者, 仅当您不满意第一个命令时,才可以尝试第二次尝试git pull origin master


0

变基当前本地跟踪器分支移动最新的远程状态的顶部局部变化:

$ git fetch && git rebase

更一般而言,要快进并丢弃本地更改(硬重置)*:

$ git fetch && git checkout ${the_branch_name} && git reset --hard origin/${the_branch_name}

快进和保持局部变化(衍合):

$ git fetch && git checkout ${the_branch_name} && git rebase origin/${the_branch_name}

*-要撤消由无意的硬重置引起的更改,请先执行do git reflog,以相反的顺序显示HEAD的状态,在重置操作之前找到HEAD指向的哈希(通常很明显),然后将分支硬重置为该哈希。


0

就您而言,要快速前进,请运行:

$ git merge --ff-only origin/master

这使用的--ff-only选项git merge,因为问题专门要求“快进”。

以下摘录git-merge(1)显示了更多快速选择:

--ff, --no-ff, --ff-only
    Specifies how a merge is handled when the merged-in history is already a descendant of the current history.  --ff is the default unless merging an annotated
    (and possibly signed) tag that is not stored in its natural place in the refs/tags/ hierarchy, in which case --no-ff is assumed.

    With --ff, when possible resolve the merge as a fast-forward (only update the branch pointer to match the merged branch; do not create a merge commit). When
    not possible (when the merged-in history is not a descendant of the current history), create a merge commit.

    With --no-ff, create a merge commit in all cases, even when the merge could instead be resolved as a fast-forward.

    With --ff-only, resolve the merge as a fast-forward when possible. When not possible, refuse to merge and exit with a non-zero status.

我经常快进,以至于需要别名:

$ git config --global alias.ff 'merge --ff-only @{upstream}'

现在,我可以运行此命令以快速前进:

$ git ff

-2

将分支指针移到HEAD:

git branch -f master

您的分支master已经存在,因此git不允许您覆盖它,除非您使用... -f(此参数代表--force

或者您可以使用rebase:

git rebase HEAD master

自行承担风险;)


1
不要尝试这样做。如果您有以下情况,将会发生不好的事情:C0 --- C1 --- C2 --- C3 --- C4(master)。C0 --- C1 --- C2 --- B1 --- B2 --- B3(dev)如果您的头位于B3(dev)并且执行git branch -f master,您将得到C0- -C1 --- C2 --- B1 --- B2 --- B3(dev)(master)。C3 --- C4无法从任何分支访问,最终将被垃圾回收。如果发现自己处于这种情况,请查看reflog并使用-b <branch>选项签出C4 commit以创建新分支。
A_P
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.