在分离的 HEAD 状态下创建的 git 提交会发生什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9984223/
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
What happens to git commits created in a detached HEAD state?
提问by Mausimo
This is what happened:
这是发生的事情:
I have a branch A. On branch A I committed a bunch of changes. I was not happy with the code, so I checked out the previous commit in branch A. I then made a bunch more changes and committed them on branch A. Now I can not find this commit anywhere. Did I lose this code?
我有一个分支 A。在分支 AI 上提交了一系列更改。我对代码不满意,所以我检查了分支 A 中的先前提交。然后我做了更多更改并将它们提交到分支 A。现在我在任何地方都找不到这个提交。我丢失了这个代码吗?
回答by Dietrich Epp
The old commit is still in the reflog.
旧的提交仍在 reflog 中。
git reflog
This will show a list of commits, and the "lost" commit should be in there. You can make it into a new branch. For example, if the SHA-1 is ba5a739, then you can make a new branch named "new-branch" at the old commit with:
这将显示一个提交列表,“丢失”的提交应该在那里。你可以把它变成一个新的分支。例如,如果 SHA-1 是 ba5a739,那么您可以在旧提交中创建一个名为“new-branch”的新分支:
git branch new-branch ba5a739
Note that "lost" commits will get deleted when the database is pruned.
请注意,当数据库被修剪时,“丢失”的提交将被删除。
回答by tanius
Your commits are still available in the reflog, as pointed out already. In addition to the other answers, here is a way to take over the detached HEAD commits into your current branch directly, without creating and merging a new branch:
正如已经指出的那样,您的提交在 reflog 中仍然可用。除了其他答案之外,这里还有一种方法可以将分离的 HEAD 提交直接接管到当前分支中,而无需创建和合并新分支:
Look up the SHA-1 hashes of the commits you made in detached HEAD state with
git reflog
Then execute, with all the commit hashes ordered from oldest to most recent:
git cherry-pick <hash1> <hash2> <hash3> ...
For example if I had only one, given in the "first 7 characters" short hash format:
git cherry-pick a21d053
查找您在分离的 HEAD 状态下所做的提交的 SHA-1 哈希值
git reflog
然后执行,所有提交哈希从最旧到最新排序:
git cherry-pick <hash1> <hash2> <hash3> ...
例如,如果我只有一个,以“前 7 个字符”短散列格式给出:
git cherry-pick a21d053
This will create new commits to your current branch, one commit per detached-HEAD-commit hash that you mention in the command. It also takes over the original commit messages.
这将为您的当前分支创建新提交,您在命令中提到的每个 detached-HEAD-commit 哈希提交一次。它还接管原始提交消息。
回答by sergtk
You may find lost (dangling) commits with the following command:
您可能会使用以下命令找到丢失(悬空)的提交:
git fsck --lost-found
Note, if your current head is dangling commit, it is not listed as lost.
请注意,如果您当前的 head 是悬空提交,则不会将其列为丢失。
You may find more info at git-fsck(1) Manual Page
您可以在git-fsck(1) 手册页找到更多信息
Then you may create branch on that lost commit:
然后你可以在丢失的提交上创建分支:
git branch new-branch ba5a739
回答by Greg Bacon
Git parlance for the state of your working directory is a “detached HEAD.” Here is another place that git reflog
makes the save.
Git 对工作目录状态的说法是“分离的 HEAD”。这是另一个git reflog
可以保存的地方。
$ git reflog
0b40dd6 HEAD@{0}: commit: my commit on detached HEAD
...
If I try to checkout a different branch, git-1.7.5.1 gives a helpful suggestion.
如果我尝试签出不同的分支,git-1.7.5.1 会提供有用的建议。
$ git checkout master Warning: you are leaving 1 commit behind, not connected to any of your branches: 0b40dd6 my commit on detached HEAD If you want to keep them by creating a new branch, this may be a good time to do so with: git branch new_branch_name 0b40dd65c06bb215327863c2ca10fdb4f904215b Switched to branch 'master'
回答by Greg Hewgill
You did not lose it, Git still keeps a copy (but it is currently unreachable by any branch head). You can find your missing commit using the git reflog
command. The reflog keeps track of the historical positions of a branch head, and you can use it to find things that the branch head was pointing at previously.
你没有丢失它,Git 仍然保留一个副本(但目前任何分支头都无法访问它)。您可以使用该git reflog
命令找到丢失的提交。reflog 会跟踪分支负责人的历史位置,您可以使用它来查找分支负责人之前指向的内容。
回答by user1520615
Follow these steps to link your detached head back to git repo
按照以下步骤将分离的头部链接回 git repo
git checkout "your branch with path but without remote name"
git checkout "your branch with path but without remote name"
e.g. if remote name is origin and branch name is bugfix/somebranch
then use git checkout bugfix/somebranch
例如,如果远程名称是原点,bugfix/somebranch
然后使用分支名称git checkout bugfix/somebranch
git reflog
get the commit SHA's listed from your commit list of detached branch.git cherry-pick "commit hash1" "commit hash2" "commit hash3"
git push
git reflog
从分离分支的提交列表中获取提交 SHA 的列表。git cherry-pick "commit hash1" "commit hash2" "commit hash3"
git push
ALL SET!!
搞定!!
回答by blalond
In Sourcetree, I found that git reflog didn't work, so I figured out how to do this using the GUI.
在 Sourcetree 中,我发现 git reflog 不起作用,所以我想出了如何使用 GUI 来做到这一点。
First, try to find the "lost" commit by looking for a message in the Command History (view:Show Command Output). It'll hopefully be in the command "Switching Branch" after the commit that you lost and you'll see the commit comment with a 1234567 commit ID.
首先,通过在命令历史记录中查找消息来尝试找到“丢失”的提交(查看:显示命令输出)。它有望在您丢失的提交之后出现在“切换分支”命令中,并且您将看到带有 1234567 提交 ID 的提交注释。
Take that Commit ID to next step.
将该提交 ID 带到下一步。
Hit the "Branch" button in the top toolbar and you should get a dialog "New Branch" where you can specify a certain commit. Put that Commit ID in there, specify a new branch name, hit Create Branch and you should get a new branch with your lost commit!
点击顶部工具栏中的“分支”按钮,您应该会看到一个对话框“新分支”,您可以在其中指定某个提交。把那个提交 ID 放在那里,指定一个新的分支名称,点击创建分支,你应该得到一个丢失提交的新分支!
This brought back some lost work for me!
这为我带来了一些丢失的工作!