gpt4 book ai didi

Git:我可以在不分离 HEAD 的情况下检查提交吗?

转载 作者:行者123 更新时间:2023-12-04 20:40:26 26 4
gpt4 key购买 nike

从 Git 手册:

git checkout [--detach] <commit> Prepare to work on top of <commit>, by detaching HEAD at it (see "DETACHED HEAD" section), and updating the index and the files in the working tree. Local modifications to the files in the working tree are kept, so that the resulting working tree will be the state recorded in the commit plus the local modifications.



似乎只有一种选择:HEAD 分离。
我可以指定一个不让 HEAD 分离的选项吗?

【升级1】
我在帖子“ Revert Git repo to a previous commit”的评论中问了一个类似的问题
# To get just one, you could use `rebase -i` to squash them afterwards
# Or, you could do it manually (be sure to do this at top level of the repo)
# get your index and work tree into the desired state, without changing HEAD:
git checkout 0d1d7fc32 .

# Then commit. Be sure and write a good message describing what you just did
git commit

My Question to him: @Jefromi, after "git checkout 0d1d7fc32 .", is the HEAD already detached? Then "git commit" cannot do anything with the dangled HEAD.

to check out the state from 0d1d7fc32 in the current directory, but it leaves HEAD where it was. It's the same as using something like git checkout other-branch path/to/file - that will leave you on your current branch, and just check out the given file. In this case we're checking out the entire current directory instead of just one file, but still not changing HEAD. – Jefromi



所以看起来我们可以通过添加“.”来让 HEAD 不被附加。

[更新 2] 从下面的答案和评论中,我认为一个简洁的是:
git reset --hard

但现在我的问题是:
Jefromi 的方法是否正确?
git checkout 0d1d7fc32 .
git commit

有关他的方法的更多详细信息,请参阅上面的链接。

最佳答案

如果你想检查一个不是任何分支的最后一次提交的提交,基本上不可能在同时拥有 HEAD 的同时检查它。指一个分支。

您可以创建或更新一个分支以指向该特定提交,如使用 git checkout 的注释所示。的 -b选项(或 -B 覆盖现有分支),或者如果您已经在要指向该特定提交的分支上,您可以更新它并同时使用 git reset --hard <commit> checkout .

关于Git:我可以在不分离 HEAD 的情况下检查提交吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33656072/

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