Git tips: To The Hotfix and Back


What:

Save the state of unfinished feature-branch, to checkout something else. Then back to it.

How:

Make an temporary commit before checking out other branch. Break it down to resume work.

Demo:

Why:

Sometimes you need to pause your current work, and do independent changes in other branch: e.g. hotfix. In many cases you can’t checkout from branch with uncommited changes. There is a nice git stash command, that allows you to save your work as a detached diff, and then apply it when necessary. It’s really good, if you need to checkout and keep your changes with you. But when you change context, it’s much better to left a real commit behind, in the branch you are working with. In that case you will never forget where is your work. So, I use two aliases, allowing me to left temporary commit and break it with ease.

Setup:

Add it to your ~/.gitconfig in [alias] section.

[alias]
    tmp = commit -a -m"tmp"
    utmp = reset --soft HEAD~1

Real Example:

unfinished_feature ➜ ✗ git checkout master 
error: Your local changes to the following files would be overwritten by checkout:
        change.txt
Please, commit your changes or stash them before you can switch branches.
Aborting

unfinished_feature ➜ ✗ git tmp
[unfinished_feature b408f65] tmp
 1 file changed, 1 insertion(+)

unfinished_feature ➜ git checkout master
 Switched to branch 'master'
 # do your job

master ➜ git checkout -
Switched to branch 'unfinished_feature'
unfinished_feature ➜ git graph

 * b408f65 - (HEAD -> unfinished_feature) tmp (2 minutes ago) <Pavel Dionisev>
 * b79bd19 - Commit 1 (5 minutes ago) <Pavel Dionisev>

unfinished_feature ➜ git utmp

unfinished_feature ➜ ✗ git status
On branch unfinished_feature
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   change.txt

unfinished_feature ➜ ✗ git graph
* b79bd19 - (HEAD -> unfinished_feature) Commit 1 (6 minutes ago) <Pavel Dionisev>