I was revising the tutorials I took about git and something is bothering me. On the video about 'Undoing things' it says that there are three ways to undo changes, namely - checkout, revert and reset - with 'reset' being the most dangerous one because once a git repo has been reseted to a previous commit with the --hard flag there is no way of getting back to any commit(s) made after the one the programmer is resetting to. But when I execute the 'git reflog' command I was able to see all the hash IDs that were generated from the commits I was making ever since the workspace was being tracked by git(as the tutorial is claiming these were not supposed to show up as they are permanently gone) and what's even more perplexing me is why does git allow me to checkout any of the previous commit(s) I have made?
Asked
Active
Viewed 5,612 times
1
-
2Possible duplicate of [What is the meaning of git reset --hard origin/master?](https://stackoverflow.com/questions/15432052/what-is-the-meaning-of-git-reset-hard-origin-master) – Makoto Jan 15 '18 at 18:23
-
1I think the docs here are pretty clear about it https://git-man-page-generator.lokaltog.net/ – Jan 15 '18 at 18:26
-
Once a commit is in git, you can get it back (with caveats). Even if it is totally unreferenced and unavailable, it won't actually be deleted for many days (this is a configurable timeout with a very generous default). `reset --hard` is dangerous because it may discard changes that have not been committed, not because it makes committed changes hard to access. – William Pursell Jan 15 '18 at 18:28
-
"what's even more perplexing me is why does git allow me to checkout any of the previous commit(s) I have made? " Why is that perplexing? The whole point of git is to allow exactly that! (Well, one of the points, anyway.) – William Pursell Jan 15 '18 at 18:29
-
@William...I said that assuming what the tutorial told me is true. – Jan 15 '18 at 19:49
-
Git for ages 4 and up: https://youtu.be/1ffBJ4sVUb4 – evolutionxbox Jan 15 '18 at 21:27
2 Answers
1
The tutorial is simply wrong, or perhaps incomplete (or you haven't gotten to the point where it tells you why they lied earlier :-) ). The reflogs exist for several reasons, specifically including being able to undo some of the effects of git reset
.

torek
- 448,244
- 59
- 642
- 775
0
git reset --hard <hash>
resets your working local directory and remote to the exact state of that hash commit in your git history. It wipes all local changes and anything else so you have exact replica of the origin branch.

Mike Tung
- 4,735
- 1
- 17
- 24
-
Thank's Mike. So what is it's difference from reverting and checkout? (P.S. - I know you switch branches with checkout but you can also checkout commits - I want you to consider that I already know this when you answer) – Jan 15 '18 at 19:53
-
revert undoes a commit with a new commit, while hard reset alters the commit history. – Mike Tung Jan 15 '18 at 19:55