git add: new vs modified vs deleted

82 views Asked by At

I've been trying to get to grips with 'git add', particularly in relation to how it behaves for new vs modified vs deleted files.

I've looked at various guides and tutorials out on the web, but a lot of what's out there seems to be outdated now, particularly in relation to whether deleted files are staged (removed from the index), since it seems that the default behaviour of git has changed recently (or at least more recently that the article concerned).

Even the documentation at http://git-scm.com/docs/git-add has me slightly confused, or uncertain about what is and isn't staged with 'git add'.

So let us consider the basic command:

$ git add <pathspec>

My understanding from the "official" documentation is that, where the pathspec is directory name (e.g. 'dir' or 'dir/subdir' or even ‘.’), this causes all new files and modified files and deleted files in that directory to be staged (i.e. local files deleted will also be deleted in remote repo).

But where pathspec is a file or set of files (e.g. 'doc.txt' or '*.txt' or 'dir/*.js'), staging is only for new and modified files, not deleted files.

Is that correct? Is that how git chooses between the two regimes, assuming no other flags are used?

And then the explicit flags are -A to stage new, modified and deleted. Or -u to stage modified and deleted (but not new). Or --no-all to stage new and modified (but not deleted).

Is that a fair summary, or have I misunderstood? The docs do refer to a leading directory name, so maybe the rule is that 'dir/*.txt' would stage deleted files?

Would be useful if someone knowledgable could state explain the behaviour for each of the following examples, just by stating something like "new: yes, modified: yes, deleted: no":

$ git add .
$ git add dir
$ git add dir/subdir
$ git add dir/*.txt
$ git add *.txt
$ git add file.txt
$ git add ./file.txt
$ git add dir/file.txt

For example:

$ git add .    (new: yes, modified: yes, deleted: yes)

Many thanks.

0

There are 0 answers