How to reset a commit

How to reset a commit

You could follow these steps to revert the incorrect commit(s) or to reset your remote branch back to correct HEAD/state.

1.checkout the remote branch to local repo.

git checkout your_branch_name

2.copy the commit hash

(i.e. id of the commit immediately before the wrong commit) from git log

git log -n5

should show something like this:

commit 7cd42475d6f95f5896b6f02e902efab0b70e8038 "Merge branch 'wrong-commit' into 'your_branch_name'"
commit f9a734f8f44b0b37ccea769b9a2fd774c0f0c012 "this is a wrong commit" 
commit 3779ab50e72908da92d2cfcd72256d7a09f446ba "this is the correct commit"

3.reset the branch to the commit hash copied in the previous step

git reset <commit-hash> (i.e. 3779ab50e72908da92d2cfcd72256d7a09f446ba)

4.run the git status to show all the changes that were part of the wrong commit.

5.simply run git reset --hard to revert all those changes.

6.force-push your local branch to remote and notice that your commit history is clean as it was before it got polluted.

git push -f origin your_branch_name

The end.

posted @ 2021-11-16 20:15  Aaronlamz  阅读(3)  评论(0编辑  收藏  举报