Baybook
Baybook

Reputation: 137

Steps to doing a git revert

I need to go back to a good configuration and that means going back past several commits and 1 merge.

Can someone confirm the correct steps? Or let me know if this is the correct process.

git checkout master
git revert commita, commitb, (merge happened between b&c), commit c
git push 

??

Thanks

Upvotes: 0

Views: 4624

Answers (3)

Michael Aquilina
Michael Aquilina

Reputation: 5540

Let's say for the sake of simplicity that the commit you wish to go to is "f8dec3". Then the simplest thing you would need to do is the following:

git branch savepoint
git reset --hard f8dec3

This will reset the state of the current branch you're on to that specific revision. You will lose all commits before that point.

You can determine the id of the commit you wish to reset to using the git log command or preferably using a tool like tig which makes reasoning about git changes much easier through a visual graph.

If you notice something is wrong then you can revert your mistake:

git reset --hard savepoint

Fixing Mistakes

There is a nice way of reverting hard resets in case you make a mistake using the git reflog. Just type git reflog and take note of the commit id you were previously at before the hard reset. This is especially useful if you forget to save the state of your branch before resetting (this has actually saved me countless times and prevented me from being so scared about screwing around with git commands).

If your remote tracking branch has commits which you have reverted from the hard reset then you will need to do a force push using git push -f. However think long and hard about whether you are doing the correct thing before doing this.

Upvotes: 0

Nick Volynkin
Nick Volynkin

Reputation: 15139

There are two crucial questions:

  1. Did you push to a remote repo? Are there other people downstream who already have the corrupted version?
  2. Are those last commits or some commits back in history?

If you want a backup. ( Having it is usually a good idea. )

git branch backup_description

Just the last commits, including the merge commit, not pushed yet or no other users downstream.

git reset --hard <sha-where-it-all-worked-well>
git push --force origin/master

There are users downstream, or commits are back in history.

git revert commita
git revert -m 1 mergecommit
git push

This can help too:

  1. https://github.com/git/git/blob/master/Documentation/howto/revert-a-faulty-merge.txt
  2. The illustration. enter image description here

Upvotes: 5

Naveen Dennis
Naveen Dennis

Reputation: 1223

If you just want to move back past the last few commit that you have made then you just have to git reset --hard <commit-hash-before-commit-a> (If you are doing this make sure that those changes have not been pushed to the server)

But if your commits a, b are present somewhere down below the current head then use git revert <commit-hash-a> <commit-hash-b> But when you want to revert a merge commit the changes are made by merging two commits (lets say c and d). If you want to revert the changes made by commit c on the branch then you have to use git revert -m 1 <merge-commit-hash>

Upvotes: 0

Related Questions