I. Git tips
2. Squash commits
In case you're contributing to a project or just want to do some cleanup in your project commits' history, knowing how to squash is really important.
First step
First, you need to know how many commits you want to squash and where they're located in the commit history. In here, I'll show you the case where you want to squash the lastest commits of your history.
For example:
> git log
commit 04fa3a3d24e5dc35dc6ab518dac413e9398bc998
Author: Guillaume Gomez <guillaume1.gomez@gmail.com>
Date: Sun Jul 31 02:17:50 2016 +0200
Fixup for pk-config#3
commit 04fa3a3d24e78935dc622518dac413e9398bc768
Author: Guillaume Gomez <guillaume1.gomez@gmail.com>
Date: Sun Jul 31 02:16:59 2016 +0200
Fixup for pk-config#2
commit 04fa3a3d24e5dc35dc622518dac413e9398bc989
Author: Guillaume Gomez <guillaume1.gomez@gmail.com>
Date: Sun Jul 31 02:16:27 2016 +0200
Fixup for pk-config
commit 0cc88a7d344c22e6a61dc2b67be6c63bdc6cb73a
Author: Guillaume Gomez <guillaume1.gomez@gmail.com>
Date: Sun Jul 31 01:38:34 2016 +0200
Add pkg-config to help looking for libs
In here, the 3 last commits (the fixup ones) are too much. We don't want to keep them. So let's squash them into "Add pkg-config to help looking for libs".
git rebase
To remove them from the history, we'll use the git rebase
command:
git rebase -i HEAD~4
Let me explain what I did first:
-i
is the interactive option.HEAD~4
means (to put it simply) "I want to edit the last four commits starting from the HEAD (which is the last commit of the current branch)".
If you wonder "why 4?", it's because we need to squash all the commits into another one. So 3 we want to remove + 1 = 4
.
You should now see a screen like the following (if your default git editor is nano
, otherwise it's just the same):
Since we need to squash the 3 last ones, replace their "pick" by "squash" (or "s"):
Now save and close. git
will now open a last window which contains the commits' message. Add a "#" at the beginning of every line you don't want: