Sobes.tech
Middle

How can you rename a commit in a version control system?

sobes.tech AI

Answer from AI

For the last commit:

git commit --amend -m "<new_commit_message>"

This will change the message of the last commit.

For an older commit:

  1. Find the commit hash you want to rename:
    git log
    
  2. Start an interactive rebase, specifying the commit before the one you want to change:
    git rebase -i <commit_hash_before_target>
    
  3. In the opened editor, change the word pick to reword (or r) for the desired commit. Save and close the file.
  4. Git will open a new editor to change the message of the selected commit. Change the message, save, and close.
  5. If the commit has already been pushed to a remote repository, a force push is required (use with caution):
    git push --force