4

To delete a file one can:

git rm path/to/file

When I want to unstage a deleted file in git, I have to:

  • git reset -- path/to/file
  • git checkout -- path/to/file

I don't quite understand why the -- is necessary and how the command differs from:

  • git reset path/to/file
  • git checkout path/to/file

other that the latter wouldn't work.

What's the rationale behind --?

The fatal error states:

Use '--' to separate paths from revisions

yet I still don't quite understand the difference.

k0pernikus
  • 50,568
  • 57
  • 198
  • 317

2 Answers2

6

git command some-path and git command -- some-path are equivalent in all cases except when some-path could be interpreted as a commit reference. The most common case is a branch that has the same name as a file.

For example, imagine your repository has a file named master in its root. Then git checkout master would checkout the branch master. But git checkout -- master would check out the file master of the current HEAD and replace the local master file with the version of that revision.

poke
  • 339,995
  • 66
  • 523
  • 574
0

As the error states, it would not be possible to know if path/to/file is a file path or a tree-ish, so it is always parsed as a tree-ish, and arguments after -- are parsed as paths.

wRAR
  • 24,218
  • 4
  • 82
  • 96