简体   繁体   中英

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?

How do I [...] reattach the head where it was detached from [...]?

The output of git branch being

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

indicates that you've run 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. To reattach the HEAD, you need to make it point to a branch, which you can do by running

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. 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 .

You can simply run git reflog (no extra arguments) to see something like this (yours will likely be much longer):

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. 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. Here v2.2.1 is a tag. 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:

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.)

I would create a branch where you are, commit your changes, checkout master, and merge the new branch into 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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