HelloWorld
HelloWorld

Reputation: 1863

Difference between `git checkout -- foo.txt` and `git checkout foo.txt`

Normally I use git checkout -- foo.txt to reset a specific file in my worktree. Today I accidentally used git checkout foo.txt (without --) and it seems it did the same.

$ git checkout -- foo.txt  # no output
$ git checkout foo.txt
Updated 1 path from the index

So I am wondering, what is the difference between the two? Or, is there any?

Upvotes: 1

Views: 280

Answers (1)

SwissCodeMen
SwissCodeMen

Reputation: 4895

Simple answer:

# checkout the branch foo.txt if this branch exist, otherwise the file
$ git checkout foo.txt

# checkout the file foo.txt
$ git checkout -- foo.txt

So if you need -- it is for files, without the two characters, it is meaning for branches. If Git can't find a branch, then it will look for a file and checkout this file.

Look at official Git doku about git checkout

Upvotes: 5

Related Questions