How to amend commits using SourceTree/CLI

Amend commits

Git is an excellent tool to safely develop applications with total control of our source code. It can be used to track changes, collaborate with others and save your work in remote in servers that GitHub or Bitbucket offer us. But all people have bad days and mistakes can occur at any moment. Luckily, Git implements some mechanisms to fix all those commits that you pushed with a wrong identity, really bad messages or you just want to remove them forever.

If your commit is local and you didn’t push it

You can git commit --amend to change the message and the author of the commit or just go to Commit options… on the right bottom side and click Amend latest commit.

Amend commit

If your commit is already pushed

You can still use the same commands, but you will need to force push the commit using git push --force example-branch. For SourceTree, you can follow exactly the same steps, but you have to take into account that you still need force push this. SourceTree is very cautious with this as the force option is by default deactivated. If you want to enable it, you need to go to options/preferences and look for Enable Force Push:

Enable Force Push in SourceTree

After that, when you click in the Push button, you will see the Force Push option in the bottom of the window:

Force Push option

Please, use --force carefully as this will rewrite your remote history with the changes you have in your local environment, if you are the only working in the repository it’s OK, but if you are sharing it with more people, you can potentially delete some commits pushed by others.

If you have multiple commits already pushed and you want to keep the changes

In this case rebase is your friend. You can use git rebase -i HEAD~n where n is the number of commits you want to amend. Git will open your commit messages in your text editor:

Rebase commits

Below the commits, you will have a list of commands you can use. Add the command you want to use (edit in our case) and save the file. After that, you will need to amend every commit you are editing:

$ git commit --amend --author="John Doe <>"
$ git rebase --continue

Once you finish, don’t forget to git push --force example-branch.

For SourceTree, you can right click to the commit you want to rebase:

Rebase from SourceTree

A new window should open and there you can select what commits you want to amend:

Amend commits from SourceTree

If you have multiple commits already pushed and you don’t mind about the changes

To delete all the commits you don’t want to hold anymore you need to perform a hard reset. You still can do a soft reset and keep your the changes in your working tree if you want to commit them again with some changes, but I’d prefer the rebase way as it will give you more control committing the changes in the way that were originally pushed. So to reset the branch you can:

$ git reset --hard HEAD~n
$ git push --force example-branch

Using SourceTree, you can just click on to the commit you want to reset:

Reset in SourceTree

And then, you have to force push to update your remote repository.

Leave a Comment