1

I've created a pull request with five commits on feature branch, Now my upstream has been ahead of some 10 commits, I want to rebase my feature branch, But while rebasing, It is failing due to merge conflicts! and when I check for conflict, The code is similar to first commit code!, And can I skip this(rebase --skip)? and again conflicts arise in later merge commit with latest code? And what If rebase finishes and I mess up something, Can I do rebase --abort after finishing rebase too?

I don't understand why I need to resolve same conflicts when I did when merging my first commit!

Note : I have merged the first commit in PR, and later did two more commits and merged and then again added 2 commits and this time I am trying to rebase instead of merge!

Update 1 : I have tried to rebase by resolving conflicts but I am getting this error, now I am not sure whether I can just remove it! Please help!

Timer
  • 37
  • 1
  • 7
  • During rebase, git compare HEAD version with the commit you rebase separately. During rebase, you can use `git status` to check the conflict files. And for `--skip` option for `git rebase` command, is skip to rebase the commit; `--abort` option is cancel the rebase. More details, you can refer https://git-scm.com/docs/git-rebase. – Marina Liu Mar 27 '18 at 10:00
  • Your question suggests that you are running `git rebase` after already running `git merge`. This is tricky to do correctly. Are you actually doing that? – torek Mar 27 '18 at 14:51
  • @torek yes! I am doing that! :( I already did git merge once and again I have pushed to 2 more commits into my PR, now I need to rebase! can rebase be done in such case at all? and if I do rebase, then how my feature branch looks like? first merge commit(my own) and then commits from master and then on top of those, my last 2 commits? (or) all master commits, then my merge commit and then my last 2 commits? I added an update to question, please check! – Timer Mar 28 '18 at 08:29
  • I need a lot more information. Is your repository public? If so, I can clone it directly and view what you're doing. If not, I can only show some general principles. – torek Mar 28 '18 at 15:15
  • Ok, Thanks a lot for taking time to look at! – Timer Mar 28 '18 at 16:55

1 Answers1

1

TL;DR: this is actually pretty normal for your case

Because rebase consists of copying commits, and you're copying commits into a new and slightly different source base, these kinds of conflicts, and having to re-resolve them, is pretty normal.

You can look into using Git's rerere.enabled feature (see Are there any downsides to enabling git rerere?) to automate some of this. I have not tried that.

Long

Using your link—https://github.com/mesonbuild/meson/pull/3277—I was able to clone the repository and find the patch:

$ 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]

(this created for me a local branch named feature which I then checked out). Let's take a look now at the commit graph. This is quite long so I am going to snip some—but not all—irrelevant parts, replacing them with a series of dots:

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

Now, the five "most interesting" commits here are the ones that are reachable from feature—from your pull request—but not from 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

This identifies the commits by their hash IDs. Looking at the graph output above, we can see that 531120e8 and 21e7e1fe are ordinary commits, but d3554ceb is a merge commit with two parents: the first one is 21e7e1fe (your own work), and the second is 3e48d476 Squash target_type warning for jar targets.

One issue with any git rebase is that it works by copying commits.1 It is, in any useful sense, impossible to properly copy a merge commit, and rebase doesn't even try. Instead, it just throws out the merge entirely.

Hence, if we now run git rebase -i master2 while on branch feature, we'll get a set of four, not five, pick commands, in our editor session:

$ 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]

Note that d3554ceb is not listed here. Git is not going to attempt to copy the merge commit.

When you did the merge, you solved a merge conflict that happened "after" your old commits. Since the rebase isn't keeping the merge, and moreover, your new copies are going to come "after" the point where the conflict occurs, you will have to solve the same conflict again, probably more than once.


1Rebase copies commits using either git cherry-pick, without any -m argument, or git apply -3 of a formatted patch.

2What we want here is to rebase on commit 9b2e533d. In my clone, the names master and origin/master both point to this commit hash. I could also run git rebase -i 9b2e533d.


Solving the conflicts manually

If you just let this rebase run, it immediately hits a merge conflict:

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

I resolved it manually myself using the same method you did in your merge, and ran git rebase --continue, which promptly hit the problem again:

$ 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

This merge conflict is a bit different, but related, and is again in run_unittests.py—basically, Git is unable to cherry-pick the original commit because its surrounding context is too different. (It might or might not have helped to have resolved the previous conflict by adding the new test after the final two unit tests in this class, rather than before them.)

Fixing that again:

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

we get yet another merge conflict:

[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

This one occurs because the parent of commit 8fe1adcb is your own merge commit d3554ceb, which Git has skipped, so it's comparing d3554ceb to 8fe1adcb to see what to copy while comparing d3554ceb to HEAD (a commit just built by the previous cherry-pick) to see what it should leave alone. There is one minor white space difference that messes with automatic resolution here.

Fixing that manually in favor of the one with correct white space and continuing, I get:

$ 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

Ignoring the rather odd (though technically OK) directions, I ran:

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

which might be correct (I know nothing of this software and have not tested any of these versions).

Rebasing without --interactive

This is really quite similar, you just get a lot less interaction. Git uses git format-patch to turn each of the four (not five) commits into patches and then applies each one with git am --3way. The first one runs into a merge conflict, which I resolved manually keeping the newly added test function at the end this time:

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

It turns out that this particular fix is no longer required, so it's correct to skip it:

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

and now the rebase is finished.

torek
  • 448,244
  • 59
  • 642
  • 775
  • Thanks a lot!!! I tried to do in the same way, but still I encountered "swap file" error which I mentioned in question details, but I just deleted that file and then continued rebasing, and force pushed the commits! Now the codes seems seemingly same to the previous one(and graph looks like all my commits are added on top), but one of the CI build is failing now :( I don't know what I even messed up! Is that because I deleted some swap file? – Timer Mar 29 '18 at 11:32
  • The complaint about `.git/.COMMIT_EDITMSG.swp` is a separate thing. It comes from the editor `vim`, which believes (based on the file) that you have another window or some such open *already* editing that file. The `PID` you see is the process ID of the other editor session that vim believes is running right now and editing that file. If there *is* such a session, that suggests that you are trying to commit something in that other window, too. – torek Mar 29 '18 at 14:34
  • JFTR, it also may mean a backgrounded console `vim` (a user pressed `ctrl-z` while editing a commit) or a killed (and hence gone) `vim` process. Vim tells whether the process created the swap file is still active and prints its PID if it beleives it's true. – kostix Mar 29 '18 at 15:35
  • 1
    @kostix: yes (that's why I said "or some such"). Note that if you have a shared mount point (NFS or Samba or some such, or shared part of a file system through Virtualbox or similar) and run different vim-s on different machines or VMs on the shared FS, you can get cases where the stored PID is meaningless but the conflict between editor sessions is real! It's often worth figuring out who's editing, where, how, and why: is it just a leftover `.swp` or `.swo` file from a crashed/killed session, or not? – torek Mar 29 '18 at 15:42
  • Ok, I see, with rebase , we can use --preserve-merges and done this too right? (I used that earlier, but aborted), but you haven't used -p at all, is it because that merge commit is just a minor change? – Timer Mar 29 '18 at 16:19
  • @Timer: Using `--preserve-merges` is even trickier: it doesn't *copy* the merge at all, it just performs a *new* merge. You have to resolve the conflicts again. If you are rebasing for integration purposes like this, keeping merges is something they probably *don't* want you to do. – torek Mar 29 '18 at 16:36
  • @torek I see, I am still a git beginner, I can't thank you enough for such a long and well explaining answer! Thank you so much! – Timer Mar 29 '18 at 16:57