Git提交没有分支的标签 [英] Git commit against tag with no branch

查看:559
本文介绍了Git提交没有分支的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在不创建分支的情况下检出我的源代码的标记版本,Git表明我根本没有与任何分支相关联。很高兴让我进行修改并检查它们。这些变化发生在哪里?如果我切换回主人,他们会消失(被主人的内容覆盖),我似乎无法再找到他们。是什么赋予了?如果Git允许我针对本质上是匿名的分支提交更改,那么我肯定可以将它们还原? 因为您的提交不在任何分支上,除非您使用SHA1检出特定提交,否则无法在工作目录中看到它。您可以通过查看 reflog 来查找提交,它跟踪您从回购中签出的内容中的更改。如果您的标签 XXX ,您会看到如下所示的内容:

  $ git reflog 
7a30fd7 ... HEAD @ {0}:结账:从主人移动到XXX
ddf751d ... HEAD @ {1}:结帐:从96c3b0300ccf16b64efc260c21c85ba9030f2e3a移动到主
96c3b03。 HEAD @ {2}:commit:标记为XXX的示例提交,不在任何分支上
7a30fd7 ... HEAD @ {3}:结帐:从主移动到XXX

这就告诉你SHA1你必须 checkout 才能看到你的提交在工作目录中。

  $ git checkout 96c3b03 
注意:移动到96c3b03,这不是本地分支
如果你想从这个checkout创建一个新的分支,你可以用
(现在或以后)再次使用-b和checkout命令。例如:
git checkout -b< new_branch_name>
HEAD现在在96c3b03 ...标签XXX上的示例提交,不在任何分支上
$ git checkout -b新分支
$ git分支#列出所有分支
特性1
master
* newbranch

这一切对我来说似乎有点奇怪,我意识到git checkout 将所有项目文件作为特定提交放入我的文件系统(工作目录)中。实际上,工作目录充当本地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?

解决方案

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.

这篇关于Git提交没有分支的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