问题描述
来自Git手册:
似乎只有一个选择:HEAD已分离.我可以指定不让HEAD脱离的选项吗?
It seems there is only one option: HEAD detached.Can I specify an option not let HEAD detached?
[升级1]我在帖子"将Git存储库还原为上一个提交"
[Upgrade 1]I asked a similar question in the comment in the post "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
在当前目录中从0d1d7fc32签出状态,但是 将HEAD留在原处.和使用git之类的东西一样 签出其他分支路径/到/文件-这将使您留在自己的 当前分支,只需签出给定的文件.在这种情况下,我们 检出整个当前目录,而不是仅检出一个文件, 但仍不更改HEAD. – Jefromi
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不被附加.
So it seems we can let HEAD not attached by adding "."
[更新2]从下面的答案和评论中,我认为一个不错的选择是:git reset --hard
[Update 2] From the answers and comments below, I think a neat one is:git reset --hard
但是现在我的问题是:Jefromi的方法正确吗?
But then now my question is:Is Jefromi's method correct?
git checkout 0d1d7fc32 .
git commit
有关他的方法的更多详细信息,请参见上面的链接.
See the above link for more details of his method.
推荐答案
如果您要签出不是任何分支的最后一个提交的提交,从根本上不可能在同时具有HEAD
引用分支.
If you want to check out a commit that isn't the last commit of any branch, it's fundamentally impossible to check that out while at the same time having HEAD
refer to a branch.
您可以创建或更新分支以指向该特定的提交,如注释中所述,使用git checkout
的-b
选项(或使用-B
覆盖现有分支),或者如果已经在要指向该特定提交的分支上,可以使用git reset --hard <commit>
更新它并同时签出.
You can create or update a branch to point to that specific commit, as indicated in the comments using git checkout
's -b
option (or -B
to overwrite an existing branch), or if you're already on a branch that you want to have point to that specific commit, you can update it and check out at the same time using git reset --hard <commit>
.
这篇关于Git:我可以在不分离HEAD的情况下签出提交吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!