如何快速前进分支?

时间:2012-03-01 07:58:11

标签: git

我在分支上长时间开发后切换到了主人。日志显示:

  

你的分支在167个提交后面是'origin / master',可以快进。

我试过了:

git checkout HEAD

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

如何让主人留在头上?

11 个答案:

答案 0 :(得分:312)

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

答案 1 :(得分:230)

这样做的:

git checkout master
git pull origin

将获取并合并origin/master分支(您可以只说git pull,因为默认为origin。)

答案 2 :(得分:34)

在你的情况下,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

答案 3 :(得分:26)

git checkout master
git pull

应该做的。

每次当你在不同于主人的分支上工作时,你会得到“你的分支落后”的消息,有人会改变主人并且你git pull。

(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

现在您的主人和出生地/主人同步

答案 4 :(得分:10)

如果您站在不同的分支机构并想要查看最新版本的主人,您也可以

git checkout -B master origin/master

答案 5 :(得分:2)

不需要复杂性 只是站在你的分支机构,做一个 git pull 它对我有用

或者,作为第二次尝试 git pull origin master ,以防万一你不幸遇到第一个命令

答案 6 :(得分:0)

rebase 当前本地跟踪器分支,在最新的远程状态之上移动本地更改:

$ 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在复位操作之前指向的哈希值(通常是显而易见的)和将分支重置为该哈希值。

答案 7 :(得分:0)

对于任何想快速转发的人,他们不在上而无需签到该分支,则可以执行以下操作:

git fetch origin master:other

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

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

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

答案 8 :(得分:0)

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

$ git merge --ff-only origin/master

这使用了--ff-only的{​​{1}}选项,因为该问题专门要求“快进”。

以下是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}'

答案 9 :(得分:0)

如果您在分支机构的后面,
变基您可能有的任何本地更改更安全:

git pull --rebase

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

答案 10 :(得分:-1)

将分支指针移动到HEAD:

git branch -f master

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

或者您可以使用rebase:

git rebase HEAD master

自行承担风险;)