Fix Merge Conflicts While Changing Commits During an Interactive Rebase

Chris Achard
InstructorChris Achard
Share this video with your friends

Social Share Links

Send Tweet

We'll enter a more complicated interactive rebase with:

git rebase -i HEAD~2

and intentionally cause a merge conflict in a previous commit.

Then we can fix that merge conflict like normal, but finish up the rebase with:

git rebase --continue

Instructor: [0:00] If we do git log oneline, we have our three commits which are not pushed yet. Let's say we want to change this commit2 and do a more complicated change here.

[0:11] We're going to rebase -i, for interactive, we're going to say HEAD~2, and we want to edit instead of pick the change2. I'm going to enter insert mode with i in vi here, change that to edit, and hit <esc> :wq. Now I'm in an interactive rebase. We can see that with git status. We're currently in a rebase onto this hash.

[0:42] Let's go to app.js. I'm going to change my change2 to say change2-inARebase, and save that. If I look at git status, my app has changed. I'm going to add app.js, and then commit it with --amend, because we want to add it to the current commit that we're rebasing. I'm going to say --no-edit, to not change the message.

[1:11] If I look at the instructions, it says I can git rebase --continue, so let's try to do that. If we do git rebase --continue, we'll get into a merge conflict during a rebase. This is going to happen if you go back in time and change a file that also gets changed later, which is exactly what happened.

[1:29] Let's take a look. We see both modified app.js, so let's go into app.js to change that. Just like any other merge conflict, we have to clean this up manually. There's no easy way to do this. We're going to get rid of the merge conflict lines. We have both change2 and the change2-inARebase. We just want that top one, so we'll get rid of change2. We can save that.

[1:54] We have app.js that needs to be added, so we can add app.js again. We can commit it now with a new message that is the, "Merge rebase changes2 into changes3." We could have amended the current commit we're in, or we can make a new one like this. Then, we can rebase --continue. It says we've successfully rebased all the way to the top.

[2:21] Let's check it out. Let's do git log oneline. We have change1, change2. This now is the merge commit for both 2 and 3. That is a more complicated interactive rebase, but it gets us to the code that we finally wanted.