0

According to git rm doc ,

Remove files from the index, or from the working tree and the index. git rm will not remove a file from just your working directory.

Also,

--cached : Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.

Now , I found a difference in the way they are changing working tree and index where I feel it does not matches the definition above.

First I ran git rm --cached and I saw this :

$ git rm --cached  src/main/java/com/kang/pagination/PaginationResult.java
rm 'src/main/java/com/kang/pagination/PaginationResult.java'

$ git status

On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    src/main/java/com/kang/pagination/PaginationResult.java

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        src/main/java/com/kang/pagination/PaginationResult.java

We can see the change as untracked file and in staged area.

Why we have file in untracked area when the file change is in staging area ?

Instead of that , If I had ran git rm , this is what I would got :

$ git rm src/main/java/com/kang/pagination/PaginationResult.java
rm 'src/main/java/com/kang/pagination/PaginationResult.java'

$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    src/main/java/com/kang/pagination/PaginationResult.java

Note that here we don't have untracked file. Why ?

Number945
  • 4,631
  • 8
  • 45
  • 83
  • git rm alone, actually deletes physically your file... where --cached not – Johnny Willer Feb 07 '19 at 15:01
  • Possible duplicate of [Remove a file from a Git repository without deleting it from the local filesystem](https://stackoverflow.com/questions/1143796/remove-a-file-from-a-git-repository-without-deleting-it-from-the-local-filesyste) – Johnny Willer Feb 07 '19 at 15:06

2 Answers2

5

Your edit notwithstanding, the answer to your question is exactly explained by what the commands do (not the commands' internal working).

Perhaps the confusion stems from understanding how to interpret git status. An "untracked file' is a file in the worktree that is not in the index. (To be listed in the default status output, it also must not be ignored.)

So if you start with a clean worktree, that means any file in the worktree is also in the index - so not untracked.

But then if you git rm --cached the file, it is removed from the index while being left alone in the worktree. That means there is now a file in the wokrtree that is not in the index, so unless that path is ignored, it's going to be shown as an untracked file - even though nothing in the worktree itself changed.

That is different from git rm (without the --cached argument) not in terms of internal workings, but rather in terms of what the command does. This command also removes the file from the worktree; in this case there is not, at the end, a file in the worktree but not in the index (an untracked file).

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

Why we have file in untracked area when the file change is in staging area ?

Because you've used git rm --cached which removes the file from the staging area, but leaves the file on disk. Since there is now no file in the repository, that file on disk must be untracked.

Note that here we don't have untracked file. Why ?

Because the file doesn't exist on disk. Therefore it couldn't possibly be untracked because it doesn't exist.

Imagine that after running git rm (before committing that changeE) that you recreate a new file with the same name. Running git status will tell you that file is untracked, just as if you had created any new file that wasn't in the index. This is the scenario that you've just emulated by running git rm --cached.

Edward Thomson
  • 74,857
  • 14
  • 158
  • 187