git Git针对没有分支的标签提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/366093/
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
Git commit against tag with no branch
提问by Andrew
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?
如果我在没有创建分支的情况下检出源代码的标记版本,Git 会表明我根本没有与任何分支相关联。很高兴让我进行更改并检查它们。这些变化去哪里了?如果我切换回“master”,它们就会消失(被 master 中的内容覆盖),而且我似乎再也找不到它们了。是什么赋予了?如果 Git 允许我针对本质上是匿名的分支提交更改,我肯定可以将它们取回吗?
回答by Paul
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:
因为您的提交不在任何分支上,所以除非您使用其 SHA1 签出该特定提交,否则您无法在工作目录中看到它。您可以通过查看reflog
which 跟踪您从 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
That tells you the SHA1 that you would have to checkout
in order to see your commit in the working directory.
这会告诉您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
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 commitinto 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.
起初这一切对我来说似乎有点奇怪,直到我意识到 gitcheckout
将特定提交的所有项目文件放入我的文件系统(工作目录)中。实际上,工作目录充当本地 Git 存储库上的浏览器。因此,您的更改并没有在存储库中被覆盖,只是在您签出 master 时它们没有显示在您的工作目录中。
回答by Dustin
Yes, they'll be in reflogs.
是的,他们会在 reflogs 中。
You can name the branch at any time like this:
您可以随时为分支命名,如下所示:
git checkout -b my-branch-name
回答by gjvis
Alternatively, you can merge the commit back into master without a new branch by finding its SHA1 (using git reflog as above) and then:
或者,您可以通过查找其 SHA1(使用 git reflog 如上所述)将提交合并回 master 而没有新分支,然后:
git checkout master
git merge SHA1
回答by Clintm
To answer the second question you'd use git reset --hard yourtagname
要回答第二个问题,您将使用 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.
至于会发生什么,你基本上在标记名处分叉了你的分支并留在同一个分支上。您在旧叉中的提交仍然存在……只是很难看到。您可能必须使用 reflog 来查找旧叉。