Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rollback the two previous commits?

Tags:

git

github

reset

Consider this scenario:

  1. Developer A does a commit: #n
  2. Dev. B does commit #n+1
  3. Dev. A does commit #n+2
  4. and commit #n+3

and then discovers that in his commit #n+2 he introduced a defect.

How can dev. A rollback his last 2 commits and continue developing on commit #n+1?

Tried git reset --hard HEAD~2*, but it's coming back to dev A's commit #n.

like image 369
Marius Butuc Avatar asked Jan 21 '11 19:01

Marius Butuc


People also ask

How do I roll back the last two commits?

In order to undo the last commit and discard all changes in the working directory and index, execute the “git reset” command with the “–hard” option and specify the commit before HEAD (“HEAD~1”).

How do I revert a set of commits?

The easy way to revert a group of commits on shared repository (that people use and you want to preserve the history) is to use git revert in conjunction with git rev-list . The latter one will provide you with a list of commits, the former will do the revert itself.

How do I roll back to a previous commit?

To roll back to a previous commit w/o throwing away your work, use --soft. Unless you want it to remove all the changes up to that point, in that case use --hard instead of --soft, it would get you to the desired point in the tree WITHOUT trowing away all of the changes made in the commits.

How do I revert two commits in Git?

14 Use git revert: git revert A^..B where Ais hash of the first of the two commits to be reverted and Bis the hash of the second commit. This approach will work even if other commits have been made on the remote branch since the two commits were made.

How do I rollback in Git?

There are different ways to rollback in Git. Here, we will use the git revert command. Revert doesn't mean undoing the changes. I described the scenario above for ease of understanding that you rollback to the previous version.

What is the difference between the previous commit and latest commit?

Every commit made is the latest version of the changes made to the files in that repo. This means the previous commit contains some changes, and the latest commits contain some new changes but are not limited to a feature update, bug fixes, etc. Say you are working on a particular feature improvement of a website.


2 Answers

It should come back to the n+1 commit. You probably have a merge commit in there as well. You can also do a git reset --hard <sha1_of_where_you_want_to_be>

WARNING!! --hard means that any uncommitted changes you currently have will be thrown away permanently.

like image 94
Adam Dymitruk Avatar answered Nov 04 '22 18:11

Adam Dymitruk


I would suggest using instead something like this:

git reset --soft <commit_hash>

Unless you want it to remove all the changes up to that point, in that case use --hard instead of --soft, it would get you to the desired point in the tree WITHOUT trowing away all of the changes made in the commits.

While I was reading I tried with --HARD and for my misfortune, it removed all my changes up to that point. So, pay attention to whether you want them removed or not.

so, if you are unlucky as me try this! : git revert <commit_hash>

like image 40
Nick Cuevas Avatar answered Nov 04 '22 20:11

Nick Cuevas