我会尝试将命令拼凑起来。
git checkout feature
git pull --rebase origin develop
- 修复冲突,完成变基
git push
此时你会得到类似的东西。
To github.com:org/repo.git
! [rejected] feature -> feature (non-fast-forward)
error: failed to push some refs to 'git@github.com:org/repo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
提示错误,这里的git pull 是错误的。 您应该用自己的 git push --force 覆盖上游 feature。这是因为您重写了 feature 分支,并且它与上游分支分歧。
以下是更详细的情况。一开始,事情看起来像这样。请注意,上游 origin/feature 和您本地的 feature 处于同一提交状态。
[origin/develop]
A - B - C - D - E - F [develop]
\
G - H - I [feature]
[origin/feature]
git pull --rebase origin develop 并修复所有冲突后,您的存储库看起来像这样。
[origin/develop]
[develop]
A - B - C - D - E - F - G1 - H1 - I1 [feature]
\
G - H - I [origin/feature]
rebase 不会重写提交。它创造了新的,并假装它一直都是这样。现在feature 和origin/feature 有分歧意味着一个不是另一个的祖先。
当你尝试git push 你的feature Git 拒绝。将origin/feature 沿几个提交移动并不是一件简单的事情,称为“快进”。需要合并,出于安全原因,git push 不会这样做。您的作品似乎与其他人的作品分歧。 push 可能会影响其他人在同一分支上的工作。
这就是为什么git push --force 是必要的。它告诉 Git 无论如何都要这样做,将origin/feature 放在提交I1 处。在git push --force 之后你会得到这个。
[origin/develop]
[develop]
A - B - C - D - E - F - G1 - H1 - I1 [feature]
\ [origin/feature]
G - H - I
现在一切都好。你的feature 工作就好像它一直在develop 之上。
如果您再次git pull --rebase origin develop,并且develop 上没有新的提交,则不会发生任何事情。如果有新的提交,您只需处理这些。