“git rm --cached x”与“git reset head --?x”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5798930/
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
"git rm --cached x" vs "git reset head --? x"?
提问by Pacerier
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
.
git rm
将从暂存区中删除条目。这与git reset HEAD
“取消暂存”文件有点不同。“取消暂存”是指将暂存区恢复到我们开始修改之前的状态。git rm
另一方面,只是将文件完全踢出舞台,这样它就不会包含在下一个提交快照中,从而有效地删除它。默认情况下, a
git rm file
将从暂存区中完全删除文件,并从磁盘 > (工作目录)中删除。要将文件保留在工作目录中,您可以使用git rm --cached
.
But what exactly is the difference between git rm --cached asd
and git reset head -- asd
?
但是git rm --cached asd
和之间究竟有什么区别git reset head -- asd
?
回答by manojlds
There are three places where a file, say, can be - the 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.
当您执行类似操作时git add file
,将其添加到索引中。当你提交它时,你也将它添加到树中。
It will probably help you to know the three more common flags in git reset:
它可能会帮助您了解 git reset 中三个更常见的标志:
git reset [--
<mode>
] [<commit>
]This form resets the current branch head to
<commit>
and possibly updates the index (resetting it to the tree of<commit>
) and the working tree depending on<mode>
, which must be one of the following:
--softDoes not touch the index file nor the working tree at all (but resets the head to
<commit>
, just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it.--mixed
Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.
--hard
Resets the index and working tree. Any changes to tracked files in the working tree since
<commit>
are discarded.
git 重置 [--
<mode>
] [<commit>
]此形式将当前分支头重置为
<commit>
并可能更新索引(将其重置为 的树<commit>
)和工作树取决于<mode>
,它必须是以下之一:--
soft根本不接触索引文件或工作树(但将头部重置为
<commit>
,就像所有模式一样)。这会留下所有更改的文件“要提交的更改”,正如 git status 所说的那样。--混合
重置索引但不重置工作树(即,更改的文件被保留但未标记为提交)并报告尚未更新的内容。这是默认操作。
- 难的
重置索引和工作树。此后对工作树中跟踪文件的任何更改
<commit>
都将被丢弃。
Now, when you do something like git reset HEAD
- what you are actually doing is git reset HEAD --mixed
and it will "reset" the index to the state it was before you started adding files / adding modifications to the index ( via git add
) In this case, the working copy and the index ( or staging ) were in sync, but you made the HEAD and the index to be in sync after the reset.
现在,当您执行类似操作时git reset HEAD
- 您实际上正在做的是git reset HEAD --mixed
,它将索引“重置”到您开始添加文件/向索引添加修改之前的状态(通过git add
)在这种情况下,工作副本和索引(或 staging )是同步的,但是您使 HEAD 和索引在重置后同步。
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 index alone and keeps it in your working copy. This is the exact opposite of git add file
In this case, you made index to be different from the HEAD and the working, in it that the HEAD has the previously committed version of the file, working copy had the last modification if any or content from HEAD of the file and you removed the file from the index. A commit now will sync the index and tree and the file will be removed.
git rm
另一方面,从工作目录和索引中删除一个文件,当您提交时,该文件也会从树中删除。git rm --cached
但是仅从索引中删除文件并将其保留在您的工作副本中。这与在这种情况下完全相反git add file
,您使索引与 HEAD 和工作不同,因为 HEAD 具有文件的先前提交版本,工作副本具有最后修改(如果有的话)或来自 HEAD 的内容文件,然后您从索引中删除了该文件。现在提交将同步索引和树,文件将被删除。
回答by Greg Hewgill
Perhaps an example will help:
也许一个例子会有所帮助:
git rm --cached asd
git commit -m "the file asd is gone from the repository"
versus
相对
git reset HEAD -- asd
git commit -m "the file asd remains in the repository"
Note that if you haven't changed anything else, the second commit won't actually do anything.
请注意,如果您没有更改任何其他内容,则第二次提交实际上不会执行任何操作。
回答by yuriks
git rm --cached file
will removethe 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.
git rm --cached file
将从舞台上删除文件。也就是说,当您提交时,文件将被删除。git reset HEAD -- file
将简单地将暂存区中的文件重置为它在 HEAD 提交时的状态,即,将撤消自上次提交以来对它所做的任何更改。如果该更改恰好是新添加的文件,那么它们将是等效的。