解决冲突后如何让 git rebase 完成

How can I get git rebase to complete after I resolve conflicts

我刚刚尝试对提交到远程存储库的一些更改进行变基,但在解决冲突后无法完成。我尝试按如下方式进行变基:

$ git rebase upstream/master
First, rewinding head to replay your work on top of it...
Applying: My Commit Message
Using index info to reconstruct a base tree...
M   dir_a/dir_b/dir_c/myfile.py
.git/rebase-apply/patch:51: trailing whitespace.

.git/rebase-apply/patch:56: trailing whitespace.

.git/rebase-apply/patch:128: trailing whitespace.

.git/rebase-apply/patch:141: trailing whitespace.

.git/rebase-apply/patch:145: trailing whitespace.

warning: squelched 3 whitespace errors
warning: 8 lines add whitespace errors.
Falling back to patching base and 3-way merge...
Auto-merging dir_a/dir_b/dir_c/myfile.py
CONFLICT (content): Merge conflict in dir_a/dir_b/dir_c/myfile.py
error: Failed to merge in the changes.
Patch failed at 0001 My Commit Message
The copy of the patch that failed is found in: .git/rebase-apply/patch

When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".

解决冲突后,我暂存并提交我的更改:

$ git add dir_a/dir_b/dir_c/myfile.py
$ git commit dir_a/dir_b/dir_c/myfile.py -m"Fixed rebase conflicts"
[detached HEAD a5a4f3b3e] Fixed rebase conflicts
 1 file changed, 193 insertions(+), 13 deletions(-)

但是当我尝试继续时,它失败了

$ git rebase --continue
Applying: My Commit Message
No changes - did you forget to use 'git add'?
If there is nothing left to stage, chances are that something else
already introduced the same changes; you might want to skip this patch.

When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".

我已经试过很多次了。有时对我所在的分支进行更详细的检查,只暂存我的冲突解决方案但不提交它们,并且总是回到同一点。

例如,如果我在继续变基之前中止并且不提交,我会看到相同的错误:

$ git rebase --abort
$ git status
On branch rebase1
nothing to commit, working tree clean

现在,当我开始变基时,我看到了(或相信我看到了)相同的结果

$ git rebase upstream/master
First, rewinding head to replay your work on top of it...
Applying: My Commit Message
Using index info to reconstruct a base tree...
M   dir_a/dir_b/dir_c/myfile.py

Falling back to patching base and 3-way merge...
Auto-merging dir_a/dir_b/dir_c/myfile.py
CONFLICT (content): Merge conflict in dir_a/dir_b/dir_c/myfile.py

error: Failed to merge in the changes.
Patch failed at 0001 My Commit Message
The copy of the patch that failed is found in: .git/rebase-apply/patch

When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".

解决冲突后我 addrebase 结果相同:

$ git add dir_a/dir_b/dir_c/myfile.py
$ git rebase --continue
Applying: My Commit Message
Applying: My Commit Message For New Code I want to Rebase
Using index info to reconstruct a base tree...
M   dir_a/dir_b/dir_c/myfile.py
Falling back to patching base and 3-way merge...
Auto-merging dir_a/dir_b/dir_c/myfile.py
CONFLICT (content): My Commit Message
error: Failed to merge in the changes.
Patch failed at 0002 Made changes to pass flake8
The copy of the patch that failed is found in: .git/rebase-apply/patch

When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".

我需要 do/not 做什么才能成功变基我的更改?

解决变基冲突时不应提交更改。完成文件编辑以解决冲突后,添加新更改(例如 git add dir_a/dir_b/dir_c/myfile.py)并使用 git rebase --continue.

完成变基

我包括这个答案,因为它解决了我遇到的问题,虽然不是我所说的那个问题。当我第一次尝试 rebase 时,我在没有 committing 的情况下 add 我更改了文件,但是当我尝试 git rebase --continue 时(我相信)没有任何运气。然后我在执行 git rebase --continue 之前尝试 commit 我的更改,但仍然没有成功。

我的错误在于没有意识到在解决一组冲突后,当我再次尝试 rebase 时,可能会出现新的冲突,需要解决这些问题。当我修复这些新的冲突时,我能够成功 rebase

在 OSX 我不得不使用 git config --global core.trustctime false

参见:Git rebase fails, 'Your local changes to the following files would be overwritten by merge'. No local changes?