简体   繁体   English

当head为*时,如何将更改提交到分支 <tag> )”

[英]How to commit changes to a branch when head is “* (detached from <tag>)”

I want to commit and push changes to remote but I am detached from tag. 我想提交更改并将其推送到远程,但是我与标签分离。 How do I take just the name of the tag and form git commands that reattach the head where it was detached from, and commit and push to remote? 我该如何仅使用标签的名称并形成git命令,以重新附加与之分离的头部,然后提交并推送到远程?

How do I [...] reattach the head where it was detached from [...]? 我该如何将头从[...]拆下,重新安装?

The output of git branch being git branch的输出为

* (detached from <tag>)
  <possibly other branches...>

indicates that you've run git checkout <tag> . 表示您已经运行了git checkout <tag> Your situation is something like the following 您的情况如下

在此处输入图片说明

The HEAD is pointing, not to a branch, but directly to a commit (the one that tag <tag> also points to, but that's beside the point): you're in detached-HEAD state. HEAD指向的不是分支,而是直接指向提交(标记<tag>也指向该提交,但仅次于该提交):您处于分离HEAD状态。 To reattach the HEAD, you need to make it point to a branch, which you can do by running 要重新连接HEAD,您需要使其指向分支,可以通过运行

git checkout <branch-in-question>

However, Git will, in general, prevent you from checking out a branch if you're not in a clean working state. 但是,如果您的工作状态不干净,Git通常会阻止您检出分支。 That's where stashing comes in handy. 那就是藏起来很方便的地方。 Stashing is akin to tidying your desk by temporarily putting everything that's sitting on it in a drawer, only to retrieve that stuff at a later stage, when needed. 存放类似于通过将放在桌子上的所有物品暂时放在抽屉中来整理桌子,只是在以后需要时才将其取回。

Here, you should run 在这里,你应该跑

git stash save
git checkout <branch-of-interest>
git stash pop

Resolve any conflict arising because of that last command. 解决由最后一条命令引起的任何冲突。 In my example above, your repo would then look like so, 在上面的示例中,您的存储库将如下所示:

在此处输入图片说明

with your local changes in the working tree. 在工作树中进行本地更改。 You can then stage those changes, commit, and push to remote. 然后,您可以进行这些更改,提交并推送到远程。

Besides Jubobs' (correct) answer , it's worth noting that git records HEAD updates in git's reflog for HEAD . 除了Jubobs'(正确)的答案 ,这是值得注意的是git的记录HEAD在Git的更新引用日志HEAD

You can simply run git reflog (no extra arguments) to see something like this (yours will likely be much longer): 您可以简单地运行git reflog (没有额外的参数)来查看类似的内容(您的时间可能更长):

9b7cbb3 HEAD@{0}: checkout: moving from master to v2.2.1
c5b9256 HEAD@{1}: checkout: moving from maint to master
c2e8e4b HEAD@{2}: checkout: moving from master to maint
c5b9256 HEAD@{3}: merge refs/remotes/origin/master: Fast-forward
c18b867 HEAD@{4}: clone: from git://github.com/git/git

This shows all the movements I made in a clone of the source for git itself. 这显示了我在git本身的源克隆中所做的所有动作。 You can see that I did a git checkout maint and then git checkout master and then git checkout v2.2.1 to get to my current "detached HEAD" state. 您可以看到我做了一个git checkout maint ,然后是git checkout master ,然后是git checkout v2.2.1以达到我当前的“分离式HEAD”状态。 Here v2.2.1 is a tag. 这里v2.2.1是一个标记。 To get back on the branch I was on before, I can simply observe that the reflog says I moved "from master", so I just need to: 回到以前的分支,我可以简单地看到reflog指出我是“从master”迁移的,所以我只需要:

git checkout master

and I will be back on branch master. 我将回到分公司主管。

(Note that if I had made some new commit(s) here, this would "lose" them, except that I could find them again in the reflog. Also, if I had made changes to the work-directory, the git checkout master step might complain that changing to branch master would lose my changes. In this case, committing, stashing, and/or making a new branch, is usually the right thing to do.) (请注意,如果我在此处进行了一些新的提交,这会“丢失”它们,除了我可以在引用日志中再次找到它们。此外,如果我对工作目录进行了更改,则git checkout master步骤可能会抱怨更改为分支master会丢失我的更改。在这种情况下,提交,存储和/或创建新分支通常是正确的选择。

I would create a branch where you are, commit your changes, checkout master, and merge the new branch into master. 我将在您所在的位置创建一个分支,提交更改,签出master,然后将新分支合并到master。

git branch my-temporary-branch
git commit -m "my temp work"
git checkout master
git merge my-temporary-branch
git branch -d my-temporary-branch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM