git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: git-rm isn't the inverse action of git-add

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 11, 2007, 21:26 UTC
Message-ID
<7vps2y3a4n.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<20070711185644.GA3069@efreet.light.src>
Jan Hudec <bulb@ucw.cz> writes:
Show 5 quoted lines
> If index matches any of that, but the working tree version does not match any
> parent, the index entry should be removed (which currently isn't -- that's
> the proposed change), but the file left in wokring tree. That would make
> git-add + git-rm get you right back where you started, with nothing in index
> and unversioned file in working tree.

Don't think of 'rm' as inverse of 'add'. That would only confuse you.

A natural inverse of 'add' is 'un-add', and that operation is called 'rm --cached', because we use that to name the option to invoke an "index-only" variant of a command when the command can operate on index and working tree file (e.g. "diff --cached", "apply --cached").

A life of a file that does _not_ make into a commit goes like this:

	[1]$ edit a-new-file
This is 'create', not 'add'.  git is not involved in this step.
	[2]$ git add a-new-file

This is 'add'; place an existing file in the index. When you do not want it in the index, you 'un-add' it.

	[3]$ git rm --cached a-new-file

This removes the entry from the index, without touching the working tree file. If you do not want that file at all (as opposed to, "I am making a series of partial commits, and the addition of this path does not belong to the first commit of the series, so I am unstaging"), this is followed by

	[4]$ rm -f a-new-file
Again, git is not involved in this step.

The thing is, people sometimes want to have steps 3 and 4 combined, and it meshes well with the users' expectation when they see the word "rm". Think of "git rm" without "--cached" as a shorthand to do 3 and 4 in one go to meet that expectation.

Obviously, we cannot usefully combine steps 1 and 2. We could have "git add --create a-new-file" launch an editor to create a new file, but that would not be very useful in practice.

The fact that steps 3 and 4 can be naturally combined, but steps 1 and 2 cannot be, makes "add" and "rm" not inverse of each other.

Previous: Jan Hudec
Message 37 of 37 in “git-rm isn't the inverse action of git-add”
  1. Christian JaegerJul 2, 2007
  2. Yann DirsonJul 2, 2007
  3. Christian JaegerJul 2, 2007
  4. Yann DirsonJul 2, 2007
  5. Matthieu MoyJul 2, 2007
  6. Johannes SchindelinJul 2, 2007
  7. Matthieu MoyJul 3, 2007
  8. Johannes SchindelinJul 3, 2007
  9. Matthieu MoyJul 3, 2007
  10. Johannes SchindelinJul 3, 2007
  11. Jan HudecJul 4, 2007
  12. Matthieu MoyJul 5, 2007
  13. David KastrupJul 5, 2007
  14. [RFC][PATCH] Re: git-rm isn't the inverse action of git-addMatthieu Moy, Jul 8, 2007
  15. Johannes SchindelinJul 8, 2007
  16. Matthieu MoyJul 8, 2007
  17. Johannes SchindelinJul 8, 2007
  18. Matthieu MoyJul 9, 2007
  19. Matthieu MoyJul 13, 2007
  20. More permissive "git-rm --cached" behavior without -f.Matthieu Moy, Jul 13, 2007
  21. Jeff KingJul 13, 2007
  22. Matthieu MoyJul 13, 2007
  23. Jeff KingJul 14, 2007
  24. Jakub NarebskiJul 14, 2007
  25. Junio C HamanoJul 14, 2007
  26. Junio C HamanoJul 14, 2007
  27. Matthieu MoyJul 14, 2007
  28. Christian JaegerJul 2, 2007
  29. Jeff KingJul 3, 2007
  30. Junio C HamanoJul 3, 2007
  31. Jeff KingJul 3, 2007
  32. Junio C HamanoJul 3, 2007
  33. Jeff KingJul 3, 2007
  34. Junio C HamanoJul 3, 2007
  35. Jakub NarebskiJul 11, 2007
  36. Jan HudecJul 11, 2007
  37. Junio C HamanoJul 11, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.