Skip to content

🚀 One Git Trick for Perfect Commits 🛠️

Posted on:August 15, 2023 at 03:04 PM

Are you tired of seeing vague and meaningless commit messages like:

fix: close pull request discussion
fix: fix review discussion

If you’re nodding your head in agreement, it might be time to introduce the “git fixup” technique to your team’s workflow, or even just adopt it for yourself.

If the concept seems complicated and overwhelming, don’t worry – you’re about to be pleasantly surprised. Git fixup is actually quite simple, especially if you’re already comfortable with making commits. Let’s jump right in! 🏊‍♂️

Imagine you’ve made several commits on your working branch and have submitted a merge or pull request. However, during the code review, issues were identified—whether by your peers or during your own self-review. Instead of creating separate commits to address these problems, you can simply use the git fixup command. Here’s how:

  1. Identify the hash of the commit where the problem was found.
  2. Run the commandđź’ˇ:
git commit --fixup=<commit hash you want to fix>

This command will generate a new commit with the original commit’s subject, prefixed with fixup!. This additional commit might seem like a slight inconvenience, but even it is already far better than having vague commit messages cluttering your history.

But we’re not done yet. The next step is to incorporate the fixup commit into the original commit using the following command:

git rebase --autosquash -i main

Here’s what these parameters mean:

!!! This is a rebasing command, and you should be aware that this command will change your Git History. So, do it only if you know how it works.

After executing this command, a text editor (VIM by default) will appear. Don’t worry—there’s no need to make changes. Simply close it by typing :q!.

Image description

Congratulations! Your commit history is now clean and organized. You’re ready to share your changes with your team. Just use either of these commands to push your changes, depending on your circumstances:

To summarize:

  1. Copy the hash of the commit you want to fix.
  2. Run git commit --fixup=<commit hash>.
  3. Execute git rebase --autosquash -i main.
  4. Check the open text editor and close it if everything looks good (:q!).
  5. Use git push --force to publish your changes.