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 idea sounds complex and intimidating, you're in for a pleasant surprise. Git fixup is straightforward, especially if you're already familiar with committing changes. Let's dive 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 utilize the git fixup command. Here's how:
Identify the hash of the commit where the problem was found.
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:
-i
: Interactive mode for the rebase.--autosquash
: A special flag that squashes fixup commits into their respective originals.main
: The branch from which your current branch originated.
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!
.
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:
if you work on the branch alone:
git push --force
💪if you share the branch with someone:
git push --force-with-lease
🤝
To summarize:
Copy the hash of the commit you want to fix.
Run
git commit --fixup=<commit hash>
.Execute
git rebase --autosquash -i main
.Check the open text editor and close it if everything looks good (
:q!
).Use
git push --force
to publish your changes.