git rm簡介

來源:互聯網
上載者:User

本文翻譯整理自:http://web.mit.edu/~mkgray/project/silk/root/afs/sipb/project/git/git-doc/git-rm.html在git中我們可以通過git rm命令把一個檔案刪除,並把它從git的倉庫管理系統中移除。但是注意最後要執行git commit才真正提交到git倉庫樣本1git rm 1.txt刪除1.txt檔案,並把它從git的倉庫管理系統中移除。樣本2git rm -r myFolder刪除檔案夾myFolder,並把它從git的倉庫管理系統中移除。樣本3

$ git add 10.txt$ git add -i           staged     unstaged path  1:        +0/-0      nothing 10.txt  2:        +0/-0      nothing branch/t.txt  3:        +0/-0      nothing branch/t2.txt
*** Commands ***  1: [s]tatus     2: [u]pdate     3: [r]evert     4: [a]dd untracked  5: [p]atch      6: [d]iff       7: [q]uit       8: [h]elpWhat now> 7Bye.$ git rm --cached 10.txtrm '10.txt'$ ls10.txt  2  3.txt  5.txt  readme.txt$ git add -i           staged     unstaged path  1:        +0/-0      nothing branch/t.txt  2:        +0/-0      nothing branch/t2.txt*** Commands ***  1: [s]tatus     2: [u]pdate     3: [r]evert     4: [a]dd untracked  5: [p]atch      6: [d]iff       7: [q]uit       8: [h]elpWhat now>在通過 git add 10.txt 命令把檔案10,txt添加到索引庫中後,又通過 git
rm --cached 10.txt 把檔案10.txt從git的索引庫中移除,但是對檔案10.txt本身並不進行任何操作。另外對於已經被git rm刪除掉(還沒被提交)的檔案或目錄,如果想取消其操作的話,可以首先通過git add -i的子命令revert從索引庫中把它們剔除,然後用git
checkout <檔案>命令來達到取消的目錄關於git add請參考《git
add詳解》關於git checkout請參考《》


文法git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>…DESCRIPTION

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. (There is no option to remove a file only from the working tree and yet keep it
in the index; use /bin/rm if you want to do that.) The files being removed have to be identical to the tip of the branch, and no updates to their contents can be staged in the index, though that default behavior
can be overridden with the -f option. When --cached is given, the staged content has to match either the tip of the branch or the file on disk, allowing the file to
be removed from just the index.

OPTIONS
<file>…

Files to remove. Fileglobs (e.g. *.c) can be given to remove all matching files. If you want git to expand file glob characters, you may need to shell-escape them. A leading directory name (e.g. dir to
remove dir/file1 and dir/file2) can be given to remove all files in the directory, and recursively all sub-directories, but this requires the -r option
to be explicitly given.

-f
--force

Override the up-to-date check.

-n
--dry-run

Don’t actually remove any file(s). Instead, just show if they exist in the index and would otherwise be removed by the command.

-r

Allow recursive removal when a leading directory name is given.

--

This option can be used to separate command-line options from the list of files, (useful when filenames might be mistaken for command-line options).

--cached

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

--ignore-unmatch

Exit with a zero status even if no files matched.

-q
--quiet

git rm normally outputs one line (in the form of an rm command) for each file removed. This option suppresses that output.

DISCUSSION

The <file> list given to the command can be exact pathnames, file glob patterns, or leading directory names. The command removes only the paths that are known to git. Giving the name of a file that you have not told git about does not remove that file.

File globbing matches across directory boundaries. Thus, given two directories d and d2, there is a difference between using git
rm 'd*' and git rm 'd/*', as the former will also remove all of directory d2.

REMOVING FILES THAT HAVE DISAPPEARED FROM THE FILESYSTEM

There is no option for git rm to remove from the index only the paths that have disappeared from the filesystem. However, depending on the use case, there are several ways that can be done.

Using “git commit -a”

If you intend that your next commit should record all modifications of tracked files in the working tree and record all removals of files that have been removed from the working tree with rm (as
opposed to git rm), use git commit -a, as it will automatically notice and record all removals. You can also have a similar effect without committing by using git
add -u.

Using “git add -A”

When accepting a new code drop for a vendor branch, you probably want to record both the removal of paths and additions of new paths as well as modifications of existing paths.

Typically you would first remove all tracked files from the working tree using this command:

git ls-files -z | xargs -0 rm -f

and then untar the new code in the working tree. Alternately you could rsync the changes into the working tree.

After that, the easiest way to record all removals, additions, and modifications in the working tree is:

git add -A

See git-add(1).

Other ways

If all you really want to do is to remove from the index the files that are no longer present in the working tree (perhaps because your working tree is dirty so that you cannot use git commit -a), use the following
command:

git diff --name-only --diff-filter=D -z | xargs -0 git rm --cached
EXAMPLES
git rm Documentation/\*.txt

Removes all *.txt files from the index that are under the Documentation directory and any of its subdirectories.

Note that the asterisk * is quoted from the shell in this example; this lets git, and not the shell, expand the pathnames of files and subdirectories under the Documentation/ directory.

git rm -f git-*.sh

Because this example lets the shell expand the asterisk (i.e. you are listing the files explicitly), it does not remove subdir/git-foo.sh.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.