David - Musings of an SRE

Unstage your last git commit - Temporary commits for workstation switches

Undo a git commit.

Sometimes when I’m switching between workstations while working halfway onto code, I’d need to find a way to transfer WIP code over.

There are a couple of ways to do this. Some would create patches, copy it into dropbox, and pull and apply it on the new workstation.

For me, I would just create a new branch, commit the changes as “WIP”, push it to the remote repository and pull the whole branch over from the other workstation.

Easy enough.

But then comes the problem. Normally, at this stage, I would just compare what was different with the previous commit and continue the work. Once everything is done, I would commit it with the proper commit message and then do a interactive rebase (git rebase -i HEAD~10) and squash away.

But that seems a little tedious, and perhaps even wrong :-O

There’s another way to do this:

$ git commit ...              
$ git reset --soft HEAD^      
$ edit                        
$ git add ....                
$ git commit -c ORIG_HEAD     
  1. git reset –soft HEAD^ would unstage the last commit so you kinda rollback to where you left off before you committed the “WIP” code.
  2. git commit -c ORIG_HEAD would commit your changes and allow you to change the commit message.

So remember, git reset –soft HEAD^.