我什么时候应该使用 rm, git rm, git rm --cached, git add
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37279654/
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
When should I use rm, git rm, git rm --cached, git add
提问by jshock
I am learning git, but I am confused by different ways of staging and committing files. To wrap my head around this I'm using a metaphor of directories: working directory, staging directory, commit directory.
我正在学习 git,但我对不同的暂存和提交文件的方式感到困惑。为了解决这个问题,我使用了目录的比喻:工作目录、暂存目录、提交目录。
- If I
rm
a file from my working directory, it only removes it onlyfrom my working directory. It that correct? - If I
git rm
a file from my working directory, it removes it from all three directories. Correct? - If I
git rm --cached
a file, it is removes the file from my staged and commit directories, but leave it in my working directory? - If I have updated, added, and deleted files from my working
directory, and do
git add .
, thengit status
shows staged files that have been added, deleted, and updated. What happens when I commit? Are the deleted files removed from the Commit directory? If I roll back to that commit later, will those deleted files reappear?
- 如果我
rm
从我的工作目录中删除一个文件,它只会从我的工作目录中删除它。那正确吗? - 如果我
git rm
是工作目录中的一个文件,它会将其从所有三个目录中删除。正确的? - 如果我
git rm --cached
是一个文件,它会从我的暂存和提交目录中删除该文件,但将其保留在我的工作目录中? - 如果我已更新、添加和删除工作目录中的文件
git add .
,然后执行,则git status
显示已添加、删除和更新的暂存文件。当我提交时会发生什么?删除的文件是否从 Commit 目录中删除?如果我稍后回滚到那个提交,那些被删除的文件会重新出现吗?
Any help to better understand these concepts would be appreciated -thanks!
任何有助于更好地理解这些概念的帮助将不胜感激 - 谢谢!
回答by Greg Bacon
Tweak your understanding of the staging area (also known as the index or cache) and the --cached
option. The documentation for git rm
states
调整您对暂存区(也称为索引或缓存)和--cached
选项的理解。在对文档git rm
状态
--cached
Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.
--cached
使用此选项可仅从索引中取消暂存和删除路径。工作树文件,无论是否修改,都将被保留。
Running down your list gives
运行你的清单给出
rm file
— remove file from the work directory onlygit rm
— remove file from the work directory and from the staging area, but not yet part of the history (repository, “commit directory”)git rm --cached
— remove from staging area but neither the work directory nor the historygit add .
in the presence of modifications, new files, and deleted files — git will record the modifications and new unignored files in the cache. (git add
will behave differently with certain options.)
rm file
— 仅从工作目录中删除文件git rm
— 从工作目录和暂存区中删除文件,但还不是历史的一部分(存储库,“提交目录”)git rm --cached
— 从暂存区中删除,但既不删除工作目录也不删除历史记录git add .
在存在修改、新文件和删除文件的情况下——git 将在缓存中记录修改和新的未忽略文件。(git add
某些选项的行为会有所不同。)
The --cached
option to various git commands causes them to operate on the index or at least with respect to the index.
--cached
各种 git 命令的选项使它们对索引进行操作,或者至少对索引进行操作。
git add
and git rm
take changes from the work directory to the index or cache. Think of these commands as building up your next commit a piece at a time.
git add
并将git rm
更改从工作目录更改为索引或缓存。将这些命令视为一次构建您的下一次提交。
After you are happy with what's in the index, move changes from the index to the repository with git commit
.
在您对索引中的内容感到满意后,将更改从索引移动到存储库git commit
。
Most of the time, what you want is the simple sequence git rm file
followed by git commit
to stop tracking fileat the current point in your history.
大多数时候,您想要的是简单的序列,git rm file
然后在历史记录的当前点git commit
停止跟踪文件。