我在长时间开发分支后切换回master主分支。日志显示:
你的分支比'origin/master'落后167次提交,可以快进。
我尝试了:
git checkout HEAD
这没有任何效果。这是因为我已经检出了 master 的一个中间提交。
我如何让 master 保持在 head 上?
我在长时间开发分支后切换回master主分支。日志显示:
你的分支比'origin/master'落后167次提交,可以快进。
我尝试了:
git checkout HEAD
这没有任何效果。这是因为我已经检出了 master 的一个中间提交。
我如何让 master 保持在 head 上?
尝试使用 git merge origin/master
。如果您想确保它仅进行快进合并,您可以使用 git merge --ff-only origin/master
。
origin/master
部分是否是必需的或者它是否有一个合理的默认值,但我发现为了快速推进合并,创建一个别名是很有用的。因此,我想确保使用上游分支而不是将其硬编码为 origin/master
:ff = merge --ff-only @{u}
(@{u}
表示上游)。 - Vala正在进行:
git checkout master
git pull origin
将获取并合并origin/master
分支(你可以只用git pull
,因为origin是默认值)。
git merge --ff-only origin/master
,它将自动快进,并且我不必再次进行pull
操作? - aspiring1在你的情况下,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 pull
存在一些问题,知情雇主(或者仅仅是Git项目)都不愿意处理。特别是它会留下一个合并提交。然而,这些缺点只是个人(或者雇主的个人)口味上的问题。 - Lazerbeak12345git checkout master
git pull
应该可以完成任务。
当你在一个不同于主分支的分支上工作,有人对主分支进行更改并进行git pull时,你会收到“Your branch is behind”消息。
(branch) $ //hack hack hack, while someone push the changes to origin/master
(branch) $ git pull
现在已经拉取了 origin/master 的引用,但是你的 master 分支尚未与其合并
(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
现在你的主分支和远程主分支(origin/master)已经同步了
如果有人想要快进,他们不能在没有检出该分支的情况下转到另一个远程分支(包括它本身)。您可以执行以下操作:
git fetch origin master:other
如果你不在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 checkout -B master origin/master
要对当前本地的 tracker 分支进行 rebase 操作,即将本地的更改移动到最新的远程状态之上:
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}
* - 要撤销非故意硬重置引起的更改,请首先执行git reflog
。这将以相反的顺序显示HEAD的状态。找到重置操作之前HEAD指向的哈希值(通常很明显),然后将分支硬重置为该哈希值。
不需要复杂操作。只需到你的分支并执行 git pull 命令即可。这对我很有效。
或者,作为第二个尝试,执行 git pull origin master 命令。但只有在第一条命令失败时才需要这样做。
如果你在某个分支上滞后了一些,
最好使用变基来应用本地的更改:
git pull --rebase
git fetch && git rebase
相同。
git checkout HEAD
没有任何作用。HEAD
已经代表了当前检出的提交。 - Emre Tapcı