我什么时候应该使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:10:51  来源:igfitidea点击:

When should I use rm, git rm, git rm --cached, git add

git

提问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 rma file from my working directory, it only removes it onlyfrom my working directory. It that correct?
  • If I git rma file from my working directory, it removes it from all three directories. Correct?
  • If I git rm --cacheda 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 ., then git statusshows 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 --cachedoption. The documentation for git rmstates

调整您对暂存区(也称为索引或缓存)和--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 only
  • git 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 history
  • git add .in the presence of modifications, new files, and deleted files — git will record the modifications and new unignored files in the cache. (git addwill behave differently with certain options.)
  • rm file— 仅从工作目录中删除文件
  • git rm— 从工作目录和暂存区中删除文件,但还不是历史的一部分(存储库,“提交目录”)
  • git rm --cached— 从暂存区中删除,但既不删除工作目录也不删除历史记录
  • git add .在存在修改、新文件和删除文件的情况下——git 将在缓存中记录修改和新的未忽略文件。(git add某些选项的行为会有所不同。)

The --cachedoption to various git commands causes them to operate on the index or at least with respect to the index.

--cached各种 git 命令的选项使它们对索引进行操作,或者至少对索引进行操作。

git addand git rmtake 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 filefollowed by git committo stop tracking fileat the current point in your history.

大多数时候,您想要的是简单的序列,git rm file然后在历史记录的当前点git commit停止跟踪文件