Using git rebase to squash commits
1 min read

Using git rebase to squash commits

Keep your repository history clean, again.

On the same note of yesterday's article, today I worked on a very small change, but I had to comment some code to be able to test locally, without the need to connect to the real backend.

I made the changes and committed, and then I realized that the code was still commented. So I uncommented that code and committed again.

Now the history of the repository is not clean, there is a uncomment code for local dev commit that I did not like.

The solution is to squash the latest commit into the previous one:

git rebase -i HEAD~2

which opens an editor

pick 127db81 Add app version to login screen
pick b9e4be5 Uncomment code for local dev

# Rebase b3fe2b2..b9e4be5 onto b3fe2b2 (2 commands)
#
# Commands:
# p, pick  = use commit
# r, reword  = use commit, but edit the commit message
# e, edit  = use commit, but stop for amending
# s, squash  = use commit, but meld into previous commit
# f, fixup  = like "squash", but discard this commit's log message
# x, exec  = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop  = remove commit
# l, label  = label current HEAD with a name
# t, reset  = reset HEAD to a label
# m, merge [-C  | -c ]  [# ]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c  to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
Terminal

I wanted to squash b9e4be5 into 127db81, but I did not care about the commit message, so I changed the first lines to

pick 127db81 Add app version to login screen
f 9cd817a Uncomment code for local dev

where f stands for fixup which discard the commit's log message. When I closed the editor, I was greeted by

Successfully rebased and updated refs/heads/feature/add-version-login.

and I was ready to push to remote.

Wanna stay up-to-date with Flutter and Dart?

Subscribe to get a weekly email with the best articles about Flutter and Dart.

    We won't send you spam. Unsubscribe at any time.