为什么在解决rebase冲突时会显示先前提交的代码?

时间:2018-03-27 05:23:47

标签: python git python-3.x github rebase

我已经在功能分支上创建了一个五次提交的拉取请求,现在我的上游已经超过了大约10次提交,我想重新设置我的功能分支,但是在重新定位时,由于合并冲突而失败!当我检查冲突时,代码类似于第一个提交代码!,我可以跳过这个(rebase --skip)吗?并且在以后的合并提交和最新代码中再次发生冲突?如果rebase完成并且我搞砸了什么,我可以进行rebase - 在完成rebase后也可以吗?

我不明白为什么在合并第一次提交时我需要解决相同的冲突!

注意:我已经合并了PR中的第一个提交,后来又做了两次提交并合并,然后又添加了2个提交,这次我试图改变而不是合并!

更新1:我试图通过解决冲突来改变,但我得到this error,现在我不确定我是否可以删除它!请帮忙!

1 个答案:

答案 0 :(得分:0)

TL; DR:这对你的情况来说实际上是正常的

因为rebase包含复制提交,并且你将提交复制到一个新的,略有不同的源代码库中,这些类型的冲突,并且必须重新解决它们,这是很正常的

您可以尝试使用Git的rerere.enabled功能(请参阅Are there any downsides to enabling git rerere?)来自动完成其中一些功能。我没试过。

使用你的链接 - https://github.com/mesonbuild/meson/pull/3277 - 我能够克隆存储库并找到补丁:

$ git clone https://github.com/mesonbuild/meson
Cloning into 'meson'...
remote: Counting objects: 33400, done.
[snip]
$ cd meson/
$ git fetch origin refs/pull/3277/head:refs/heads/feature
remote: Counting objects: 31, done.
remote: Total 31 (delta 20), reused 20 (delta 20), pack-reused 11
[snip]

