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

你的分支比“origin/master”慢了167次提交,并且可以快进。

我试着:

git checkout HEAD

它没有任何效果。这是因为我在master上签出了一个中间提交。

我怎样才能使主人保持头脑清醒?


尝试git合并origin/master。如果你想确保它只做快进,你可以说git merge——ff-only origin/master。

git checkout master
git pull

应该做这份工作。

每次当你在不同于master的分支上工作时,你都会得到“你的分支落后”消息,有人对master进行了更改,而你git拉。

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

现在,源/主引用被拉出,但您的主引用没有与其合并

(branch) $ git checkout master
(master) $ 

现在master在origin/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

现在你的master和原点/master是同步的

做的事情:

git checkout master
git pull origin

将获取并合并origin/master分支(你可能会说git pull as origin是默认的)。

在你的情况下,git rebase也会奏效。因为你没有master没有的更改,git会快进。如果你正在使用一个rebase工作流,这可能是更可取的,因为如果你搞砸了,你就不会以合并提交告终。

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

不需要复杂。你就站在你的树枝上,做一个git拉。这对我很管用。

或者,作为第二次尝试,git拉origin master。但这只是以防万一,如果你第一个命令不走运的话。

如果你站在不同的分支上,想要签出master的最新版本,你也可以这样做

git checkout -B master origin/master

将分支指针移动到HEAD:

git branch -f master

你的分支主机已经存在,所以Git不允许你覆盖它,除非你使用…-f(这个参数代表——force)

或者你可以使用rebase:

git rebase HEAD master

请自行承担风险;)

要将当前本地跟踪器分支移动到最新的远程状态之上,需要重新设置本地跟踪器分支:

git fetch && git rebase

一般来说,快进和删除本地更改(硬重置)*:

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

快进并保持本地更改(rebase):

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

* -要撤消无意硬重置造成的更改,首先做git reflog。它以相反的顺序显示HEAD的状态。找到HEAD在重置操作之前指向的散列(通常是明显的),并将分支硬重置到该散列。

对于任何想要快进的人来说,如果不签出该分支,他们就不能进入另一个远程分支(包括它本身)。你可以:

git fetch origin master:other

如果您不在其他分支上,这基本上会将other的索引快速转发到origin/master。您可以通过这种方式快速前进多个分支。

如果你在另一个分支上工作了一段时间,并且想要从远程更新过时的分支到它们各自的头:

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

在你的例子中,要快进,运行:

$ git merge --ff-only origin/master

这使用了git merge的——ff-only选项,因为问题特别要求“快进”。

下面是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

如果你在树枝后面的某个地方, 更安全的做法是将你所做的任何局部更改重新设置为:

git pull --rebase

这有助于防止不必要的合并提交。

(这与执行git fetch && git rebase相同。)

在我的方式,它返回所有改变的头,是最快的方式:

git fetch origin master
git reset --hard FETCH_HEAD
git clean -df