herve-guerin
herve-guerin

Reputation: 3101

How to undo last local commits in Git and go back to the remote state?

I merged a remote branch into my local repo and it created 26 local commits.

Now, I want to revert this merge, but it think that it is quite boring and error-sensitive to revert the commits 1 by 1 or seek for the last commit of the remote version to revert to it.

I went through the post How to undo last commit(s) in Git? which is very interesting with its answers, but I didn't found any simple way to do my local revert.

Can anybody help ?

Upvotes: 0

Views: 961

Answers (2)

herve-guerin
herve-guerin

Reputation: 3101

I tried one or two things and this one is a little bit dirty, but it is simple and seems to work :

  • checkout to another branch (create one if none) : git checkout -b tempo
  • remove the bad local branch : git branch -D dirty_one
  • recreate the local branch synchronized with remote one : git checkout -b dirty_one origin/dirty_one

and that's all.

The dirty thing I think is that the bad local commits are still in the local repo, but the branch don't know them so they may remain hidden.

If I redo my merge (which I have done in fact), the merge just work well, but I don't know exactly how (that the dirty part ...)

Upvotes: 0

harshitpthk
harshitpthk

Reputation: 4136

Use Git Log & Git Reset

git log

this will give you the commit id with message through which you can identify your commit id.

use the commit id to then reset back.

git reset <commit-id>

if you permanently want to go back to that commit. you can use the following.

git reset --hard <commit-id>

Upvotes: 2

Related Questions