Remove Gibberish Messages in Git
I am a perfectionist.
I want to do everything perfectly, including writing commit messages in Git. Whenever I need to work on new features or fix bugs, I will create a new Git branch.
But, I can't avoid writing crapy and meaningless commit messages, especially when change a piece of code many times just because need to trigger the CI/CD pipelines.
Here is an example.
I was working on a new feature of my project.
I will create a new branch called feat/shinning.
After working on the code, here are the commit logs when I type git log
:
commit c62397c29f5a299c814d7a138092fc345448dc05 (HEAD -> installer, origin/installer)
Author: swardana
Date: Wed Oct 11 21:22:14 2023 +0700
change styles
commit 4cbf76038d2d6685dacf0f253341d057ce348f50
Author: swardana
Date: Wed Oct 11 21:11:04 2023 +0700
fix typos
commit 8a5dcd63364263dfda0f2ec38a609c3de53c3c08
Author: swardana
Date: Thu Oct 5 21:04:05 2023 +0700
fix once again
commit 3abc24267d4130a9e9b58c051f406249054a906f (origin/main, main)
Author: swardana
Date: Tue Oct 3 22:12:13 2023 +0700
wrong class
Well, now you know how messy and meaningless my commit messages are. I could still merge the feat/shinning into the main, but I hate to see that on my logs history.
My goal is to combine all the commits into only one meaningful commit message.
There are multiple ways to achieve it. But, what works for me is this.
Step 1: Working with temporary branch
Rather than create a proper branch, I will create a temporary branch and I could put many crapy or silly commit messages there.
$ git checkout -b tmp/shinning
Step 2: Create proper branch
When done with the changes and ready to merge it,
I will create a new branch from the latest master,
commit in the main branch where the temporary branch
is initiated.
$ git checkout main
$ git checkout -b feat/shinning ecddf76
Step 3: Merge the temporary branch to the real branch with --squash option
$ git merge --squash tmp/shinning
$ git commit # without -m
When enter the git commit
, without -m,
an editor should be popup with all the commit logs, and files changed from
the tmp/shinning.
I will delete everything and write proper commit message.
Step 4: Merge the real branch to main
This is the last and final step, merge the real branch to the main
$ git checkout main
$ git merge feat/shinning
Finally, it's done. With this, I could avoid any crapy and messy commit messages in my logs history.
This is inspired from the Stack Overflow answer which refer to this Github Wiki.