Enhance commit history using Git's rename feature with this concise and insightful guide. Optimize your workflow and maintain clarity in version control.
The Context and Clean Git History
Committing without conventions and using unappealing names can pose risks. A consistent convention facilitates faster rebase, conflict resolution, squashing, and overall version control. Occasional commit renaming becomes necessary when you identify ugly commits.
Before delving into technical details, assume we have 3 commits with unclear names that require rename.
Under Organized Git commits with Commitlint article, you can read more about Commitlint and commits conventions.
Interactive Rebase and Reword
Execute in the terminal: git rebase -i HEAD~3. This opens an interactive tab to specify the commits for renaming. Next, follow the steps outlined in the GIF below.
Interactive rename in action
Now, let's elaborate on what happened.
- We identified multiple commits with poor names - 3x "wip"
- Executed the interactive rebase command - "git rebase -i HEAD~3" to rename the last 3 commits
- Switched the operation from "pick" to "reword" for each
- The interactive mode prompted us for new names for each commit
- Finally, used "git push --force" to overwrite and push the updated commits to origin
Keep in mind that when employing git push --force, you replace existing commits with new ones, resulting in changed hashes.
Conclusions and thoughts
Now you know how to rename n commits using the interactive mode. For Git beginners with crucial work on branch, consider cloning the branch and experimenting. Every developer has faced Git mishaps in their career, so you might not be an exception to the rule. Good luck! ψ(｀∇´)ψ