1.

How to revert the previous commit in git?

Answer»

git reset --hard HEAD~N- To undo last commit and changes.N stands for how far you would like to undo your changes. For undo last changes N=1 LET's talk about below use CASE where C is current HEAD while F is a state of files.

  •   (F)
  • A-B-C
  •    ↑
  •  master

If you like to undo commit C. You do this:

  • git reset --hard HEAD~1
  • The result is:
  • (F)
  • A-B
  •  ↑
  • master

Now B is the HEAD. Because you used --hard, your files are reset to their state at commit B.Suppose we would like to undo the commit but keep our changes before we do good commit. Let's start again with the above example, with C as our HEAD:

  •   (F)
  • A-B-C
  •    ↑
  •  master

We can EASILY achieve this by, leaving off the --hard:

  • git reset HEAD~1
  • In this case, the result will be below :
  •   (F)
  • A-B-C
  •  ↑
  • master

In both scenarios, the HEAD was just a pointer to the last commit. When we run a git reset HEAD~1, we ask Git to move the HEAD pointer one commit back. But (unless we use --hard) we leave our files as they were. So now git status will SHOW the changes you had checked into C. Your files will remain as it is but only GIT HEAD has moved one commit back.



Discussion

No Comment Found