niceseb niceseb - 4 months ago 20
Git Question

How to revert to previous commit and remove later commits in Git

Wanted to revert to previous commit by

$ git reset --hard commit_sha


Which seems to revert successfully with HEAD is now at that previous commit but within PyCharm log I still see the later commit in branch history.

So my question is how to remove that later commit ? i.e. image here, I want to remove commit
7e260f7


Do I right click on
7e260f7
or the commit named TEMP CHANGE GROUP and select 'Reset Current Branch to Here'?

Because the
origin/master
7e260f7
is still in BitBucket?

$ git reset --hard 7e260f7


enter image description here

enter image description here

Answer

Your branch master on remote origin is still pointing to the commit you're trying to remove. You need to force update the remote branch using git push origin master --force.

There is also a difference between reset and revert which is worth knowing.

When you use git reset you move the branch pointer to the commit you specify. You essentially move backwards through the history. Docs: https://git-scm.com/docs/git-reset

When you use git revert you reverse the changes that the commit you specify made and create another commit containing those reversed changes. In this instance you will create an additional commit with the reversed change set. Docs: https://git-scm.com/docs/git-revert

I hope this helps.