Amend commit message in SourceTree

If you’re using Atlassian SourceTree with a git repository and you do a local commit but then realise the message was incorrect, you can amend it before you push it to remote. To set this up, you can create a “Custom Action” in SourceTree:

  1. Tools -> Options -> Custom Actions
  2. Click Add
  3. Set Menu caption, e.g. “Amend commit message”
  4. Select “Open in a separate window” and unselect “Run command silently”
  5. Set Script to run to “git.exe” including path
  6. Set Parameters to “commit –amend”

Now, whenever you want to correct the message on your latest commit, you right-click on the commit, and select “Custom Actions” -> “Amend commit message“. A terminal window will open with a vi editor open with the current commit message at the top of the file (there will also be some instructions with # at the start of each line; you can leave them alone).

If you know vi, you’ll know what to do. Here’s a quick guide:

  • To start editing, press “i” and then edit the message (on the first line of the file).
  • To delete everything on the line, press ESC, followed by “ddO” (this will delete the line and then return you to edit mode to type the new message)
  • To save and quit, press ESC, followed by “:wq
  • To quit without making any changes, press ESC, followed by “:q!


Comments

  1. Rui Pinheiro
    15 May 2020 - 7:31 pm

    Really? I love SourceTree, but there is a reason to use SourceTree: so that we continue using a GUI, not a command line and not even using “vi” to edit text. Also, using ammend only to change text is part of the need: you sometimes also need to add additional forgotten files. The solution I propose (and that I think to be way better than creating some Custom Action in SourceTree) is just use another application for the ammendments. I use git-gui, that has a fantastic option for ammendments (menu Commit -> Ammend Last Commit). Then you have a very intuitive GUI that allows you to not only properly edit the commit message in a WYSIWYG way instead of some vi ninja wizard magic voodoo, but also to move files from unstaged staged zones. Then just click “Commit” button and voilà, your last commit message and files are updated. Of course you can only do this if you still did no push 🙂

Leave a Reply

Your email address will not be published / Required fields are marked *