Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"git checkout --" . vs git checkout [duplicate]

Tags:

git

I always used git checkout -- . to clear my working directory. I thought I read somewhere that the -- was required to avoid git thinking you are passing parameters (or something)

Now a colleague told me I could drop the --, and indeed, a quick test did exactly the same.

Is there are any difference between those two commands?

PS: Asking here because git checkout -- . vs git checkout . is kind of hard to google...

like image 939
Laoujin Avatar asked Dec 12 '16 13:12

Laoujin


People also ask

What is difference between git checkout and git checkout?

Difference between git checkout -- and git checkout Note that git checkout <name> is really meant for branches, but Git syntax is relaxed, and if Git can't find a branch, then it will look for a file.

What is git checkout --?

The git checkout command is used to switch between branches in a repository. Be careful with your staged files and commits when switching between branches. The git checkout command operates upon three different entities which are files, commits, and branches.

Does git checkout overwrite?

Checkout old commitsSince this has the potential to overwrite local changes, Git forces you to commit or stash any changes in the working directory that will be lost during the checkout operation. Unlike git reset , git checkout doesn't move any branches around.

What is git checkout Branchname?

If you aren't using a Git GUI to help visualize your remote branches, you will start by running the git branch command followed by the -r flag. This will pull up a list of your remote branches. Next, you will run the git checkout command followed by the name of the remote branch.


2 Answers

I seem to recall that the -- is a way to tell Git to treat what follows checkout as a file and not as a branch. Suppose that you had both a file and a branch called stuff. Then the following command would seem ambiguous:

git checkout stuff 

because it is not clear whether you are asking to checkout a file or a branch. By using -- you explicitly tell Git to checkout a file by that name/path. So in this case the following commands allow checking out a branch and a file called stuff:

git checkout stuff       # checkout the branch stuff git checkout -- stuff    # checkout the file stuff 

Note that git checkout <name> is really meant for branches, but Git syntax is relaxed, and if Git can't find a branch, then it will look for a file.

Closely related: Git change branch when file of same name is present

like image 109
Tim Biegeleisen Avatar answered Sep 19 '22 00:09

Tim Biegeleisen


-- as a standalone argument (i.e. not part of another argument) is used by many UNIX command line programs to indicate that anything that follows it is not an argument.

Why? Well, in this case, it's being used in case you have a path whose name starts with --, which shouldn't be interpreted as its own argument.

i.e. git checkout -- --mydirectory which, without the -- would throw an error.

like image 30
Powerlord Avatar answered Sep 23 '22 00:09

Powerlord