21

I'm trying to write a .gitignore file. I'd like to get a preview of everything that would be added if I were to run "git add -u". Is that possible? Or, if I ran "git add -u", and some stuff got added that I don't want, can I undo the entire thing? Then I'll tweak my .gitignore some more and repeat.

Thanks

user291701
  • 38,411
  • 72
  • 187
  • 285
  • to be honest right now I'm to stupid to understand what 'git add --help' is saying about '-u'. What differences would we (eventually) see in 'git status' before and after applying 'git add -u' ? And what if we used 'git add' instead'? – grenix May 18 '21 at 11:07
  • Could this be helpful for your use case (How do I undo 'git add' before commit?) ? https://stackoverflow.com/q/348170/2623045 – grenix May 18 '21 at 11:16

3 Answers3

28

I think you're looking for:

git add -n

.. which is the --dry-run short switch :)

Edit: from the root of the repository, try:

git add -n .

If you were to mistakenly add files that were supposed to be ignored, and you didn't catch it in the dry run, you'd just use:

git reset HEAD --

.. which would essentially unstage the files.

Nic
  • 13,287
  • 7
  • 40
  • 42
1

Note that the same preview won't work with an interractive add.

Git 2.32 (Q2 2021) is now clearer: "git add -i --dry-run"(man) does not dry-run, which was surprising.
The combination of options has taught to error out.

See commit a1989cf (05 May 2021) by Øystein Walle (Osse).
(Merged by Junio C Hamano -- gitster -- in commit 47fa106, 14 May 2021)

add: die if both --dry-run and --interactive are given

Signed-off-by: Øystein Walle

The interactive machinery does not obey --dry-run.
Die appropriately if both flags are passed.


And: With Git 2.34 (Q4 2021), stop "git add --dry-run"(man) from creating new blob and tree objects.

See commit e578d03 (12 Oct 2021) by René Scharfe (rscharfe).
(Merged by Junio C Hamano -- gitster -- in commit 6ffb5fc, 25 Oct 2021)

add: don't write objects with --dry-run

Reported-by: git.mexon@spamgourmet.com
Signed-off-by: René Scharfe

When the option --dry-run/-n is given, "git add"(man) doesn't change the index, but still writes out new object files.
Only hash the latter without writing instead to make the run as dry as possible.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

I found that

git add -n .

also is showing modified files while

git ls-files --others --exclude-standard

does not.

For now for me the second one is the prefered way previewing/testing .gitignore.

Also I found some comments in posts discouraging to use 'add .' at all and prefer 'add -u' .

I also tried

git add -u -n

but from my experience this is showing modified files only.

https://stackoverflow.com/a/10888722/2623045

BTW: git submodules seem to me to be an extra use case in this context (don't know if additional switches to 'git ls-files' can reflect that)

grenix
  • 623
  • 7
  • 15