Often I want to break up a large commit:
git commit -a -m "Monolith"
I know how to split a commit (git gui
is my friend) but sometimes I actually want to rework it by hand...
git branch temp # Keep a reference to all my hard work
git reset --hard HEAD~ # Rewind 'my-topic' branch
# hack hack
git commit -a -m "Refactor 1"
# hack hack
git commit -a -m "Refactor 2"
To complete it, I then want to apply another commit that brings the code back to exactly the state stored in branch temp
, usually including the original commit message.
Is there an easy way to do this?
I imagined that a merge-strategy on cherry-pick
might get the job done, but alas none of these did:
git cherry-pick --strategy=theirs temp # I am told about conflicts
git status # ... But here I find no changes to commit!
git cherry-pick --strategy=ours temp # Worth a try. Nope, empty commit.
git cherry-pick --strategy=recursive --strategy-option=ours temp # Partial result, bits are missing
I know of one way to achieve this:
git log -1 temp # note the SHA
git checkout temp
git reset --soft my-topic
git commit -C <SHA-from-above> # branch 'temp' now contains the desired commit
git checkout HEAD -B my-topic
git br -d temp
However this seems a very error-prone way to go about it. Any little mistake and the commits I want to keep might not be in any branch (git reflog
to the rescue...). I'd like to find something more logical and easier to remember/do.