Roman
Roman

Reputation: 131038

Why git-rm is not opposite to git-add?

Files can be tracked or untracked. We start to track files by executing "git add".

Now let's assume that we want to stop tracking a file. I would assume that "git rm" is the way to go. My expectation was that by "git rm" we make a file "untracked". However, in reality we remove the file completely. Why? And what should one do to make file just untracked (without removing it).

Upvotes: 3

Views: 1297

Answers (3)

Mark Adelsberger
Mark Adelsberger

Reputation: 45659

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

Upvotes: 2

axiac
axiac

Reputation: 72177

The command to stop tracking a file is git rm --cached.

Remember that Git uses the index to prepare the next commit. You add files from the working tree to the index using git add.

Completely removing a file from the project is more common than untracking it. That's why git rm removes the file completely from the project, i.e. from both the working directory and the index.

In order to remove the file only from the repository you have, in fact, to remove it (or better said, mark it as removed) from the index, because this is where the next commit is prepared. The --cached option tells git rm about that. (Please note that other commands also use --cached to refer to the copy of the file stored in the index).

Upvotes: 4

Shubham Khatri
Shubham Khatri

Reputation: 281606

In order to unstage the file that you added to the list of staged files with git add you would use

git reset HEAD <file>...

Upvotes: 0

Related Questions