Skip to content

TIL: Git Tips #2 - Remove gibberish commit messages with merge --squash

My website is built with a static site generator engine. I put all the codes and article contents in git.

Each time I want to write a new post, I will create a new branch. And when it’s done, I will merge it into the master branch.

But, Often times I always have meaningless commit messages. Compared to working on projects, writing a post is prone to silly mistakes. And it’s affecting my commit messages.

Want to know how silly my commit messages are? Consider something like this:

  • Fix typo

  • Forgot to add a dot (.)

  • Another fix

  • p

  • draft

Well, I only care about the final result of my content. And I want to maintain my commit messages as clean as possible. So, I don’t want all those meaningless commit messages recorded in my master branch.

I found a great tip, which is using the git merge --squash command.

From Git merge

--squash

--no-squash

Produce the working tree and index state as if a real merge happened (except for the merge information), but do not actually make a commit or move the HEAD, nor record $GIT_DIR/MERGE_HEAD to cause the next git commit command to create a merge commit. This allows you to create a single commit on top of the current branch whose effect is the same as merging another branch (or more in case of an octopus).

But, using git merge --squash will not show the merged branch as actually merged. So, I do some tweaks to the process.

This is what works for me:

  1. Create a temporary branch (the branch name could be tmp/ctn/post-a) and create my post on this branch.

  2. I’m free to put many silly commit messages on the temporary branch but when the content is done and ready, the next step is:

  3. Create a new branch from the latest master, commit in the master branch where the temporary branch initiated (the branch name could be content/post-a).

  4. Merge the temporary branch (tmp/ctn/post-a) into the above new branch (content/post-a) using git merge --squash.

  5. Merge the newly created branch (content/post-a) into the master.

Now, the content branch will contain only one commit and the merge will be represented in a short and tidy illustration.

References:

If you have any comments or corrections please feel free to email them to me. Also, if you found any of the content on this website useful consider buy me a coffee ;)