In class we are working our way through the Ruby Koans. This is partly to learn Ruby, and partly to get us used to making git commits and doing pull requests.
While exploring the section “about_regular_expressions” and fixing each test and doing some Gitx practice, I found myself the proud owner of 27 separate commits!
At this point Jason Noble, our instructor, stepped in and said something like
“Wow, that’s a lot of commits. You should git rebase -i
and
squash some of them.”
What is squash
, you ask? It is where you take a number of commits
and turn them into a single commit, combining the commit messages at the same
time.
In the following example, lines 3-7 are five commits I performed:
1 2 3 4 5 6 7 8 9 |
|
Issuing the following command: git rebase -i HEAD~4
make the default editor editor open up git-rebase-todo
with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
I used HEAD ~4
to tell rebase to count four commits from
HEAD
. If you are rebasing against a remote you would use
something like git rebase -i upstream/master
.
The instructions on how to use all these features are in the comments
below the list of commits. Change pick
to
squash
on the commits you want to combine, and save your changes.
WARNING: Do NOT change the first line! Bad things happen.
1 2 3 4 5 6 |
|
Upon closing the editor, you will be presented with the file
COMMIT_EDITMSG
containing all the messages from the squashed
commits:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
Edit the new single commit message, and remove any duplicated or extraneous text, and hit save and close the file. Git will update the files and adjust the commit. Your list of commits now looks like this:
1 2 3 4 5 |
|
Problem solved!
You can also use the same procedure to edit commit messages, only use the
option reword
instead of pick
.