(这为我创建了一个名为feature的本地分支,然后我检查了它。现在让我们看看提交图。这很长,所以我要剪掉一些但不是全部无关的部分,用一系列点代替它们:

$ git log --decorate --oneline --graph master feature
* 9b2e533d (origin/master, origin/HEAD, master) Always build parser objects anew to avoid leaking old data.
* 977acc94 Do not leave open file handlers, use context manager to clean them up
* 8efd9400 pkgconfig generator: Add required version
*   f6f07840 Merge pull request #2976 from dzabraev/fix-reversed-order
|\  
| * ea6e9298 keep include paths order
............
* | c4192a04 Support data types larger than 128 bytes
| | * 6a3db989 (HEAD -> feature) Fixing typo closes #2865
| | * 8fe1adcb Fixing typo closes #2865
| | *   d3554ceb Fixing PR changes closes #2865
| | |\  
| |_|/  
|/| |   
* | | 3e48d476 Squash target_type warning for jar targets
................
* | |   12bac512 Fix b_ndebug=if-release silently not working
|\ \ \  
| * | | 6910f604 Disable b_ndebug tests on MSVC
................
| * | | 39a3bdb4 Add tests for b_ndebug=if-release and buildtype default options
* | | | 30827b56 Do not install configure_file output if install_dir is empty. Closes #3270.
|/ / /  
| | * 21e7e1fe PR review changes closes #2865
| | * 531120e8 fix2865
| |/  
|/|   
* | dd614015 Open mesontest logfiles in utf-8 mode
............

现在,五个"最有趣的"这里的提交是feature可以从您的请求中获得的 - 但不是来自master

$ git log --no-decorate --oneline master..feature
6a3db989 Fixing typo closes #2865
8fe1adcb Fixing typo closes #2865
d3554ceb Fixing PR changes closes #2865
21e7e1fe PR review changes closes #2865
531120e8 fix2865

这通过其哈希ID来标识提交。查看上面的图表输出,我们可以看到531120e821e7e1fe是普通提交,但d3554ceb是一个 merge 提交,有两个父母:第一个是21e7e1fe(您自己的作品),第二个是3e48d476 Squash target_type warning for jar targets

任何 git rebase的一个问题是它通过复制提交来工作。 1 在任何有用的意义上,无法正确复制合并提交,并且rebase甚至不会尝试。相反,它只是完全抛出合并。

因此,如果我们现在在分支git rebase -i master上运行feature 2 ,我们将获得一组四个而不是五个pick命令,在我们的编辑器会话中:

$ git rebase -i master
pick 531120e8 fix2865
pick 21e7e1fe PR review changes closes #2865
pick 8fe1adcb Fixing typo closes #2865
pick 6a3db989 Fixing typo closes #2865

# Rebase 9b2e533d..6a3db989 onto 9b2e533d (4 commands)
[snip rest of instructions]

请注意,此处未列出d3554ceb。 Git不会尝试复制合并提交。

当您进行合并时,您解决了发生的合并冲突""你的旧提交。由于rebase不是保持合并,而且,你的新副本将会在"之后出现。在发生冲突的地方,您将不得不再次解决相同的冲突,可能不止一次。

1 Rebase使用git cherry-pick,没有任何-m参数或格式化补丁的git apply -3复制提交。

2 我们在这里要的是在提交9b2e533d上进行rebase。在我的克隆中,名称masterorigin/master都指向此提交哈希。我也可以运行git rebase -i 9b2e533d

手动解决冲突

如果您只是让这个rebase运行,它会立即遇到合并冲突:

Auto-merging run_unittests.py
CONFLICT (content): Merge conflict in run_unittests.py
Auto-merging mesonbuild/build.py
error: could not apply 531120e8... fix2865

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".

Could not apply 531120e8... fix2865
$ git status
[snip]
        both modified:   run_unittests.py

我使用您在合并中使用的相同方法手动解决了它,然后运行了git rebase --continue,这会立即再次遇到问题:

$ git add run_unittests.py
$ git rebase --continue
[snip editor session]
[detached HEAD bbd74944] fix2865
 Author: chitranjali <chitranjali189@gmail.com>
 6 files changed, 51 insertions(+)
 create mode 100644 test cases/unit/25 shared_mod linking/installed_files.txt
 create mode 100644 test cases/unit/25 shared_mod linking/libfile.c
 create mode 100644 test cases/unit/25 shared_mod linking/main.c
 create mode 100644 test cases/unit/25 shared_mod linking/meson.build
Removing test cases/unit/25 shared_mod linking/installed_files.txt
Auto-merging run_unittests.py
CONFLICT (content): Merge conflict in run_unittests.py
Auto-merging mesonbuild/build.py
error: could not apply 21e7e1fe... PR review changes closes #2865

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".

Could not apply 21e7e1fe... PR review changes closes #2865

这个合并冲突有点不同,但是相关,并且又在run_unittests.py - 基本上,Git无法挑选原始提交,因为它的周围环境太不同了。 (通过在此类中的最后两个单元测试之后添加新测试,而不是在它们之前添加新测试,可能会或可能没有帮助解决之前的冲突。)

再次修复:

$ git add run_unittests.py
$ git rebase --continue
[snip editor session]

我们又发生了另一次合并冲突:

[detached HEAD 08b4eefc] PR review changes closes #2865
 Author: chitranjali <chitranjali189@gmail.com>
 4 files changed, 10 insertions(+), 11 deletions(-)
 delete mode 100644 test cases/unit/25 shared_mod linking/installed_files.txt
Auto-merging run_unittests.py
CONFLICT (content): Merge conflict in run_unittests.py
error: could not apply 8fe1adcb... Fixing typo closes #2865

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".

Could not apply 8fe1adcb... Fixing typo closes #2865

发生这种情况是因为提交8fe1adcb的父级是您自己的合并提交d3554ceb,Git已跳过它,因此它将d3554ceb8fe1adcb进行比较在将d3554cebHEAD(由前一个樱桃选择刚刚建立的提交)进行比较时查看要复制的内容,以了解它应该单独留下什么。这里有一个微小的空白区域与自动分辨率相混淆。

手动修复有正确空白区域并继续使用的那个,我得到:

$ git add run_unittests.py
$ git rebase --continue
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:

    git commit --allow-empty

Otherwise, please use 'git reset'
interactive rebase in progress; onto 9b2e533d
Last commands done (4 commands done):
   pick 8fe1adcb Fixing typo closes #2865
   pick 6a3db989 Fixing typo closes #2865
No commands remaining.
You are currently rebasing branch 'feature' on '9b2e533d'.

nothing to commit, working tree clean
Could not apply 6a3db989... Fixing typo closes #2865

忽略了相当奇怪的(虽然技术上很好)方向,我跑了:

$ git rebase --continue
Successfully rebased and updated refs/heads/feature.

这可能是正确的(我对这个软件一无所知,也没有测试过任何这些版本)。

没有--interactive

的重新定位

这非常相似,只是减少了很少的互动。 Git使用git format-patch将四个(而不是五个)提交中的每个提交转换为补丁,然后将每个提交应用git am --3way。第一个遇到合并冲突,我这次手动解决了新添加的测试功能:

$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: fix2865
Using index info to reconstruct a base tree...
M   mesonbuild/build.py
M   run_unittests.py
Falling back to patching base and 3-way merge...
Auto-merging run_unittests.py
CONFLICT (content): Merge conflict in run_unittests.py
Auto-merging mesonbuild/build.py
error: Failed to merge in the changes.
Patch failed at 0001 fix2865
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".

[snip editor session on run_unittests.py]
$ git add run_unittests.py
$ git rebase --continue
Applying: fix2865
Applying: PR review changes closes #2865
Using index info to reconstruct a base tree...
M   mesonbuild/build.py
M   run_unittests.py
.git/rebase-apply/patch:10: trailing whitespace.
                mlog.warning('''target links against shared modules. This is not 
.git/rebase-apply/patch:36: trailing whitespace.
        msg = ('''WARNING: target links against shared modules. This is not 
warning: 2 lines add whitespace errors.
Falling back to patching base and 3-way merge...
Removing test cases/unit/25 shared_mod linking/installed_files.txt
Auto-merging run_unittests.py
Auto-merging mesonbuild/build.py
Applying: Fixing typo closes #2865
Using index info to reconstruct a base tree...
M   run_unittests.py
Falling back to patching base and 3-way merge...
Auto-merging run_unittests.py
CONFLICT (content): Merge conflict in run_unittests.py
error: Failed to merge in the changes.
Patch failed at 0003 Fixing typo closes #2865
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".

[snip editor session again]
$ git add run_unittests.py
$ git rebase --continue
Applying: Fixing typo closes #2865
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 --skip
Applying: Fixing typo closes #2865
Using index info to reconstruct a base tree...
M   run_unittests.py
Falling back to patching base and 3-way merge...
No changes -- Patch already applied.

现在rebase已经完成。