Reset Git Commit

The guide shows how to reset git branch head to previous commit with reflection on origin

Thursday, October 31, 2019

Find Your Target Commit

One of ways to reset a commit is to find the target commit Id. Target commit Id can be found e.g. by executing git log command. Output will have format of

...
commit <commit Id>
Author: <author>
Date: <date>

    <commit message>
...
There are multiple options available such as to search for commits in the given date range, etc. See closing notes links for more details.

Reset Local Repository

Once the target commit Id has been found, execute following command

git reset --hard <commit Id>
--hard option is given to reset also working tree changes. By now your local repository head is reset to the exact state as it was at the time of your target commit.

Reflect Local Reset On Remote

To reflect your local changes on remote branch, execute following command

git push --force origin <remote branch name>
--force option is given to allow update remote ref even when it is not an ancestor of local ref. Both your local and remote branch states are now set to the state of the given past target commit.

Closing Notes

Additional documentation can be found at
https://www.git-scm.com/docs/git-log
https://www.git-scm.com/docs/git-reset
https://www.git-scm.com/docs/git-push