Git:我可以在不分离HEAD的情况下签出提交吗? [英] Git: can I check out a commit without HEAD detached?

查看:114
本文介绍了Git:我可以在不分离HEAD的情况下签出提交吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自Git手册:

git checkout [--detach]< commit>准备在< commit>上工作, 通过分离HEAD(请参阅"DETACHED HEAD"部分)并进行更新 索引和工作树中的文件.本地修改 保留工作树中的文件,以便最终工作 树将是提交中记录的状态加上本地 修改.

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脱离的选项吗?

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

我对他的问题: @Jefromi,在"git checkout 0d1d7fc32"之后,已经是HEAD 超脱?然后,"git commit"对悬挂的HEAD无法执行任何操作.

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

在当前目录中从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的情况下签出提交吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