如果我在没有创建分支的情况下签出源代码的标记版本,Git 会表明我根本没有与任何分支相关联。很高兴让我进行更改并签入。这些变化去哪儿了?如果我切换回“主”,它们就会消失(被主中的内容覆盖),我似乎再也找不到它们了。是什么赋予了?如果 Git 允许我对本质上是匿名的分支提交更改,我肯定可以将它们取回吗?
Andrew
问问题
21878 次
4 回答
79
因为您的提交不在任何分支上,所以您无法在工作目录中看到它,除非您使用其 SHA1 签出该特定提交。reflog
您可以通过查看您从 repo 中签出的内容中的哪些跟踪更改来找到提交。如果您的标签是,XXX
您会看到如下内容:
$ git reflog
7a30fd7... HEAD@{0}: checkout: moving from master to XXX
ddf751d... HEAD@{1}: checkout: moving from 96c3b0300ccf16b64efc260c21c85ba9030f2e3a to master
96c3b03... HEAD@{2}: commit: example commit on tag XXX, not on any branch
7a30fd7... HEAD@{3}: checkout: moving from master to XXX
这告诉您checkout
为了在工作目录中看到您的提交,您必须使用 SHA1。
$ git checkout 96c3b03
Note: moving to "96c3b03" which isn't a local branch
If you want to create a new branch from this checkout, you may do so
(now or later) by using -b with the checkout command again. Example:
git checkout -b <new_branch_name>
HEAD is now at 96c3b03... example commit on tag XXX, not on any branch
$ git checkout -b newbranch
$ git branch #lists all branches
feature1
master
* newbranch
起初这一切对我来说似乎有点奇怪,直到我意识到 gitcheckout
将所有项目文件作为特定提交放入我的文件系统(工作目录)中。实际上,工作目录充当本地 Git 存储库上的浏览器。因此,您的更改并没有在存储库中被覆盖,当您签出主文件时,它们只是没有显示在您的工作目录中。
于 2008-12-14T03:47:18.000 回答
8
是的,他们将在 reflogs 中。
您可以随时像这样命名分支:
git checkout -b my-branch-name
于 2008-12-14T02:49:34.527 回答
5
或者,您可以通过查找其 SHA1(如上使用 git reflog )将提交合并回 master 而无需新分支,然后:
git checkout master
git merge SHA1
于 2012-01-06T16:08:21.700 回答
0
要回答第二个问题,您可以使用 git reset --hard yourtagname
至于会发生什么,你基本上在标记名处分叉了你的分支并留在同一个分支上。您在旧分叉中的提交仍然存在......他们很难看到。您可能必须使用 reflog 来查找旧分叉。
于 2010-06-04T22:31:52.840 回答