How2Tips : Git command squash / reflog / stash

Reading time : < 3 min

Squash commits

You may be asked to squash your commits during the discussion around a PR.
“Squashing” means “fusioning” together many commits into one.

Main advantages are:

  • clean commit history
  • no problem during git bisect

how

git rebase -i HEAD~3 # 3 being the number of commits you want to modify
# this opens an editor, hopefully vim :)
# chose which commits to keep and which to squash
# by writing `squash` for useless ones
:wq # write and quit
# this will open a final editor where you can modify the final commit message
:wq

Done!

Now verify your git history.

All the concerned commits have been modified (their hash changed too). Your git history should contain only the commit(s) you chose to keep.

This also means you should never rebase commits that have been pulled by someone else!

Just push your new git history. You might have to use -f if you already pushed before.

Made a mistake ?
Don’t worry, just use reflog :)

Reflog

You’ll surely make mistakes and be horrified because you deleted everything, or lost commits. (facepalm)

git reflog to the rescue!

0ce6526 HEAD@{19}: commit: fix js callback, heck of a bug with wkhtml
b11961b HEAD@{20}: checkout: moving from 592289b3a3bc1a45455d90010dcfae2300ecf923 to master
592289b HEAD@{21}: checkout: moving from master to 592289b3a3bc1a45455d90010dcfae2300ecf923
b11961b HEAD@{22}: checkout: moving from 592289b3a3bc1a45455d90010dcfae2300ecf923 to master
592289b HEAD@{23}: checkout: moving from master to 592289b3a3bc1a45455d90010dcfae2300ecf923
b11961b HEAD@{24}: commit: add percentage on table instead of pie chart

It will list all the last operations made, be it

  • move to branch
  • merge
  • rebase

You can go back to a previous operation by using git checkout:

git checkout HEAD@{24}

You’ll then be in a detached state. But don’t worry, just create a branch out of it, and you’re safe :)

You could also use git reset –hard:

$ git reset --hard HEAD@{24}

But be careful! –hard is, well, hard. In fact, once something is commited in git, it’s hard to lose anything.

Still –hard is destructive, in the sense it will modify your sources (but git checkout does it too, in fact).

The default behavior of git reset is soft: it will keep your modifications, but remove only the commits, resulting in having unstaged modifications.

Stash

What a pain when you are currently working on a branch and you suddenly need to do a quick fix on another branch. What to do with your pending modifications ? Here comes git stash !

Let’s say you are working on the branch feature/awesome-stuff when your colleague ask you to fix a bug on the branch feature/painful-stuff.

What you need to do is:

$ git stash

Will put your current diff away. If you run the git status command at this point you will see there is no diff.

$ git co feature/painful-stuff

Switch to your target branch and do what you have to do

$ git co feature/awesome-stuff

Go back to your current branch.

$ git stash pop

Retrieve the diff you just put away and reapply it on top of your current branch.

Any questions ? Ping us at @KNPLabs 

IMG_0626-ANIMATION (1)