远程分支后面的本地分支(pull、rebase、fetch、merge)

Local branch behind remote branch (pull, rebase, fetch, merge)

如果我在我的分支上工作,branch1 然后我推送一些提交,而我的团队成员也在 branch1 上工作——当我的团队成员推送他的提交时变化,他现在落后了。

他获取我最近的提交并尝试将他的更改与我的合并的最简单方法是什么?假设他在意识到这个错误之前已经提交了更改。

我以为你会这样做:

git pull origin/branch1 && git merge origin/branch1

但这似乎根本不起作用。

我相信你会做一个 rebase,但我不确定这个过程;所有人都在谈论用 masterrebase--但此时我不想用 master 做任何事情。

git pull origin/branch1 && git merge origin/branch1

git pull

git pull 实际上是这两个命令的别名:git fetch + git merge 所以命令的第二部分没有用。


抓取变化的方法是这样- 几个选项:

# Update your local repo with the latest code:
git fetch --all --prune

# Merge the changes (you already did a pull)
git merge origin branch1

或:

# grab & merge the latest changes into your current branch 
git pull origin branch1

rebase

如果您希望您的更改位于其他更改之上,那么您可以在提取内容时使用 --rebase 标志。

# As before - update your local repo with the latest code:
git fetch --all --prune

# Merge the changes with the rebase flag
git pull --rebase origin/branch1

图片来源:http://blogs.atlassian.com/


自动存储 + 变基

您有可以设置的配置:

rebase.autoStash + pull.rebase

rebase.autoStash

When set to true, automatically create a temporary stash before the operation begins, and apply it after the operation ends.
This means that you can run rebase on a dirty worktree.

However, use with care: the final stash application after a successful rebase might result in non-trivial conflicts. Defaults to false.

pull.rebase

When true, rebase branches on top of the fetched branch, instead of merging the default branch from the default remote when "git pull" is run.

git config pull.rebase true
git config rebase.autoStash true