Why is git rm
not the opposite of git add
? It seems to me you're asking for an opinion - specifically the opinions of the designers of the git commands - which would be off topic for SO.
The only factual answer is, they don't do that because that's not what they're designed to do, as documented in the appropriate manual pages.
git add
updates paths in the index to match corresponding paths in the working tree. That could mean tracking a new file (as you define add
in the question), some of the time. But also it could mean removing a file from the index, or it could mean replacing one version of a file with another. With that in mind, add
might not seem like the right name; if anything it "adds changes".
But whether it's the best name for the command or not, it's a good thing that a single command performs all updates - whether they be "add file" or "remove file" or something else. There's a reason git add .
is a frequently-used command, and I wouldn't want to replace it with a series of commands where I figure out which files I deleted so I can rm
them separately, etc.
As add
is the older command, then, if you wanted to define "the opposite of add
"... what would that be? Revert the index to a previous state? Which previous state? If I had both staged and unstaged changes to foo
, and I said git add foo
, git doesn't have the information to perform the inverse of that particular add
.
The other answers further point out the ambiguity of "the opposite of add". Some people think you mean "remove a file" (git rm --cached -- path
) and others think you mean "remove changes" (git reset HEAD -- path
). And that's even though you explicitly said you're talking about "tracking" and "untracking".
git rm
is better understood as a version of the *nix rm
command with added functionality to address the index, rather than any relation between its function and that of add