A
A
Alexander2019-04-19 12:19:51
git
Alexander, 2019-04-19 12:19:51

What does the "--" flag mean in many git operations?

I constantly notice that it is present in many basic operations, but I still do not understand the meaning.
For example:
git reset -- file1.txt dev/nginx_vhosts/file2.txt

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2019-04-19
@aleksand44

By a fairly common convention, all parameters after the double bar are considered positional (most often these are file names). gitadheres to this agreement.
For example, someone accidentally created a file named -n, and you want to display all the files in the folder with the commandcat

$ ls -l
total 8
-rw-rw-r-- 1 lynn lynn 6 Apr 19 12:34 file1
-rw-rw-r-- 1 lynn lynn 6 Apr 19 12:33 -n

if you just execute cat *, it will turn out strange:
 $ cat *
     1	file1

because bash expanded the asterisk and got the command can file1 -n, i.e. output a file file1with line numbers.
And this is how it will work out correctly:
$ cat -- *
file1
a
b
c

Because it will open in cat -- file1 -n, but by convention, --there can be no command keys after, only file names.
Specifically,git there is an even stricter convention, according to which the parameters after --can only be paths.
For example, if you have a file testand a branch test, then it will be like this:
$ git branch 
* master
  test

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

  modified:   test

no changes added to commit (use "git add" and/or "git commit -a")

$ git reset test
fatal: ambiguous argument 'test': both revision and filename
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

$ git reset -- test
Unstaged changes after reset:
M	test

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question