I've seen many different approaches for discarding changes / reverting to a previous commit using Git. I usually can figure out which works for my situation, but in the process I've gotten quite confused by the different approaches. Most recently I was trying to undo some file renames and no matter how hard I tried to git checkout
the old versions of the files I still couldn't get my old files back.
I'm looking for clarification on which approach to use and why. Here is my understanding of a few approaches. I realize that the answer may be very contextual, but I'd like to try and sort out which contexts require which approaches.
1)git checkout -- .
2)git stash save --keep-index
followed by git stash drop
3)git reset --hard
This is my current understanding of my options. Are there any changes you would make to my explanations? I'm also unsure when I would use a git revert
in lieu of the above commands.
Source posts:
git stash -u
is the preferred way. Don't drop. They won't get pushed. In case you did zap something you discovered was important, you can get them back.
Your other choices are destructive.
Revert is adding a new commit to the history that applies the opposite of a patch that a commit has introduced. Your choices here are for what you want to do with changes in your worktree that are not yet part of history. Revert is for committed changes that are part of history.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With