Barka
Barka

Reputation: 8922

How to revert to older version in git

On github we are at commit 57d7cb274622f86e586c2595317264f23626e6c5 I want to revert to commit 5798ee7c56efd7e898d3115da6efe3d5e7cddb6c which is several commits before the latest one. Here is what I am doing:

$ git fetch origin
$ git reset --hard origin/master
Head is now at  57d7cb274
$ git status
nothing to commit (working directory clean)
$git revert 5798ee7c56efd7e898d3115da6efe3d5e7cddb6c
#On branch master
nothing to commit (working directory clean)
$ git status
#On branch master
nothing to commit (working directory clean)

But the resulting code is not really at the commit at 5798ee7c56efd7e898d3115da6efe3d5e7cddb6c What am I missing?

Upvotes: 2

Views: 7211

Answers (2)

michas
michas

Reputation: 26505

You are mixing up git revert and git reset.

With git reset you really throw away all those commits and you result at exactly the given commit.

But git revert will actually add another commit which does exactly the opposite as the original commit, and therefore reverts that commit.

Using git reset is fine for your local repository, but once you pushed your commits other people might already started using those commits for their own work. Therefore you want an explicit revert commit in this case.

Be aware that you need to revert each unneeded commit if you want to go the revert way.

Upvotes: 3

Brandon
Brandon

Reputation: 10028

According to the documentation for git at https://www.kernel.org/pub/software/scm/git/docs/git-revert.html, git revert creates commits which reverse the specified commit.

Therefore, the reason git status says there is nothing to commit after running git revert is that the changes you are looking for were already committed.

I think what you are trying to achieve is done, but your test to see if it worked is flawed.

Upvotes: 2

Related Questions