What is the difference between git checkout [commit-name]
and
git checkout [commit-name] .
?
I saw that the first command changes to detached head state, where you can commit up from there and go on to create a new branch.
I am confused as to what the second command does. It stays on the master but reverts all the files back to the state at the specified commit. Why is it so? What can you do with this and in what situations is this used?
CodePudding user response:
The .
in git checkout [commit-name] .
is a standard UNIX wildcard character that refers to the current directory. Assuming you are using a UNIX system (or UNIX-based terminal like git bash) and that you are running the command from the root of your project, you are essentially saying to replace files from your current working state (with respect to your current folder location) with those of the specified commit.
My understanding is that the git checkout [commit-name]
command moves you to that commit. You are going back to a previous snapshot of your project or another branch. I am sure there are many quirks and features of it but that is beyond the scope of this answer. You seem to have enough understanding of this command.
So with a dot you are not moving anywhere but without a dot you are.
Anyway, back to just git checkout [commit-name] .
. A situation I can think where it is useful is if you want to replace your entire work with that commit. Maybe you made a horrible mistake or finished experimenting some code that you do not want to actually use. It might be a way to reset since last commit, if you know its commit number (you can find this with git log
).