I want to use git rebase
to take a sequence of commits and apply them to a different root commit. For example,
git rebase --onto root start finish
to get the commits from start
to finish
based on root
.
When git cannot apply a commit cleanly, it updates the file to show conflicts like this (example from git manual):
Here are lines that are either unchanged from the common
ancestor, or cleanly resolved because only one side changed.
<<<<<<< yours:sample.txt
Conflict resolution is hard;
let's go shopping.
=======
Git makes conflict resolution easy.
>>>>>>> theirs:sample.txt
And here is another line that is cleanly resolved or unmodified.
The programmer edits the file to what it should be in the new branch, and then runs git --rebase continue
to continue adding the commits from the source.
However, when there are a lot of changes in the file between root
and start
, there may be many lines like this and they may be hard to interpret. In such cases, one might prefer to have the "failed hunks" output to a file, so that one could read through the changes in the original commit (manually making the necessary changes in the file being altered, and then running git rebase --continue
to continue adding commits).
The --reject
option of git apply
does this:
--reject
For atomicity, git apply by default fails the whole patch and does
not touch the working tree when some of the hunks do not apply.
This option makes it apply the parts of the patch that are
applicable, and leave the rejected hunks in corresponding *.rej
files.
This is also the behaviour of the patch
program - so I can get what I want by first outputing the commit with git show
, and then applying it with patch
. This is not convenient when there are many commits involved, however.
Is there any way of doing this with git rebase
(or another git command)?
You can do something like this on a commit-by-commit basis.
When a merge conflict occurs, git gives a message like:
If file.c is mangled due to showing merge conflicts in the file, you can get the file back to the way it was before the commit with
You can then run
The output of this could look something like
Then make the changes in file.c.rej manually, then run
to continue.