Junio C Hamano | 34b1811 | 2006-02-23 11:21:40 | [diff] [blame] | 1 | git-rm(1) |
| 2 | ========= |
| 3 | |
| 4 | NAME |
| 5 | ---- |
Junio C Hamano | 0107892 | 2006-03-10 00:31:47 | [diff] [blame] | 6 | git-rm - Remove files from the working tree and from the index |
Junio C Hamano | 34b1811 | 2006-02-23 11:21:40 | [diff] [blame] | 7 | |
| 8 | SYNOPSIS |
| 9 | -------- |
Junio C Hamano | 15567bc | 2011-07-23 00:51:59 | [diff] [blame] | 10 | [verse] |
Junio C Hamano | faa1e50 | 2008-08-10 03:55:58 | [diff] [blame] | 11 | 'git rm' [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>... |
Junio C Hamano | 34b1811 | 2006-02-23 11:21:40 | [diff] [blame] | 12 | |
| 13 | DESCRIPTION |
| 14 | ----------- |
Junio C Hamano | 2ec4d36 | 2008-04-17 02:17:53 | [diff] [blame] | 15 | Remove files from the index, or from the working tree and the index. |
Junio C Hamano | ae05fee | 2010-01-08 07:39:46 | [diff] [blame] | 16 | `git rm` will not remove a file from just your working directory. |
| 17 | (There is no option to remove a file only from the working tree |
Junio C Hamano | 2ec4d36 | 2008-04-17 02:17:53 | [diff] [blame] | 18 | and yet keep it in the index; use `/bin/rm` if you want to do that.) |
| 19 | The files being removed have to be identical to the tip of the branch, |
| 20 | and no updates to their contents can be staged in the index, |
| 21 | though that default behavior can be overridden with the `-f` option. |
Junio C Hamano | ae05fee | 2010-01-08 07:39:46 | [diff] [blame] | 22 | When `--cached` is given, the staged content has to |
Junio C Hamano | 2ec4d36 | 2008-04-17 02:17:53 | [diff] [blame] | 23 | match either the tip of the branch or the file on disk, |
| 24 | allowing the file to be removed from just the index. |
Junio C Hamano | 34b1811 | 2006-02-23 11:21:40 | [diff] [blame] | 25 | |
| 26 | |
| 27 | OPTIONS |
| 28 | ------- |
| 29 | <file>...:: |
Junio C Hamano | 3d2adc7 | 2006-12-26 03:21:45 | [diff] [blame] | 30 | Files to remove. Fileglobs (e.g. `*.c`) can be given to |
Junio C Hamano | 2ec4d36 | 2008-04-17 02:17:53 | [diff] [blame] | 31 | remove all matching files. If you want git to expand |
| 32 | file glob characters, you may need to shell-escape them. |
| 33 | A leading directory name |
| 34 | (e.g. `dir` to remove `dir/file1` and `dir/file2`) can be |
| 35 | given to remove all files in the directory, and recursively |
| 36 | all sub-directories, |
| 37 | but this requires the `-r` option to be explicitly given. |
Junio C Hamano | 34b1811 | 2006-02-23 11:21:40 | [diff] [blame] | 38 | |
| 39 | -f:: |
Junio C Hamano | faa1e50 | 2008-08-10 03:55:58 | [diff] [blame] | 40 | --force:: |
Junio C Hamano | 3d2adc7 | 2006-12-26 03:21:45 | [diff] [blame] | 41 | Override the up-to-date check. |
Junio C Hamano | 34b1811 | 2006-02-23 11:21:40 | [diff] [blame] | 42 | |
Junio C Hamano | eb41599 | 2008-06-08 22:49:47 | [diff] [blame] | 43 | -n:: |
| 44 | --dry-run:: |
Junio C Hamano | 2ec4d36 | 2008-04-17 02:17:53 | [diff] [blame] | 45 | Don't actually remove any file(s). Instead, just show |
| 46 | if they exist in the index and would otherwise be removed |
| 47 | by the command. |
Junio C Hamano | 34b1811 | 2006-02-23 11:21:40 | [diff] [blame] | 48 | |
Junio C Hamano | 3d2adc7 | 2006-12-26 03:21:45 | [diff] [blame] | 49 | -r:: |
| 50 | Allow recursive removal when a leading directory name is |
| 51 | given. |
Junio C Hamano | 34b1811 | 2006-02-23 11:21:40 | [diff] [blame] | 52 | |
Junio C Hamano | 1bb569e | 2006-05-05 23:14:25 | [diff] [blame] | 53 | \--:: |
Junio C Hamano | 34b1811 | 2006-02-23 11:21:40 | [diff] [blame] | 54 | This option can be used to separate command-line options from |
| 55 | the list of files, (useful when filenames might be mistaken |
| 56 | for command-line options). |
| 57 | |
Junio C Hamano | eb41599 | 2008-06-08 22:49:47 | [diff] [blame] | 58 | --cached:: |
Junio C Hamano | 2ec4d36 | 2008-04-17 02:17:53 | [diff] [blame] | 59 | Use this option to unstage and remove paths only from the index. |
| 60 | Working tree files, whether modified or not, will be |
| 61 | left alone. |
Junio C Hamano | 3d2adc7 | 2006-12-26 03:21:45 | [diff] [blame] | 62 | |
Junio C Hamano | eb41599 | 2008-06-08 22:49:47 | [diff] [blame] | 63 | --ignore-unmatch:: |
Junio C Hamano | 28cea6a | 2007-04-18 04:57:17 | [diff] [blame] | 64 | Exit with a zero status even if no files matched. |
| 65 | |
Junio C Hamano | eb41599 | 2008-06-08 22:49:47 | [diff] [blame] | 66 | -q:: |
| 67 | --quiet:: |
Junio C Hamano | ae05fee | 2010-01-08 07:39:46 | [diff] [blame] | 68 | `git rm` normally outputs one line (in the form of an `rm` command) |
Junio C Hamano | 0e3cb53 | 2007-04-17 08:28:11 | [diff] [blame] | 69 | for each file removed. This option suppresses that output. |
| 70 | |
Junio C Hamano | 34b1811 | 2006-02-23 11:21:40 | [diff] [blame] | 71 | |
| 72 | DISCUSSION |
| 73 | ---------- |
| 74 | |
Junio C Hamano | 2ec4d36 | 2008-04-17 02:17:53 | [diff] [blame] | 75 | The <file> list given to the command can be exact pathnames, |
| 76 | file glob patterns, or leading directory names. The command |
| 77 | removes only the paths that are known to git. Giving the name of |
Junio C Hamano | 3d2adc7 | 2006-12-26 03:21:45 | [diff] [blame] | 78 | a file that you have not told git about does not remove that file. |
Junio C Hamano | 34b1811 | 2006-02-23 11:21:40 | [diff] [blame] | 79 | |
Junio C Hamano | 2ec4d36 | 2008-04-17 02:17:53 | [diff] [blame] | 80 | File globbing matches across directory boundaries. Thus, given |
| 81 | two directories `d` and `d2`, there is a difference between |
Junio C Hamano | b76a686 | 2012-05-02 22:02:46 | [diff] [blame] | 82 | using `git rm 'd*'` and `git rm 'd/*'`, as the former will |
Junio C Hamano | 2ec4d36 | 2008-04-17 02:17:53 | [diff] [blame] | 83 | also remove all of directory `d2`. |
Junio C Hamano | 34b1811 | 2006-02-23 11:21:40 | [diff] [blame] | 84 | |
Junio C Hamano | ae05fee | 2010-01-08 07:39:46 | [diff] [blame] | 85 | REMOVING FILES THAT HAVE DISAPPEARED FROM THE FILESYSTEM |
| 86 | -------------------------------------------------------- |
| 87 | There is no option for `git rm` to remove from the index only |
| 88 | the paths that have disappeared from the filesystem. However, |
| 89 | depending on the use case, there are several ways that can be |
| 90 | done. |
| 91 | |
Junio C Hamano | 28e6d1f | 2010-12-08 02:06:14 | [diff] [blame] | 92 | Using ``git commit -a'' |
| 93 | ~~~~~~~~~~~~~~~~~~~~~~~ |
Junio C Hamano | ae05fee | 2010-01-08 07:39:46 | [diff] [blame] | 94 | If you intend that your next commit should record all modifications |
| 95 | of tracked files in the working tree and record all removals of |
| 96 | files that have been removed from the working tree with `rm` |
| 97 | (as opposed to `git rm`), use `git commit -a`, as it will |
| 98 | automatically notice and record all removals. You can also have a |
| 99 | similar effect without committing by using `git add -u`. |
| 100 | |
Junio C Hamano | 28e6d1f | 2010-12-08 02:06:14 | [diff] [blame] | 101 | Using ``git add -A'' |
| 102 | ~~~~~~~~~~~~~~~~~~~~ |
Junio C Hamano | ae05fee | 2010-01-08 07:39:46 | [diff] [blame] | 103 | When accepting a new code drop for a vendor branch, you probably |
| 104 | want to record both the removal of paths and additions of new paths |
| 105 | as well as modifications of existing paths. |
| 106 | |
| 107 | Typically you would first remove all tracked files from the working |
| 108 | tree using this command: |
| 109 | |
| 110 | ---------------- |
| 111 | git ls-files -z | xargs -0 rm -f |
| 112 | ---------------- |
| 113 | |
Junio C Hamano | 28e6d1f | 2010-12-08 02:06:14 | [diff] [blame] | 114 | and then untar the new code in the working tree. Alternately |
| 115 | you could 'rsync' the changes into the working tree. |
Junio C Hamano | ae05fee | 2010-01-08 07:39:46 | [diff] [blame] | 116 | |
| 117 | After that, the easiest way to record all removals, additions, and |
| 118 | modifications in the working tree is: |
| 119 | |
| 120 | ---------------- |
| 121 | git add -A |
| 122 | ---------------- |
| 123 | |
| 124 | See linkgit:git-add[1]. |
| 125 | |
| 126 | Other ways |
| 127 | ~~~~~~~~~~ |
| 128 | If all you really want to do is to remove from the index the files |
| 129 | that are no longer present in the working tree (perhaps because |
| 130 | your working tree is dirty so that you cannot use `git commit -a`), |
| 131 | use the following command: |
| 132 | |
| 133 | ---------------- |
| 134 | git diff --name-only --diff-filter=D -z | xargs -0 git rm --cached |
| 135 | ---------------- |
| 136 | |
Junio C Hamano | f2b7494 | 2012-11-20 21:06:26 | [diff] [blame^] | 137 | Submodules |
| 138 | ~~~~~~~~~~ |
| 139 | Only submodules using a gitfile (which means they were cloned |
| 140 | with a git version 1.7.8 or newer) will be removed from the work |
| 141 | tree, as their repository lives inside the .git directory of the |
| 142 | superproject. If a submodule (or one of those nested inside it) |
| 143 | still uses a .git directory, `git rm` will fail - no matter if forced |
| 144 | or not - to protect the submodule's history. |
| 145 | |
| 146 | A submodule is considered up-to-date when the HEAD is the same as |
| 147 | recorded in the index, no tracked files are modified and no untracked |
| 148 | files that aren't ignored are present in the submodules work tree. |
| 149 | Ignored files are deemed expendable and won't stop a submodule's work |
| 150 | tree from being removed. |
| 151 | |
Junio C Hamano | 34b1811 | 2006-02-23 11:21:40 | [diff] [blame] | 152 | EXAMPLES |
| 153 | -------- |
Junio C Hamano | 16ebcd0 | 2011-08-05 00:05:45 | [diff] [blame] | 154 | `git rm Documentation/\*.txt`:: |
Junio C Hamano | 2db3e75 | 2010-09-03 21:33:06 | [diff] [blame] | 155 | Removes all `*.txt` files from the index that are under the |
Junio C Hamano | 1ce39ab | 2007-01-16 22:05:10 | [diff] [blame] | 156 | `Documentation` directory and any of its subdirectories. |
Junio C Hamano | 34b1811 | 2006-02-23 11:21:40 | [diff] [blame] | 157 | + |
Junio C Hamano | 2db3e75 | 2010-09-03 21:33:06 | [diff] [blame] | 158 | Note that the asterisk `*` is quoted from the shell in this |
Junio C Hamano | 2ec4d36 | 2008-04-17 02:17:53 | [diff] [blame] | 159 | example; this lets git, and not the shell, expand the pathnames |
| 160 | of files and subdirectories under the `Documentation/` directory. |
Junio C Hamano | 34b1811 | 2006-02-23 11:21:40 | [diff] [blame] | 161 | |
Junio C Hamano | 16ebcd0 | 2011-08-05 00:05:45 | [diff] [blame] | 162 | `git rm -f git-*.sh`:: |
Junio C Hamano | 1ce39ab | 2007-01-16 22:05:10 | [diff] [blame] | 163 | Because this example lets the shell expand the asterisk |
| 164 | (i.e. you are listing the files explicitly), it |
Junio C Hamano | 3d2adc7 | 2006-12-26 03:21:45 | [diff] [blame] | 165 | does not remove `subdir/git-foo.sh`. |
Junio C Hamano | 34b1811 | 2006-02-23 11:21:40 | [diff] [blame] | 166 | |
Junio C Hamano | 9049d91 | 2008-05-29 02:09:50 | [diff] [blame] | 167 | SEE ALSO |
Junio C Hamano | 2bb1310 | 2006-03-06 07:10:21 | [diff] [blame] | 168 | -------- |
Junio C Hamano | 35738e8 | 2008-01-07 07:55:46 | [diff] [blame] | 169 | linkgit:git-add[1] |
Junio C Hamano | 34b1811 | 2006-02-23 11:21:40 | [diff] [blame] | 170 | |
Junio C Hamano | 34b1811 | 2006-02-23 11:21:40 | [diff] [blame] | 171 | GIT |
| 172 | --- |
Junio C Hamano | f7c042d | 2008-06-06 22:50:53 | [diff] [blame] | 173 | Part of the linkgit:git[1] suite |