0

In Git, I can use the revert command to revert back to a previous commit. But this assumes that I have already made a commit to the current code that I want to undo. But suppose I do an initial commit and then want to make changes to try something out. After trying things out, I decide that I want to undo the changes I just made and go back to the state of my last commit. The only way I see that I can do this is to first commit the changes I just made (even though I really don't need them) and then revert back to the previous commit. Is there a way to just go back to the previous commit without committing the current changes? I also know that there is a stash but the documentation seems to imply that you use this when you want to switch to a different branch. But I really don't want to switch to a different branch. Maybe the solution is just to create a new branch to test out my modified code? And if I don't like it, just switch back to the previous branch?

Johann
  • 27,536
  • 39
  • 165
  • 279
  • You need to learn about `git reset`. In particular you want to say `git reset --hard`. But you should learn about it first. – matt May 04 '15 at 05:03
  • possible duplicate of [How do you discard unstaged changes in git?](http://stackoverflow.com/questions/52704/how-do-you-discard-unstaged-changes-in-git) – Oliver Charlesworth May 04 '15 at 05:03
  • possible duplicate of [Can you explain what "git reset" does in plain english?](http://stackoverflow.com/questions/2530060/can-you-explain-what-git-reset-does-in-plain-english) – matt May 04 '15 at 05:04
  • So I can do either a reset or discard unstaged changes. Which is the preferred approach? – Johann May 04 '15 at 05:06

1 Answers1

1

Go to last commit state by git checkout .

This will discard your local change. Depends on your intention, this may or may not be what you indended

user2829759
  • 3,372
  • 2
  • 29
  • 53