Friday, August 24, 2018

Git: Editing the Pushed Remote Commit Message and Recent Local Commit

Editing the Commit Message Which is already Pushed to Remote

Sometimes you might need to change the git commit messages which you have already pushed to the remote. It is unfortunate that we face such situation, but fortunately git provides a mechanism to do this. Following steps explain to change the commit message.
  1. git rebase -i <commit hash you want to change>^

  2. This will open your default editor (usually vi) with a list of commits and actions for each one. By default, the action is pick. You will be able to edit all the commit messages of the commit whose hash you have provited and those above it.

  3. For any commit you wish to change the message, change pick to reword.

  4. Save and quit (in vi: :wq).

  5. For each such commit, you'll get an editor to edit the commit message. Change it as you see fit, save and quit.

  6. Once you're done editing all the commit messages, you'll return to the command prompt, and have a new tree with the updated messages.

  7. You can now upload them to github by using git push origin --force.

  8. If you just need to fix your last commit, you can replace steps 1-4 with git commit --amend.

Editing Your Most Recent Local Commit

Editing Message

If there is a case when you just made a commit with a wrong message, then you can edit the message using following command
git commit --amend -m "New commit message"

Adding a Missing File

It can also be a case that you might have missed to add a file to your recent commit. In that case, you can still add the missing file to the same commit without deleting the commit and committing again. It can be done using following command.
git add file.file
git commit --amend --no-edit
--no-edit means that the commit message does not change.






No comments:

Post a Comment