Git checkout with dot
What difference between next git
commands:
git checkout branch
git checkout branch .
git checkout . #<-- used at the branch
Why when I checkout different branches into different folders with first one I missed some files.
But when I am using second command, everything is ok?
git checkout
(1) does very different things whether given path specifier or not.
- With branch specifier only (
git checkout branch
) it will switch current working directory to specified branch, keeping local changes if possible and failing otherwise. If you already are onbranch
, it will do nothing at all. It only modifies files in the working directory that differ betweenHEAD
andbranch
and fails if any of them has local modifications (indexed or not). - With path specifier it will overwrite the all matching files (all files match
.
) with specified content:- With path specifier only (
git checkout .
) it writes content from index. That is, it undoes unstaged local modification. To undo staged modifications, usegit reset
with path specifier. - With both branch and path specifiers (
git checkout branch .
) it writes content in specified revision. It will not modify whereHEAD
points, so ifbranch
is different fromHEAD
, there will be unstaged changes afterwards.
- With path specifier only (
Note, that the man page distinguishes additional cases for use of the -b/--branch option and -p/--patch option, but those are mostly straightforward extensions of the above cases.
The above explanation is fine but let me explain with examples.
git checkout
can be used with files folders and branches.
Let say there is index.html
file and dev
folder.
If I accidentally change index.html
and i want to undo that I will simply run git checkout index.html
. It will get my file back to its original form.
Now let say I made some changes inside dev folder and and want those changes back inside dev folder. If I will use git checkout dev
and if there is any dev
branch then it will checkout that dev branch rather than folder named dev
.
So rather I would run
git checkout -- dev
Now this bare double dash stands for current branch. So above command is asking from git is that please give me 'dev' named folder from current branch.
Lets talk about your use case.
git checkout branch
this command will simply checkout the branch named 'branch'
git checkout branch .
The second command will tell git that please checkout .
or current folder name from the branch named 'branch'
git checkout . #<-- used at the branch
as you are saying first you switched to branch named 'branch' using git checkout branch
then you are simply doing git checkout .
Now there is no branch named .
so it will simply pull down current folder name from current branch.