当前不在任何分支上 + git commit + checkout 当不在任何分支中时。我是否丢失了更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6048425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:57:32  来源:igfitidea点击:

Not currently on any branch + git commit + checkout when not in any branch. Did i loose my changes?

gitcommit

提问by Sewdn

I was not currently on any branch when I commited my changes. I didn't really notice the message and checked out another branch.

当我提交更改时,我目前不在任何分支上。我并没有真正注意到该消息并检查了另一个分支。

How can I retrieve my changes? I can't merge or checkout, since there is no branch to merge from.

如何检索我的更改?我无法合并或结帐,因为没有要合并的分支。

回答by manojlds

You can use git reflogto get the commit hash of the commit that you did while in "no branch" ( a detached HEAD) and merge that in to the branch that you are currently in ( master maybe)

您可以使用git reflog获取您在“无分支”(分离的 HEAD)中所做的提交的提交哈希,并将其合并到您当前所在的分支(可能是 master)

Something like git merge HEAD@{1}

就像是 git merge HEAD@{1}

You can also git rebase -iand "pick" the commit you want from the reflog.

您还可以git rebase -i从 reflog 中“选择”您想要的提交。

回答by Kel Solaar

I was in a similar state after committing some work:

做了一些工作后,我也处于类似的状态:

Lilith:Manager KelSolaar$ git status

Not currently on any branch.

莉莉丝:经理 KelSolaar$ git status

目前不在任何分支上。

I issued a git logto see my last commit hash:

我发布了一个git log来查看我的最后一次提交哈希:

Lilith:Manager KelSolaar$ git log

commit 49984303037e970d637161c3154b7fd7d6ae3a43 Author: KelSolaar Date: Wed Oct 5 22:41:31 2011 +0100

Introduce new "QObject" components category and rename existing ones to "Def

莉莉丝:经理 KelSolaar$ git log

提交 49984303037e970d637161c3154b7fd7d6ae3a43 作者:KelSolaar 日期:2011 年 10 月 5 日星期三 22:41:31 +0100

Introduce new "QObject" components category and rename existing ones to "Def

I then checked out my master branch:

然后我检查了我的主分支:

Lilith:Manager KelSolaar$ git checkout master

Previous HEAD position was 4998430... Introduce new "QObject" components categorie and rename exising ones to "Default" and "QWidget".

Switched to branch 'master'

Lilith:经理 KelSolaar$ git checkout master

以前的 HEAD 位置是 4998430... 引入新的“QObject”组件类别并将现有组件重命名为“默认”和“QWidget”。

切换到分支“master”

And I finally merged using the commit hash:

我最终使用提交哈希合并:

Lilith:Manager KelSolaar$ git merge 49984303037e970d637161

Updating 141bc69..4998430

Fast-forward

src/manager/component.py | 2 +-

...

莉莉丝:经理 KelSolaar$ git merge 49984303037e970d637161

更新 141bc69..4998430

快进

src/manager/component.py | 2 +-

...

回答by Max Nanasy

git checkout -will switch you back to the previous branch:

git checkout -将您切换回上一个分支:

Thu Feb 21 12:50 AM /src/test ((08f84f4...)) $ git checkout master

Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  08f84f4 Fix everything

Switched to branch 'master'

Thu Feb 21 12:50 AM /src/test (master) $ git checkout -

HEAD is now at 08f84f4... Fix everything

Thu Feb 21 12:50 AM /src/test ((08f84f4...)) $

回答by CharlesB

Your commit isn't gone, you can recover by asking git to show you the hidden commits, and put them back in a temporary branch.

你的提交没有消失,你可以通过让 git 向你显示隐藏的提交来恢复,并将它们放回临时分支。

See this answerfor instructions.

有关说明,请参阅此答案

回答by karmakaze

Use "git reflog" it shows the commit hashes of the results of your git command history. You can then "git co hash" and when you've found the right one, set/make a branch for it.

使用“git reflog”它会显示你的 git 命令历史结果的提交哈希。然后你可以“git co hash”,当你找到正确的时,为它设置/创建一个分支。

回答by Dan Ray

You're never "not on any branch". You might have been on the branch called master, but you WERE on a branch when you committed. So that commit is SOMEWHERE.

你永远不会“不在任何分支上”。您可能已经在名为 的分支上master,但是当您提交时,您在一个分支上。所以那个提交是某处。

Use git logto look at history. You can use git resetto go back in time (including, optionally, leaving the changes in your working directory).

使用git log来看待历史。您可以使用git reset回到过去(包括,可选地,将更改保留在您的工作目录中)。