Skip to content

Commit 47b7012

Browse files
bjornggitster
authored andcommitted
git-rm doc: Describe how to sync index & work tree
Newcomers to git that want to remove from the index only the files that have disappeared from the working tree will probably look for a way to do that in the documentation for 'git rm'. Therefore, describe how that can be done (even though it involves other commands than 'git rm'). Based on a suggestion by Junio, but re-arranged and rewritten to better fit into the style of command reference. While at it, change a single occurrence of "work tree" to "working tree" for consistency. Signed-off-by: Björn Gustavsson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 441947f commit 47b7012

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

Documentation/git-rm.txt

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ DESCRIPTION
1313
-----------
1414
Remove files from the index, or from the working tree and the index.
1515
`git rm` will not remove a file from just your working directory.
16-
(There is no option to remove a file only from the work tree
16+
(There is no option to remove a file only from the working tree
1717
and yet keep it in the index; use `/bin/rm` if you want to do that.)
1818
The files being removed have to be identical to the tip of the branch,
1919
and no updates to their contents can be staged in the index,
@@ -81,6 +81,58 @@ two directories `d` and `d2`, there is a difference between
8181
using `git rm \'d\*\'` and `git rm \'d/\*\'`, as the former will
8282
also remove all of directory `d2`.
8383

84+
REMOVING FILES THAT HAVE DISAPPEARED FROM THE FILESYSTEM
85+
--------------------------------------------------------
86+
There is no option for `git rm` to remove from the index only
87+
the paths that have disappeared from the filesystem. However,
88+
depending on the use case, there are several ways that can be
89+
done.
90+
91+
Using "git commit -a"
92+
~~~~~~~~~~~~~~~~~~~~~
93+
If you intend that your next commit should record all modifications
94+
of tracked files in the working tree and record all removals of
95+
files that have been removed from the working tree with `rm`
96+
(as opposed to `git rm`), use `git commit -a`, as it will
97+
automatically notice and record all removals. You can also have a
98+
similar effect without committing by using `git add -u`.
99+
100+
Using "git add -A"
101+
~~~~~~~~~~~~~~~~~~
102+
When accepting a new code drop for a vendor branch, you probably
103+
want to record both the removal of paths and additions of new paths
104+
as well as modifications of existing paths.
105+
106+
Typically you would first remove all tracked files from the working
107+
tree using this command:
108+
109+
----------------
110+
git ls-files -z | xargs -0 rm -f
111+
----------------
112+
113+
and then "untar" the new code in the working tree. Alternately
114+
you could "rsync" the changes into the working tree.
115+
116+
After that, the easiest way to record all removals, additions, and
117+
modifications in the working tree is:
118+
119+
----------------
120+
git add -A
121+
----------------
122+
123+
See linkgit:git-add[1].
124+
125+
Other ways
126+
~~~~~~~~~~
127+
If all you really want to do is to remove from the index the files
128+
that are no longer present in the working tree (perhaps because
129+
your working tree is dirty so that you cannot use `git commit -a`),
130+
use the following command:
131+
132+
----------------
133+
git diff --name-only --diff-filter=D -z | xargs -0 git rm --cached
134+
----------------
135+
84136
EXAMPLES
85137
--------
86138
git rm Documentation/\\*.txt::

0 commit comments

Comments
 (0)