Git:如果我一直在分离的头状态下进行多次提交,那么我会因为退出而失去任何东西吗?
不久前,我检查了以前的提交,然后再次检查了我最近的提交。我当时并没有意识到这一点,但这让我处于一种超然的头脑状态。从那时起我已经完成了多次提交,并且仍然处于分离的头部状态。我知道我可以通过执行 git checkout
A while ago I checked out a previous commit, then checked out my most recent commit again. I didn't realize this at the time, but that left me in a detached head state. I've done multiple commits since then, and am still in the detached head state. I know I can get out of this state by doing git checkout <branch name>
, but if I do this will I lose anything?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您将丢失在分离头模式下所做的所有提交,因为没有分支名称会指向它们。 (您不会立即完全丢失它们,但它们将被销毁,并且访问它们将变得更加困难。)
因此,只需先创建一个分支,以保留它们并为您提供引用它们的方法。
现在可以安全地签出
branchname
。但实际上,在您的情况下,由于您的 HEAD 实际上位于您希望分支所在的位置,因此您可以将分支名称移动到您现在所在的位置。根本不需要
temp
。Yes, you will lose all the commits you've made in detached head mode, because no branch name will be pointing to them. (You won't totally lose them immediately, but they will be slated for destruction, and accessing them will become more difficult.)
So simply create a branch first, to retain them and give you a way to refer to them.
Now it's safe to checkout
branchname
.Actually in your case, though, since your HEAD is in fact where you wish your branch was, you can just move your branch name to where you are now instead. No need for
temp
at all.这就是为什么我提到最近的(Git 2.23 ,2019 年第 3 季度)
git切换
命令。您无法使用 git switch并且没有意识到您最终会处于分离的 HEAD 状态,因为这样的命令实际上会失败,错误消息:
请参阅“为什么我的 Git 存储库进入分离 HEAD 状态?”。
使用
--detach
选项的提示来自 Git 2.36(2022 年第 2 季度)。使用 Git 2.45(2024 年第 2 季度),第 18 批,
git checkout/switch --detach foo
(man),之后切换到分离的 HEAD 状态,提供了“foo
”分支的跟踪信息,这是毫无意义的。请参阅 提交 b9f2e1a(2024 年 3 月 30 日),作者:Junio C Hamano (
gitster
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 847af43,2024 年 4 月 12 日)That is why I mention the more recent (Git 2.23, Q3 2019)
git switch
command.You cannot use
git switch <commit>
and not realize you end up in a detached HEAD state because such a command would actually fail, with the error message:See more at "Why did my Git repo enter a detached HEAD state?".
The hint to use the
--detach
option comes from Git 2.36 (Q2 2022).With Git 2.45 (Q2 2024), batch 18,
git checkout/switch --detach foo
(man), after switching to the detached HEAD state, gave the tracking information for the 'foo
' branch, which was pointless.See commit b9f2e1a (30 Mar 2024) by Junio C Hamano (
gitster
).(Merged by Junio C Hamano --
gitster
-- in commit 847af43, 12 Apr 2024)