什么时候应该使用rm,git rm,git rm --cached,git add [英] When should I use rm, git rm, git rm --cached, git add
问题描述
我正在学习git,但是我对于不同的分段和提交文件方式感到困惑。为了包装我的头,我使用了一个目录隐喻:工作目录,暂存目录,提交目录。
- 如果我
rm
一个来自我的工作目录的文件,它只能从我的工作目录中删除 。它正确吗? - 如果我从我的工作目录中找到一个文件,它会从所有
三个目录中删除它。正确? - 如果我
git rm --cached
文件,它将从我的暂存
中删除该文件并提交目录,但将它留在我的工作目录中? - 如果我已从我的工作
目录更新,添加和删除文件,并执行git add 。
,然后git status
显示已添加,删除和更新
的分段文件。我承诺会发生什么?从提交目录中删除
删除的文件吗?如果我回滚到稍后提交的
,那么这些删除的文件会重新出现吗?
有助于更好地理解这些概念请注意 - 谢谢!
调整您对暂存区域(也称为索引或缓存)和 - cached
选项。针对 git rm
的状态
- 缓存
使用此选项可以仅从索引取消保存和删除路径。工作树文件无论是否被修改,都将被单独保存。
运行你的列表给出
-
rm文件
- 仅从工作目录中移除文件 c $ c> git rm - 从工作目录和暂存区域中删除文件,但尚未成为历史记录的一部分(存储库,提交目录) -
git rm --cached
- 从暂存区域中删除,但工作目录和历史记录都不存在
git add。
在存在修改,新文件和已删除文件的情况下--git会在缓存中记录修改和新的未登录文件。 (git add
的行为会有所不同)
各种git命令的 - cached
选项导致他们在指数上或至少在指数上操作。
git add
和 git rm
工作目录到索引或缓存。将这些命令看作是一次构建下一个提交的内容。
在您对索引中的内容感到满意之后,将更改从索引移动到存储库 git commit
。
大多数时候,你想要的是简单的序列 git rm文件
后跟 git commit
停止跟踪文件在您的历史记录中的当前位置。
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.
- If I
rm
a file from my working directory, it only removes it only from 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?
Any help to better understand these concepts would be appreciated -thanks!
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
Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.
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.)
The --cached
option to various git commands causes them to operate on the index or at least with respect to the index.
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.
After you are happy with what’s in the index, move changes from the index to the repository with git commit
.
Most of the time, what you want is the simple sequence git rm file
followed by git commit
to stop tracking file at the current point in your history.
这篇关于什么时候应该使用rm,git rm,git rm --cached,git add的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!