3

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).

Roman
  • 124,451
  • 167
  • 349
  • 456
  • 1
    Possible duplicate of [Remove file from the repository but keep it locally](https://stackoverflow.com/questions/3469741/remove-file-from-the-repository-but-keep-it-locally) – Tim Biegeleisen Nov 28 '17 at 13:14
  • Use `git rm --cached` to keep the file in the local filesystem. – Tim Biegeleisen Nov 28 '17 at 13:14
  • 1
    "We start to track files by executing git add". -- Not exactly. With add you add content to the staging area for your next commit. That's different form other systems, where you add files - in git you add content. – miku Nov 28 '17 at 13:15
  • @miku, and how do you start to track files? I do not know any other ways except "git add". I know that "git add" does more than just start tracking files but it does not disprove the fact that "git add" is the way to start tracking. – Roman Nov 28 '17 at 13:17

3 Answers3

4

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).

axiac
  • 68,258
  • 9
  • 99
  • 134
2

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

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
0

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>...
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400