Remove inadvertently pushed commits - sauter-hq/git-guidelines GitHub Wiki

It could happens that someone did a mistakes and pushed commits to some branch. There are different solutions to this:

  1. Commiting reverse-commits, which exactly performs the inverse of the problematic commit.
  2. Resetting the branch to the commit preceding the mistakes and forcing the push.

While the first solution is an useful one to record in the history that we took the decision to revert some change, this is not necessarily what we wants when there was an inadvertent mistakes and we want to remove any trace of it, that's why the second solution exists.

Please note however that the second solution requires people who already pulled the mistakes to also reset or delete their local branch copy to be able to pull the modified changes

Resetting the branch to the commit preceding the mistakes

This can be useful, especially when making a final releases with Git Flow, in a moment where you want to finish a release from a tagged commit, but where someone mistakenly commited to the release branch.

To remove the commits you don't want to get in the master branch, do the following :

  • Right click on the last valid tag or commit (i.e. It's a tag in the case of release, as it's only possible to release a version which was given to the Quality Assurance, and those release are tagged according to LINK-TO-WORKFLOW)
  • In the contextual menu, choose Reset current branch to this commit, and in the window which appears select Reset hard (i.e. If you have uncommited changes that blocks you to perform the action stash them)
![Reset branch to specific commit](pictures/reset-to-tagged-commit.jpg)
![Reset hard](pictures/reset-to-tagged-commit-hard.jpg)
  • To push the changes online, you cannot use SourceTree App, as it doesn't allow forced pushes. Click on the Git Terminal button in the upper-right corner and type git push origin +master:master to overwrite the remote master branch with you local master.

    Force push the changes