“git rm --cached x” vs “git 重置头 --” x”?
git rm
将从中删除条目 暂存区。这有点不同 来自 git reset HEAD ,它“取消了阶段” 文件。我所说的“unstage”是指它恢复 暂存区到那里的情况 在我们开始修改之前。 另一方面, git rm 就可以了 文件完全退出舞台,所以 它不包含在下一个中 提交快照,从而有效地 删除它。默认情况下,
git rm 文件
会将文件从暂存区域完全删除,并从磁盘上删除> (工作目录)。要将文件保留在工作目录中,您可以使用git rm --cached
。
但是 git rm --cached asd 和 git reset head -- asd 之间到底有什么区别呢?
git rm
will remove entries from the
staging area. This is a bit different
fromgit reset HEAD
which "unstages"
files. By "unstage" I mean it reverts
the staging area to what was there
before we started modifying things.git rm
on the other hand just kicks
the file off the stage entirely, so
that it's not included in the next
commit snapshot, thereby effectively
deleting it.By default, a
git rm file
will remove the file from the staging area entirely and also off your disk > (the working directory). To leave the file in the working directory, you can usegit rm --cached
.
But what exactly is the difference between git rm --cached asd
and git reset head -- asd
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
比如说,文件可以位于三个地方——(提交的)树、索引和工作副本。当您仅将文件添加到文件夹时,您就是将其添加到工作副本。
当您执行诸如 git add file 之类的操作时,您会将其添加到索引中。当您提交它时,您也将它添加到树中。
它可能会帮助您了解 git reset 中的三个更常见的标志:
现在,当您执行诸如 git reset HEAD 之类的操作时,您实际上在做的是 git reset HEAD --mixed ,它会将索引“重置”到原来的状态在开始添加文件/添加对索引的修改之前(通过 git add )。在这种情况下,无论工作副本的状态是什么,您都没有对其进行任何更改,但您以现在与树的 HEAD 同步的方式更改了索引。 无论
git add
用于暂存先前提交但已更改的文件,还是添加新的(先前未跟踪的)文件,git reset HEAD
与git add
.git rm
另一方面,从工作目录和索引中删除文件,当您提交时,该文件将从中删除树也是如此。但是, git rm --cached 会单独从索引中删除文件并将其保留在工作副本中。在这种情况下,如果文件之前已提交,那么您使索引与树的 HEAD 和工作副本不同,因此 HEAD 现在具有文件的先前提交版本,索引根本没有文件,并且工作副本具有该文件的最后修改。现在提交将同步索引和树,并且文件将从树中删除(使其在工作副本中不被跟踪)。 当使用git add
添加新的(之前未跟踪的)文件时,git rm --cached
与git add
完全相反code> (与git reset HEAD
几乎相同)。Git 2.25 针对这些情况引入了一个新命令,
git Restore
,但从 Git 2.28 开始它在手册页中被描述为“实验性”,即行为可能会改变。There are three places where a file, say, can be - the (committed) tree, the index and the working copy. When you just add a file to a folder, you are adding it to the working copy.
When you do something like
git add file
you add it to the index. And when you commit it, you add it to the tree as well.It will probably help you to know the three more common flags in
git reset
:Now, when you do something like
git reset HEAD
, what you are actually doing isgit reset HEAD --mixed
and it will "reset" the index to the state it was before you started adding files / adding modifications to the index (viagit add
). In this case, no matter what the state of the working copy was, you didn't change it a single bit, but you changed the index in such a way that is now in sync with the HEAD of the tree. Whethergit add
was used to stage a previously committed but changed file, or to add a new (previously untracked) file,git reset HEAD
is the exact opposite ofgit add
.git rm
, on the other hand, removes a file from the working directory and the index, and when you commit, the file is removed from the tree as well.git rm --cached
, however, removes the file from the index alone and keeps it in your working copy. In this case, if the file was previously committed, then you made the index to be different from the HEAD of the tree and the working copy, so that the HEAD now has the previously committed version of the file, the index has no file at all, and the working copy has the last modification of it. A commit now will sync the index and the tree, and the file will be removed from the tree (leaving it untracked in the working copy). Whengit add
was used to add a new (previously untracked) file, thengit rm --cached
is the exact opposite ofgit add
(and is pretty much identical togit reset HEAD
).Git 2.25 introduced a new command for these cases,
git restore
, but as of Git 2.28 it is described as “experimental” in the man page, in the sense that the behavior may change.也许一个例子会有所帮助:
vs
请注意,如果您没有更改任何其他内容,则第二次提交实际上不会执行任何操作。
Perhaps an example will help:
versus
Note that if you haven't changed anything else, the second commit won't actually do anything.
git rm --cached file
将从舞台中删除文件。也就是说,当您提交时该文件将被删除。 git reset HEAD -- file 只会将暂存区域中的文件重置为 HEAD 提交时的状态,即撤消自上次提交以来对其所做的任何更改。如果该更改恰好是新添加文件,那么它们将是等效的。git rm --cached file
will remove the file from the stage. That is, when you commit the file will be removed.git reset HEAD -- file
will simply reset file in the staging area to the state where it was on the HEAD commit, i.e. will undo any changes you did to it since last commiting. If that change happens to be newly adding the file, then they will be equivalent.