How do we understand git checkout [dot]
Solution 1
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 files
There 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).
Solution 2
you 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.
Related videos on Youtube
Yves
BY DAT: hello world \n BY NIGHT: hello world \n FOR FUN: have fun \n
Updated on November 12, 2022Comments
-
Yves over 1 year
As my understanding,
git checkout
is about moving thehead
.For example,
git checkout <commit id>
is to move thehead
to the<commit id>
,git checkout <branch_name>
is to move thehead
to the<branch_name>
.However,
git checkout .
is to discard the unstaged changes. it doesn't seem that there is any business abouthead
.So I can not understand why
git
uses the same key wordcheckout
to do two totally no-relative things. Orgit checkout .
still works based onhead
?