When we run into merge conflicts during a rebase, we are effectively in the middle of a merge, so the rules for git checkout --ours/--theirs
from above still apply. But, the tricky part is identifying the "current" branch. Let me explain what happens during a rebase.
What happens during a rebase?
Again, let's assume the following history:
A---B---C feature
/
D---E---F---G master
When we rebase master
"into" feature
, what we are really doing is this:
"Roll back" to the common ancestor and save the diff
In our case, we roll back to commit E, and save the diff of each commit introduce by the feature
branch.
A---B---C (saved in temporary files)
feature
/
D---E---F---G master
Reset the feature
branch to the current commit from master
The feature
branch now has the same history as master
.
A---B---C (saved in temporary files)
feature
/
D---E---F---G master
Apply the saved changes from the feature
branch
Now each change from the feature
branch (A
, B
, and C
) will be applied to the new feature
branch once again. It's important to note, for the sake of this guide, that this is accomplished through a merge.
The new history looks like this:
A---B---C feature
/
D---E---F---G master
Ok, so how do I use git checkout --ours/--theirs
during a rebase?
The point of that long winded explanation was to show that when you are fixing merge conflicts in the middle of a rebase, your "current" branch is not longer your original feature
branch but rather a new branch that is up to date with master
. And the commits being merge into the current branch are the commits from your original feature branch. So --ours
and --theirs
will appear to be flipped around.
Use --ours
to keep changes from the branch being rebased onto (master
)
At the beginning of the rebase, we had feature
branch checked out, so it may seem backward, but we will use --ours
to keep changes from master.
git checkout --ours myscript.py
Use --theirs
to keep the changes from the "current" branch (feature
)
And naturally, the opposite is true. Use --theirs
to keep changes from the feature
branch.
git checkout --theirs myscript.py