InterviewSolution
| 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.
If you like to undo commit C. You do this:
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:
We can EASILY achieve this by, leaving off the --hard:
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. |
|