“git rm --cached x” vs “git 重置头 --” x”?

发布于 2024-11-03 09:34:51 字数 490 浏览 6 评论 0原文

GitRef.org - 基本

git rm 将从中删除条目 暂存区。这有点不同 来自 git reset HEAD ,它“取消了阶段” 文件。我所说的“unstage”是指它恢复 暂存区到那里的情况 在我们开始修改之前。 另一方面, git rm 就可以了 文件完全退出舞台,所以 它不包含在下一个中 提交快照,从而有效地 删除它。

默认情况下,git rm 文件 会将文件从暂存区域完全删除,并从磁盘上删除> (工作目录)。要将文件保留在工作目录中,您可以使用git rm --cached

但是 git rm --cached asd 和 git reset head -- asd 之间到底有什么区别呢?

GitRef.org - Basic:

git rm will remove entries from the
staging area. This is a bit different
from git 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 use git rm --cached.

But what exactly is the difference between git rm --cached asd and git reset head -- asd?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

记忆で 2024-11-10 09:34:51

比如说,文件可以位于三个地方——(提交的)树、索引和工作副本。当您仅将文件添加到文件夹时,您就是将其添加到工作副本。

当您执行诸如 git add file 之类的操作时,您会将其添加到索引中。当您提交它时,您也将它添加到树中。

它可能会帮助您了解 git reset 中的三个更常见的标志:

git reset [--] []

此表单将当前分支头重置为 并且可能
更新索引(将其重置为树)和
工作树取决于,它必须是其中之一
以下:
--软

根本不触及索引文件或工作树(但会重置
头部到 ,就像所有模式一样)。这让你所有的
更改的文件“要提交的更改”,正如 git status 所说的那样。

--混合

重置索引但不重置工作树(即更改的文件
被保留但未标记为提交)并报告尚未提交的内容
已更新。这是默认操作。

--硬

重置索引和工作树。对跟踪文件的任何更改
起的工作树将被丢弃。

现在,当您执行诸如 git reset HEAD 之类的操作时,您实际上在做的是 git reset HEAD --mixed ,它会将索引“重置”到原来的状态在开始添加文件/添加对索引的修改之前(通过 git add )。在这种情况下,无论工作副本的状态是什么,您都没有对其进行任何更改,但您以现在与树的 HEAD 同步的方式更改了索引。 无论git add用于暂存先前提交但已更改的文件,还是添加新的(先前未跟踪的)文件,git reset HEADgit add.

git rm 另一方面,从工作目录和索引中删除文件,当您提交时,该文件将从中删除树也是如此。但是, git rm --cached 会单独从索引中删除文件并将其保留在工作副本中。在这种情况下,如果文件之前已提交,那么您使索引与树的 HEAD 和工作副本不同,因此 HEAD 现在具有文件的先前提交版本,索引根本没有文件,并且工作副本具有该文件的最后修改。现在提交将同步索引和树,并且文件将从树中删除(使其在工作副本中不被跟踪)。 当使用 git add 添加新的(之前未跟踪的)文件时,git rm --cachedgit 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:

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:
--soft

Does 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.

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, 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. Whether git 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 of git 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). When git add was used to add a new (previously untracked) file, then git rm --cached is the exact opposite of git add (and is pretty much identical to git 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.

忆梦 2024-11-10 09:34:51

也许一个例子会有所帮助:

git rm --cached asd
git commit -m "the file asd is gone from the repository"

vs

git reset HEAD -- asd
git commit -m "the file asd remains in the repository"

请注意,如果您没有更改任何其他内容,则第二次提交实际上不会执行任何操作。

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.

路弥 2024-11-10 09:34:51

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文