git Reset 文件和 git checkout 文件有什么区别?

发布于 2024-11-27 20:26:23 字数 219 浏览 1 评论 0原文

为什么 git 允许我重置文件?我以为我理解了重置,从某种意义上说,它正在移动头部……显然我错了。

因此,git Reset sha file似乎与git checkout sha file做相同的事情,除了我在索引和中看到file工作目录。

这对我来说没有意义。有人可以解释一下区别吗?

Why is it that git allows me to reset a file? I thought I understood reset, in the sense that it was moving the HEAD ... clearly I was wrong.

So, git reset sha file seems to do the same as git checkout sha file, with the exception that I see file in the index and in the working directory.

It doesn't make sense to me. Can someone please explain the difference?

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

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

发布评论

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

评论(2

画尸师 2024-12-04 20:26:23

tl;dr git reset COMMIT FILE 仅更改索引,git checkout COMMIT FILE 将更改索引和工作树。

git reset 有非常重要的标志 --soft--hard--mixed (和 >--keep--merge )

http://git-scm.com/docs/git-reset

--mixed 是默认值,当您执行 git reset sha file 时您正在进行混合重置,其中:

--混合

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

就像上面所说的那样,这种情况下的重置根本不会影响你的工作树,只有索引中的版本会重置为 sha 中的版本。

另一方面,git checkout

当给出 或 --patch 时,git checkout 不会切换
分支机构。它从以下位置更新工作树中的命名路径
索引文件或来自命名文件(通常是提交)。

因此,当您执行 git checkout 时,您将丢失文件中的更改,并且它将被 sha 中文件版本中的任何内容替换,而当您执行混合重置时,只有您的索引会被替换。被重置,您的工作目录仍将有更改,您可以稍后根据需要再次进行更改。

tl;dr git reset COMMIT FILE changes only index, git checkout COMMIT FILE will change both index and working tree.

git reset has very important flags of --soft, --hard and --mixed ( and --keep and --merge )

http://git-scm.com/docs/git-reset

--mixed is the default and when you do git reset sha file you are doing mixed reset whereby:

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

Like it says above, the reset in this case would not touch your working tree at all and only the version in the index is reset to the one in the sha.

git checkout on the other hand:

When or --patch are given, git checkout does not switch
branches. It updates the named paths in the working tree from the
index file or from a named (most often a commit).

So when you do git checkout you will lose the changes in the file and it will be replaced with whatever was there in the version of file in sha, whereas when you do the mixed reset, only your index will be reset and your working directory will still have the changes which you can later stage again as needed.

蒗幽 2024-12-04 20:26:23
git checkout

恢复对文件的更改。

git reset

从暂存区域中删除文件,但保留更改。

git checkout

Reverts changes to the file.

git reset

Removes the file from the staging area, but keeps the changes.

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