Article thumbnail

🔰 Renaming git commits

2m
New
version control
commits history
commits rename

Intro

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.

Loading

Interactive rename in action

Loading

Now, let's elaborate on what happened.

  1. We identified multiple commits with poor names - 3x "wip"
  2. Executed the interactive rebase command - "git rebase -i HEAD~3" to rename the last 3 commits
  3. Switched the operation from "pick" to "reword" for each
  4. The interactive mode prompted us for new names for each commit
  5. 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! ψ(`∇´)ψ

I create content regularly!

I hope you found my post interesting. If so, maybe you'll take a peek at my LinkedIn, where I publish posts daily.

Comments

Add your honest opinion about this article and help us improve the content.

created: 11-12-2023
updated: 11-12-2023