As my understanding, git checkout is about moving the head.
For example, git checkout <commit id> is to move the head to the <commit id>, git checkout <branch_name> is to move the head to the <branch_name>.
However, git checkout . is to discard the unstaged changes. it doesn't seem that there is any business about head.
So I can not understand why git uses the same key word checkout to do two totally no-relative things. Or git checkout . still works based on head?
2 Answers
As you've noticed, the checkout command is overloaded to mean two different things. I think git help checkout makes that fairly clear though:
git-checkout - Switch branches or restore working tree filesThere are several forms of the command, the one you're asking about is:
git checkout [<tree-ish>] [--] <pathspec>...In your case the <tree-ish> argument is omitted, and the -- argument to separate options and filenames is ommitted, and <pathspec> is . (i.e. the current directory). That form of the command is documented as:
Overwrite paths in the working tree by replacing with the contents in the index or in the
<tree-ish>(most often a commit).
So since you didn't specify a <tree-ish> argument, the contents of the files in . are replaced with the contents from the index. That means discarding any changes in . that have not been added to the index yet.
You can think of it as "checkout these files from the repository", which can either mean from a commit, or from the index (which might contain changes that have been staged but not yet committed).
2you can use git checkout to change your branch name & as well as to reset a file (to remote) or to a file in particular branch.
working will depend on the argument after checkout. if its a file name it reset the file as in Current Index. this is also useful to get the file from other branches.
if it is a branch name it will change the current working branch.
0