如何快速前进分支?

pen*_*001 234 git

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

您的分支在167个提交后面是"origin/master",可以快速转发.

我试过了:

git checkout HEAD
Run Code Online (Sandbox Code Playgroud)

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

如何让主人留在头上?

rob*_*off 330

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

  • `--ff-only`非常有用. (25认同)
  • 当你的遥控器有一些认证箍跳过时,这很好用.当我拉入一个分支时,我必须进行身份验证.然后,当我切换到另一个分支(即,挑选我的更改)时,我更喜欢使用这个`merge`命令,这样我就不必重新进行身份验证了. (4认同)
  • 我不知道`origin/master`部分是否是必需的,或者它是否合理默认,但我发现为快进制作别名很有用所以我想确保使用上游分支而不是硬编码`origin/master`:`ff = merge --ff-only @ {u}`(`@ {u}`是上游). (3认同)
  • 如果离线,那比建议的答案要好 (2认同)
  • 你能解释一下为什么简单的拉动不能做同样的事情吗?另外,如果我们这样做,还需要拉动吗? (2认同)

Gre*_*ill 235

这样做:

git checkout master
git pull origin
Run Code Online (Sandbox Code Playgroud)

将获取并合并origin/master分支(您可以说git pull原点是默认值).

  • 我认为Rob的答案更好.我一般遇到这种情况我有*刚拉完*,然后我切换到不同的分支,它需要快速转发.这很烦人给我,如果我必须做的另一个(无操作)拉并等待它完成; 做一个仅限本地的操作更快,也是我想要的. (49认同)

voi*_*tor 36

在你的情况下,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
Run Code Online (Sandbox Code Playgroud)

  • 对我来说非常有用,因为我们不应该使用 git pull! (2认同)

Kub*_*uba 27

git checkout master
git pull
Run Code Online (Sandbox Code Playgroud)

应该做的工作.

每当你在一个不同于主人的分支上工作时,你会得到"你的分支在后面"的消息,有人确实改变了主人并且你git pull.

(branch) $ //hack hack hack, while someone push the changes to origin/master
(branch) $ git pull   
Run Code Online (Sandbox Code Playgroud)

现在拉出了原点/主参考,但是你的主机没有与它合并

(branch) $ git checkout master
(master) $ 
Run Code Online (Sandbox Code Playgroud)

现在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
Run Code Online (Sandbox Code Playgroud)

现在你的主人和原始人/主人是同步的


Sun*_*tel 16

对于想要快进的任何人,如果不检查该分支,他们就不会进入另一个远程分支(包括它自己),您可以这样做:

git fetch origin master:other
Run Code Online (Sandbox Code Playgroud)

如果您不在分支上,这基本上会快进otherto的索引。您可以通过这种方式快进多个分支。origin/masterother

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

git fetch origin master:master other:other etc:etc
Run Code Online (Sandbox Code Playgroud)


jon*_*tro 12

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

git checkout -B master origin/master


aud*_*ude 6

在您的情况下,要快进,请运行:

$ git merge --ff-only origin/master
Run Code Online (Sandbox Code Playgroud)

这使用 的--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.
Run Code Online (Sandbox Code Playgroud)

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

$ git config --global alias.ff 'merge --ff-only @{upstream}'
Run Code Online (Sandbox Code Playgroud)

现在我可以运行它来快进:

$ git ff
Run Code Online (Sandbox Code Playgroud)