Git 提交针对没有分支的标签
如果我在没有创建分支的情况下查看源代码的标记版本,Git 会指示我根本不与任何分支关联。 不过,很高兴让我进行更改并检查它们。 这些变化去哪里了? 如果我切换回“master”,它们就会消失(被 master 中的内容覆盖),而且我似乎无法再次找到它们。 是什么赋予了? 如果 Git 允许我针对本质上是匿名的分支提交更改,我肯定可以取回它们吗?
If I check out a tagged version of my source code without creating a branch, Git indicates that I'm not associated with any branch at all. It's happy to let me make changes and check them in though. Where do those changes go? If I switch back to 'master' they disappear (overwritten by what was in master) and I can't seem to find them again. What gives? If Git lets me commit changes against what's essentially an anonymous branch, surely I can get them back?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您的提交不在任何分支上,因此您无法在工作目录中看到它,除非您使用其 SHA1 签出该特定提交。 您可以通过查看
reflog
来找到提交,该日志跟踪您从存储库中签出的内容的更改。 如果您的标签是XXX
,您将看到类似以下内容:这告诉您必须
checkout
才能在工作目录中查看您的提交。一开始这一切对我来说有点奇怪,直到我意识到 git
checkout
将所有项目文件作为特定提交放入我的文件系统(工作目录)中。 实际上,工作目录充当本地 Git 存储库上的浏览器。 因此,您的更改并未在存储库中被覆盖,只是当您签出主版本时,它们没有显示在您的工作目录中。Because your commit isn't on any branch, you can't see it in the working directory unless you checkout that specific commit, using its SHA1. You can find the commit by looking at the
reflog
which tracks changes in what you have checked out from the repo. If your tag wasXXX
you'll see something like:That tells you the SHA1 that you would have to
checkout
in order to see your commit in the working directory.This all seemed a little weird to me at first, until I realized that git
checkout
places all the project files as of a particular commit into my file system (working directory). In effect, the working directory acts as a browser on the local Git repository. So your changes haven't been overwritten in the repository, they're just not being shown in your working directory when you've checked out the master.是的,它们会出现在转发日志中。
您可以随时为分支命名,如下所示:
Yes, they'll be in reflogs.
You can name the branch at any time like this:
或者,您可以通过查找其 SHA1(如上所述使用 git reflog)将提交合并回 master,而无需创建新分支,然后:
Alternatively, you can merge the commit back into master without a new branch by finding its SHA1 (using git reflog as above) and then:
要回答第二个问题,您可以使用 git reset --hard yourtagname
至于会发生什么,您基本上在标记名处分叉了分支并保留在同一分支上。 您在旧分叉中的提交仍然存在......它们只是很难看到。 您可能必须使用 reflog 来找到旧的分叉。
To answer the second question you'd use git reset --hard yourtagname
As for what would happen you essentially forked your branch at the tagname and stayed on the same branch. Your commits in the old fork are still there... they're just hard to see. You might have to use the reflog to find the old fork.