gpt4 book ai didi

git - 如何一步从 git 索引中删除修改过的文件并丢弃修改?

转载 作者:太空狗 更新时间:2023-10-29 14:47:36 25 4
gpt4 key购买 nike

我最近对错误的分支进行了草率的大提交。我决定我希望将更改提交到不同的分支上,并可能将大提交拆分为几个较小的提交。我按照 this answer 的指示执行了 git cherry-pick -n .这导致了我当前分支中的一系列阶段性更改。我知道我不需要这些改变中的任何一个。所以我想知道:有没有什么办法可以从索引中删除修改后的文件并在一步中丢失更改?我见过的大多数解决方案都需要 git reset,然后是 git checkout -- path/to/file

最佳答案

我最后做的是 git checkout HEAD -- path/to/file,据我所知,它用 HEAD 提交树中的文件替换了文件,而不是 checkin 首先是索引。

我没有引用 git 手册页就想到了这个解决方案,但感谢@torek 指出了 git-checkout(1)手册页。这是我使用的第五种形式的 git checkout,如下所示:

git checkout [-p|--patch] [<tree-ish>] [--] <pathspec>...

该表格的部分文档说:

The <tree-ish> argument can be used to specify a specific tree-ish (i.e. commit,
tag or tree) to update the index for the given paths before updating the working
tree.

所以实际发生的事情是这样的:git 用 HEAD 中的版本替换了索引中的文件(有效地从索引中删除了文件),然后用索引中的版本替换了工作树中的文件,消除了更改.

关于git - 如何一步从 git 索引中删除修改过的文件并丢弃修改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17891521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com