Skip to contentSkip to conversation

git rm --cached

PATCH In review Jing Xue 6 participants 10 messages Jun 15, 2016 Documentation

lore
Share & tools
lore

Jing Xue

author
#0 lore raw

In the following scenario, why do I have to run 'git reset' following 'git rm --cached 1.txt' to revert to exactly where I was before 'git add 1.txt'? Shouldn't 'git rm --cached' have done that already?

jingxue@fawkes:~/workspace/t1.git$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#       modified:   1.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
jingxue@fawkes:~/workspace/t1.git$ git add 1.txt
jingxue@fawkes:~/workspace/t1.git$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   1.txt
#
jingxue@fawkes:~/workspace/t1.git$ git rm --cached 1.txt
rm '1.txt'
jingxue@fawkes:~/workspace/t1.git$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    1.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       1.txt
jingxue@fawkes:~/workspace/t1.git$ git reset
1.txt: needs update
jingxue@fawkes:~/workspace/t1.git$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#       modified:   1.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

Thanks.

Signature · 1 line
Jing Xue
#1 lore raw
Quoted 3 lines · Jing Xue

In the following scenario, why do I have to run 'git reset' following 'git rm --cached 1.txt' to revert to exactly where I was before 'git add 1.txt'? Shouldn't 'git rm --cached' have done that already?

Observed behavior are exactly what I expected: 'git rm --cached' mark the file in the index as been deleted without deleting it in the working directories, it did not but the index it was before the 'git add 1.txt'.

You probably want to use git reset HEAD -- 1.txt to unstage modification on 1.txt

Signature · 1 line
Rémi Vanicat
author
#2 lore raw

Quoting Remi Vanicat <[email protected]>:

Quoted 10 lines
Quoted 3 lines · Jing Xue

In the following scenario, why do I have to run 'git reset' following 'git rm --cached 1.txt' to revert to exactly where I was before 'git add 1.txt'? Shouldn't 'git rm --cached' have done that already?

Observed behavior are exactly what I expected: 'git rm --cached' mark the file in the index as been deleted without deleting it in the working directories, it did not but the index it was before the 'git add 1.txt'.

I was confused by two things I guess:

1. I looked at the "index" as a staging area for _changes_ not files themselves. So where 'man git-rm' says '--caches ... remove[s] the paths only from the index, leaving working tree files.' I took it to mean that it removes the changes on those paths, rather than staging a new "path deletion" action for a later commit.

2. The FAQ entry "Why 'git rm' is not inverse of 'git add'" says "a natural inverse of 'add' is 'un-add', and that operation is called 'rm --cached',..." Now I realize that only applies to adding a new file, but not changes on an existing file.

Quoted 2 lines

You probably want to use git reset HEAD -- 1.txt to unstage modification on 1.txt

Sure.

Thanks.

Signature · 1 line
Jing Xue
#3 lore raw
Quoted 4 lines · Jing Xue

2. The FAQ entry "Why 'git rm' is not inverse of 'git add'" says "a natural inverse of 'add' is 'un-add', and that operation is called 'rm --cached',..." Now I realize that only applies to adding a new file, but not changes on an existing file.

Well, so it seem that to think of "git rm --cached" as inverse to "git add" is also confusing. The FAQ entry should probably be rewrite. Or at least clarified.

Signature · 1 line
Rémi Vanicat
#4 lore raw
In reply to Jing Xue
Quoted 5 lines · Jing Xue

1. I looked at the "index" as a staging area for _changes_ not files themselves. So where 'man git-rm' says '--caches ... remove[s] the paths only from the index, leaving working tree files.' I took it to mean that it removes the changes on those paths, rather than staging a new "path deletion" action for a later commit.

The index is a full snapshot of "what will be commited". The interesting parts of the index are usually the ones which differ from either HEAD or the working tree, but the index do contain everything.

Signature · 1 line
Matthieu
#5 lore raw
In reply to Jing Xue
Quoted 3 lines · Jing Xue

In the following scenario, why do I have to run 'git reset' following 'git rm --cached 1.txt' to revert to exactly where I was before 'git add 1.txt'? Shouldn't 'git rm --cached' have done that already?

The message in git-commit suggesting to use 'git rm --cached' to unstage is just plain wrong. It really should mention 'git reset'.

git rm, as the name suggests, *removes* the file.

git reset, as the name suggests, reverts it to the state it was before (but, somewhat confusingly, with path limit only resets the index, so no --cached option there).

Signature · 1 line
						 Jan 'Bulb' Hudec <[email protected]>
author
#6 lore raw

PATCH 1/1 replace reference to git-rm with git-reset in git-commit doc

Quoted 3 lines · Jan Hudec

The message in git-commit suggesting to use 'git rm --cached' to unstage is just plain wrong. It really should mention 'git reset'.

Hopefully this makes it clearer. I have also updated the faq in wiki to clarify.

