When things go wrong, for example, caused by a bad commit, we need to revert back to an earlier known good commit. To identify the commit to revert to, read the commit graph carefully, pay close attention to the dots on the lines. The commit to revert to should be a closed dot, that is, not an open ended dot behind the bad commit.
TortoiseGit Steps
- Stash your changes if you have uncommitted changes
- After identifying the commit to revert to in the graph in BitBucket
- Switch to the staging or master branch in local repo
- Select Show Log and look for the good commit.
- Right click on the commit, select Reset "master" to this, option Hard. Click OK
- Now Git Push, option Force: unknown changes, the branch to BitBucket
- Deploy to staging or production servers
IMPORTANT
User who resets the master or staging or any branch which resides in remote need to inform other repository users so that they can reset their own local repository accordingly. They can follow these simple steps.
Else, the bad commits which could have been pulled by other users could get re-committed.
Repeat for other branches.
If committed to master instead of a new branch
If a mistake is made, instead of committing to a new branch, the changes were committed to local master:
- In master branch, create the new branch, now it (new branch) has all the committed changes
- Switch to master and reset it to remote, now master is reverted
remote: Permission denied to force push branch master.
You need to provide permission to rewrite branch history in
BitBucket > Go to your repo > Repository settings > Branch permissions > click edit icon of the branch > check the box Allow rewriting branch history