为什么使用“git rm”而不是“rm”来删除文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7434449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why use 'git rm' to remove a file instead of 'rm'?
提问by cjm2671
On SVN, removing something from the filesystem directly (rather than using svn) created a load of headaches.
在 SVN 上,直接从文件系统中删除某些内容(而不是使用 svn)会带来很多麻烦。
I haven't found this to be an issue when using git
, but I notice that git has it's own rm
implementation (git rm
).
我在使用时没有发现这是一个问题git
,但我注意到 git 有它自己的rm
实现 ( git rm
)。
What is the difference between rm
and git rm
?
rm
和 和有git rm
什么区别?
回答by Andy
If you just use rm
, you will need to follow it up with git add <fileRemoved>
. git rm
does this in one step.
如果你只是使用rm
,你将需要跟进它git add <fileRemoved>
。 git rm
一步完成。
You can also use git rm --cached
which will remove the file from the index (staging it for deletion on the next commit), but keep your copy in the local file system.
您还可以使用git rm --cached
which 将从索引中删除文件(将其暂存以在下一次提交时删除),但将副本保留在本地文件系统中。
回答by hammar
Removing files using rm
is not a problem per se, but if you then want to commit that the file was removed, you will have to do a git rm
anyway, so you might as well do it that way right off the bat.
使用删除文件rm
本身不是问题,但是如果您随后想要提交该文件已被删除,则git rm
无论如何您都必须执行,因此您最好立即这样做。
Also, depending on your shell, doing git rm
after having deleted the file, you will not get tab-completion so you'll have to spell out the path yourself, whereas if you git rm
while the file still exists, tab completion will work as normal.
此外,根据您的 shell,git rm
在删除文件后执行操作,您将不会获得制表符补全,因此您必须自己拼出路径,而如果您git rm
在文件仍然存在时,制表符补全将正常工作。
回答by manojlds
git rm
will remove the file from the index and working directory ( only index if you used --cached
) so that the deletion is staged for next commit.
git rm
将从索引和工作目录中删除文件(如果您使用只有索引--cached
),以便为下一次提交暂存删除。
回答by basicxman
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.
从索引或从工作树和索引中删除文件。git rm 不会仅从您的工作目录中删除文件。
Here's how you might delete a file using rm -f
and then remove it from your index with git rm
以下是如何使用删除文件rm -f
然后将其从索引中删除git rm
$ rm -f index.html
$ git status -s
D index.html
$ git rm index.html
rm 'index.html'
$ git status -s
D index.html
However you can do this all in one go with just git rm
但是,您可以一次性完成这一切 git rm
$ git status -s
$ git rm index.html
rm 'index.html'
$ ls
lib vendor
$ git status -s
D index.html
回答by Struggler
However, if you do end up using rm instead of git rm. You can skip the git add and directly commit the changes using:
但是,如果您最终使用 rm 而不是 git rm。您可以跳过 git add 并使用以下命令直接提交更改:
git commit -a
git commit -a
回答by keis
When using git rm, the removal will part of your next commit. So if you want to push the change you should use git rm
使用 git rm 时,删除将成为您下一次提交的一部分。所以如果你想推动改变,你应该使用 git rm
回答by Joe
Adding to Andy's answer, there is additional utility to git rm
:
除了安迪的回答之外,还有以下附加实用程序git rm
:
Safety: When doing
git rm
instead ofrm
, Git will block the removal if there is a discrepancy between theHEAD
version of a file and the staging index or working tree version. This block is a safetymechanism to prevent removal of in-progress changes.Safeguarding:
git rm --dry-run
. This option is a safeguard that will execute thegit rm
command but not actually delete the files. Instead it will output which files it would have removed.
安全性:当使用
git rm
代替 时rm
,如果HEAD
文件版本与暂存索引或工作树版本之间存在差异,Git 将阻止删除。此块是一种安全机制,可防止删除正在进行的更改。保护:
git rm --dry-run
。此选项是一种安全措施,将执行git rm
命令但不会实际删除文件。相反,它将输出它会删除哪些文件。