git: Moving partial changes between commits

Now and then I face the fact that I’ve added changes to a commit I’d like to have moved into a different commit. Here is what you do:

What’s there

We have two commits. For illustration purposes I’ve trimmed the log output down:

$ git log --stat
commit 19c698a9ee91a5f03f1c3240fc957e6b328931f5

    WIP: adding tests

 parts/tests/functional/       |  4 ++--
 parts/tests/functional/  | 43 ++++++++++                              | 14 +++++++++++++-

commit c7ef6c3014ca9d049dea46fbed44010acf53ae79

    prepare frob frob schemas

 parts/tests/functional/           | 31 +++++++++++++
 frobfrob/                           | 32 +++++++++++++

commit 5b30d351f51fda40d37d2f7dc25d2367bd37845a

Now I want to move the changes made to from commit c7ef6c3014ca9d049dea46fbed44010acf53ae79 into commit 19c698a9ee91a5f03f1c3240fc957e6b328931f5 (or HEAD).

Pluck out the commit

In order to pluck out the changes to, we’ll reset the file against the previous commit 5b30d351f51fda40d37d2f7dc25d2367bd37845a (you could also use HEAD~3).

$ git reset 5b30d351f51fda40d37d2f7dc25d2367bd37845a parts/tests/functional/
Unstaged changes after reset:
M       parts/tests/functional/

$ git status -s
MM parts/tests/functional/

As you can see, we will have staged changes and unstaged changes. The staged changes remove the additions to the file and the unstaged changes add our code to

Remove and Add

We now create two commits:

  1. Use the staged changes for a new commit which we’ll squash with c7ef6c3014ca9d049dea46fbed44010acf53ae79.
  2. Stage the unstaged changes and create another commit which we’ll squash with 19c698a9ee91a5f03f1c3240fc957e6b328931f5 or HEAD.
# 1. commit Message is something like: squash: removes changes to
$ git commit

# 2. commit
# stage changes
$ git add -p

# commit, message will be something like: squash: adds changes to
$ git commit

# we end up with two additional commits
$ git log --oneline
492ff22 Adds changes to conftest
8485946 removes conftest files
19c698a WIP: adding tests
c7ef6c3 prepare frob frob schemas
Interactive rebase put’s it all together

Now use an interactive rebase to squash the changes with the right commits:

$ git rebase -i HEAD~5

Abort a git commit –amend

The situation

You hack on a patch, add files to the index and with a knee jerk reaction do:

git commit --amend

(In fact, I do this in my editor with the vim-fugitive plug-in, but it also happened in the terminal). For the commit message git places you in your text editor. If you quit, your changes are merged with the last commit. Being aware of your trapped situation, what do you do?

The solution

Simply delete the commit message (up to where the comments start with #). The typical git commit-hook will see it as a commit with an empty message and abort the commit and therefore the merge.

Undo a git reset

I just happened to muck around with a repository and “accidently” reset it to a previous commit. It looked as if I lost all my recent changes. But I found out that you can “undo” the operation, here is how:

Lets say you’ve reset your repository to specific commit in the past:

$ git reset --hard b0f7f7e600b1add7d27cc6794c68ec332a8eb90e

Now the latest commit is obviously b0f7f7e600b1add7d27cc6794c68ec332a8eb90e and all newer commits seem to be gone. You can figure out the SHA id of your previous HEAD with the reflog:

$ git reflog
c8b2660 HEAD@{0}: commit (amend): Bla foobar
780cd51 HEAD@{1}: commit: updated HEAD

Your previous HEAD should be among those entries in the reflog. Look for the ‘updated HEAD’ commit. In my example it’s HEAD@{1}. Pick the SHA of the commit you think was the previous HEAD and reset the repository again:

$ git reset --hard 780cd51