---
 Documentation/git-add.txt    |    1 +
 Documentation/git-commit.txt |   12 ++++++------
 2 files changed, 7 insertions(+), 6 deletions(-)
Documentation/git-add.txt +1 -0
index 963e1ab..63829d9 100644
@@ -224,6 +224,7 @@ See Also
 --------
 gitlink:git-status[1]
 gitlink:git-rm[1]
+gitlink:git-reset[1]
 gitlink:git-mv[1]
 gitlink:git-commit[1]
 gitlink:git-update-index[1]
Documentation/git-commit.txt +6 -6
index e54fb12..7c63dd8 100644
@@ -154,12 +154,12 @@ EXAMPLES
 --------
 When recording your own work, the contents of modified files in
 your working tree are temporarily stored to a staging area
-called the "index" with gitlink:git-add[1].  Removal
-of a file is staged with gitlink:git-rm[1].  After building the
-state to be committed incrementally with these commands, `git
-commit` (without any pathname parameter) is used to record what
-has been staged so far.  This is the most basic form of the
-command.  An example:
+called the "index" with gitlink:git-add[1].  File changes
+previously staged can be removed with `git-reset
+HEAD -- <file>`.  After building the state to be committed
+incrementally with these commands, `git commit` (without any
+pathname parameter) is used to record what has been staged so
+far.  This is the most basic form of the command.  An example:
 
 ------------
 $ edit hello.c
#7 lore raw

Re: [PATCH] replace reference to git-rm with git-reset in git-commit doc

Quoted 13 lines · Jing Xue
Quoted 3 lines · Jan Hudec

The message in git-commit suggesting to use 'git rm --cached' to unstage is just plain wrong. It really should mention 'git reset'.

Hopefully this makes it clearer. I have also updated the faq in wiki to clarify.

Documentation/git-commit.txt +0 -0
index e54fb12..7c63dd8 100644

Documentation/git-commit.txt EXAMPLES · line 159

Quoted 6 lines
Documentation/git-commit.txt +3 -0
@@ -154,12 +154,12 @@ EXAMPLES
 --------
 When recording your own work, the contents of modified files in
 your working tree are temporarily stored to a staging area
+called the "index" with gitlink:git-add[1].  File changes
+previously staged can be removed with `git-reset
+HEAD -- <file>`.

I think "changes ... can be removed" risks to give a confused mental model that somehow git tracks changes. "A file can be reverted back to that of the last commit with ..." would be less risky.

author
#8 lore raw

PATCH 1/1 RESUBMIT: replace reference to git-rm with git-reset in git-commit doc

Quoted 3 lines · Junio C Hamano

I think "changes ... can be removed" risks to give a confused mental model that somehow git tracks changes.

I see what you mean. "Changes" shouldn't be the subject here.

Quoted 3 lines

"A file can be
reverted back to that of the last commit with ..." would be less risky.

On top of that, I somehow still want to make it relevant to that git-reset instead of git-rm should be used to revert git-add. So how about this?

---
 Documentation/git-add.txt    |    1 +
 Documentation/git-commit.txt |   13 ++++++++-----
 2 files changed, 9 insertions(+), 5 deletions(-)
Documentation/git-add.txt +1 -0
index 963e1ab..63829d9 100644
@@ -224,6 +224,7 @@ See Also
 --------
 gitlink:git-status[1]
 gitlink:git-rm[1]
+gitlink:git-reset[1]
 gitlink:git-mv[1]
 gitlink:git-commit[1]
 gitlink:git-update-index[1]
Documentation/git-commit.txt +8 -5
index e54fb12..4b26cae 100644
@@ -154,11 +154,14 @@ EXAMPLES
 --------
 When recording your own work, the contents of modified files in
 your working tree are temporarily stored to a staging area
-called the "index" with gitlink:git-add[1].  Removal
-of a file is staged with gitlink:git-rm[1].  After building the
-state to be committed incrementally with these commands, `git
-commit` (without any pathname parameter) is used to record what
-has been staged so far.  This is the most basic form of the
+called the "index" with gitlink:git-add[1].  A file can be
+reverted back, only in the index but not in the working tree,
+to that of the last commit with `git-reset HEAD -- <file>`,
+which effectively reverts `git-add` and prevents this file from
+participating in the next commit.  After building the state to
+be committed incrementally with these commands, `git commit`
+(without any pathname parameter) is used to record what has
+been staged so far.  This is the most basic form of the
 command.  An example:
 
 ------------
#9 lore raw

Re: [PATCH] RESUBMIT: replace reference to git-rm with git-reset in git-commit doc

Quoted 3 lines · Jing Xue

On top of that, I somehow still want to make it relevant to that git-reset instead of git-rm should be used to revert git-add. So how about this?

Thanks. I'll fix up the log message and with a bit of rewording.