Git 提交针对没有分支的标签

发布于 2024-07-10 11:18:33 字数 185 浏览 6 评论 0原文

如果我在没有创建分支的情况下查看源代码的标记版本,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

虫児飞 2024-07-17 11:18:33

由于您的提交不在任何分支上,因此您无法在工作目录中看到它,除非您使用其 SHA1 签出该特定提交。 您可以通过查看 reflog 来找到提交,该日志跟踪您从存储库中签出的内容的更改。 如果您的标签是 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 才能在工作目录中查看您的提交。

$ 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

一开始这一切对我来说有点奇怪,直到我意识到 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 was XXX you'll see something like:

$ 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

That tells you the SHA1 that you would have to checkout in order to see your commit in the working directory.

$ 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

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.

绿光 2024-07-17 11:18:33

是的,它们会出现在转发日志中。

您可以随时为分支命名,如下所示:

git checkout -b my-branch-name

Yes, they'll be in reflogs.

You can name the branch at any time like this:

git checkout -b my-branch-name
暮倦 2024-07-17 11:18:33

或者,您可以通过查找其 SHA1(如上所述使用 git reflog)将提交合并回 master,而无需创建新分支,然后:

git checkout master
git merge SHA1

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 checkout master
git merge SHA1
仄言 2024-07-17 11:18:33

要回答第二个问题,您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文