If I understand difference options for git add correctly, git add -u only adds modified and deleted files to the staging area, whereas git add . adds not only modified and deleted files but also untracked files. So I was wondering why we would ever wanna use git add -u when there's git add . or git add -A
CodePudding user response:
I use it a lot:
If
git status
tells me that there are various modified files that are not staged for commit, and I want to add exactly those files,git add -u
adds exactly those files without me having to think about it.If
git status
tells me that there are various modified files, and/or other files, where I don't want to en-masse add everything thatgit status
mentioned, I rungit diff
andgit diff --staged
1 if/as desired,git add
individually, maybe an occasionalgit add -p
, etc.If
git status
tells me that there are untracked files that I do want to add, I may usegit add .
(I use this rarely / sparingly so as to accidentally avoid adding files that should remain untracked; ifgit status
shows untracked files that I don't want, I add them to an appropriate.gitignore
first, but I still tend not to rungit add .
.)
Note that while git add -A
will "add" a removal (and git add file-i-removed
will do the same thing), my brain balks at this construct: I use git rm
for it instead.
1My fingers are wired to type this as git diff --cached
, actually. These do exactly the same thing.
CodePudding user response:
Short form: it depends on your build/test cycle. If you generate a lot of detritus or other local-only work and it's easier to just step around it than manage it explicitly, git add -u
makes it drop-dead easy to add only the stuff git already knows you care about.
Maybe you're generating something like UI-trace datasets to feed unit tests and have a lot of candidates, you explicitly add the ones worth tracking and use git add -u
to apply any autogenerated updates (like say to keep the datasets current with changes to your trace driver).
Sure, you could make a "don't track these" directory and keep the not-ready-for-prime-time stuff in there, but that's more tedious than just using git add -u
.