Note: VSTS = Visual Studio Team Services.

Sometimes in git, we end up mistakenly writing an incorrect commit message (especially if working on multiple projects). Now, I know that there are lots of sources online that walk through how to fix incorrect commit messages, but personally having experienced this, it can take time to search around and put all the relevant steps together. So, I decided to create my own walk through guide on how I managed to correct an incorrect git commit message. Hoping this comes in use for anyone who may stumble across such a situaton:

  1. First checkout a temp branch –
    • git checkout -b temp
  2. On temp branch, reset –hard to a commit that you want to change it’s message. (To check the commit number login to either VSTS or Github and track commits under the code branch). Take the commit number to be 946992 for example –
    • git reset –hard 946992
    • You should now get a message similar to: HEAD is now at 94992 <’old commit m’>
  3. Use amend to change the message –
    • git commit –amend -m “<new_message>”
  4. This is an optional step. If the commit being edited is the latest commit then this step can be skipped. However, if the commit has had other commits after it then cherry-pick all the proceeding commits ahead of 946992 from master to temp and commit them. Use amend if you want to change their message as well:
    • git cherry-pick<9143a9>
    • git commit –amend -m <”new m”>
    • git cherry-pick <last commit number>
    • git commit –amend -m <”new m”>
  5. Now, force push the temp branch to remote –
    • git push –force origin temp:master
    • At this point if you get the following error: ![remote rejected] temp -> master ( … You need the Git ‘Force Push’ … ) this means that either VSTS or Github have locked Force command permissions for you. If this is the case, you need to go into either VST or Github and assign yourself permissions to “Force push (rewrite history, delete branches and tags)”. You may have to ask somebody else ‘Directory /owner’ to assign the rights to you, then try again.
  6. Once done, check out of your temp branch –
    • git checkout temp
  7. Delete Master branch locally (make sure it is a capital D)
    • git branch -D master
  8. Then git fetch origin master
  9. Now, finally git checkout master

This will move you back into your master branch locally and you should be able to commence as normal from here.

%d bloggers like this: