简体   繁体   中英

Fixing detached head on a renamed git branch

New to git here. I have a feature branch ( feature/123 ) that was originally cut from develop , worked on, and then pushed to GitHub.

I just started making some local changes to this branch and then I decided I wanted to preserve my changes (not throw them away entirely) and start over fresh with the version of the branch that is currently up on GitHub.

So I decided to:

  1. Commit my changes (that I want to preserve and possibly -- though not likely -- come back to)
  2. Rename my feature branch
  3. Pull the feature/123 branch thats on the origin (GitHub)

So I:

git add .
git commit -m "Saving changes made thus far"
git branch -m feature/123-OLD
git checkout origin/feature/123

When I did this I saw:

$ git checkout origin/feature/123
Note: checking out 'origin/feature/123'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, 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 8877c28... 123: did some stuff

And now when I look at my current branch, I see:

$ git branch
* (HEAD detached at origin/feature/123)

develop

All this "detached head" stuff is creepin me out!

All I want is to have a local branch named feature/123 that is at the same exact state that GitHub is at. How can I accomplish this?

The first three commands here were all good:

 git add . git commit -m "Saving changes made thus far" git branch -m feature/123-OLD 

The last one changed the name feature/123 to the name feature/123-OLD (assuming, of course, that you were on a branch named feature/123 in the first place).

The last command is not wrong , it's just not the one you wanted:

 git checkout origin/feature/123 

What you wanted was:

git checkout feature/123

This would look around at your (local) branch names, not find anything named feature/123 (it doesn't exist any more— feature/123-OLD exists but that's not the same), and then invoke the "do what I mean" feature of git checkout .

This feature says: Hmm, I can't find the branch you asked for. I think maybe you meant for me to create a new branch! Can I find exactly one remote-tracking name, like origin/feature/123 , that looks a lot like feature/123 ? If I can find none, or two or more, I'll complain and fail. But if I can find exactly one , I'll make a new feature/123 , pointing to the same commit as the remote-tracking name!

And of course, it will find just one, so it will create feature/123 . When it does create feature/123 , it will default to setting its upstream to origin/feature/123 . That's the Do What I Mean or DWIM mode of checkout and it is in fact what you meant.

您可以结帐所需的分支

git checkout feature/123

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