56
* [971f835] (HEAD, original_idea) Now working here. Some comment 9
* [692b673] Some comment 8
* [3ebff62] Reverted to original idea. Some comment 7
| * [72ea01d] (master) Decided it wasn't worth the effort. Some comment 6
| * [985c1ad] Some comment 5
| * [4d7d491] Some comment 4
| * [0c697bb] Branched to try an idea. Some comment 3
|/
* [7280b1f] Some comment 2
* [5c2c6d0] Some comment 1
* [bc7aac6] Initial commit

So, master got left behind. Not sure how that happened. Once I decided I was done with the branch that didn't work out I checked out [7280b1f] and continued from there.

How do I fix this? I this the infamous "detached head" issue?

Does master have to be aligned with HEAD for optimal health of the repo?

My workflow is super-simple. This is just managing a single text file. In the vast majority of cases it's:

git add .
git commit -am "Some comment"

And that's it.

I decided to try an idea and created a branch. When it didn't work out I checked out the root of that branch and went back to the git add/commit routine.

martin's
  • 3,853
  • 6
  • 32
  • 58
  • possible duplicate of [Git: How can I reconcile detached HEAD with master/origin?](http://stackoverflow.com/questions/5772192/git-how-can-i-reconcile-detached-head-with-master-origin) – Zombo Oct 26 '14 at 06:03

2 Answers2

62

If git branch shows that your current branch is original_idea, that wouldn't be a detached HEAD situation.

But in any case, if you want your master to reflect your "current commit" (which is what HEAD means in Git):

git branch -f master HEAD
git checkout master

You would find other alternatives in "How to I “move” my commits from “no branch” to an actual branch?".

The problem with this is that it makes the entire branch currently ending at master disappear.

Then all you need to do is:

  • make sure HEAD is referenced by a branch (like original_idea on your schema)
  • rebase that branch on top of master:

That would mean:

git checkout original_idea
git rebase master
git checkout master
git merge original_idea
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
26

Is this the infamous "detached head" issue?

No, HEAD is pointing at branch original_idea

Does master have to be aligned with HEAD for optimal health of the repo?

No, master is just a conventional name, usually taken to be a branch which is readily buildable and hopefully a working snapshot.

How do I fix this?

If you have pushed master to another repository, you need to merge you changes from original_idea into master:

git checkout master
git merge original_idea

Be aware that if there are any conflicting changes you'll need to resolve them.

If you have not yet pushed master to another repository, you could delete master and recreate it from original_idea. WARNING: Everyone who was working on master will have to reset their master branch.

$ git checkout master
Switched to branch 'master'

$ git checkout -b experiment_1
Switched to a new branch 'experiment_1'

# Here we are going to delete master.  Should be safe, since experiment_1
# was created from this point on the previous command

$ git branch -d master
Deleted branch master (was 72ea01d).

$ git checkout original_idea 
Switched to branch 'original_idea'

# Now that we checked out the branch HEAD is pointing to, we can recreate master

$ git checkout -b master
Switched to a new branch 'master'
Ed I
  • 7,008
  • 3
  • 41
  • 50
  • 1
    Could I ask you to cover one more case? Move `master` to `HEAD` while retaining the branch currently named `master` by renaming it `experiment_1` first. In other words, the structure does not change at all, labels move. Thanks! – martin's Oct 27 '14 at 14:36
  • Which commit in your diagram do you want to be the tip of *master*? – Ed I Oct 27 '14 at 14:59
  • '72ea01d`, where `master` is right now, would be named `experiment_1`. Then `master` would move to where `HEAD` is right now, `971f835`. Thanks! I am working on understanding this better through a couple of online tutorials. – martin's Oct 28 '14 at 05:04
  • Try to follow these commands now. Surely you've backed up your whole repo to experiment, if not you should do it now. There are many ways of "moving branches", and [deleting a branch](http://git-scm.com/docs/git-branch#_options) with `branch -d ` is just one of them, but when I was learning git I used to do it like this. – Ed I Oct 28 '14 at 05:34
  • Yes, I backup my repo before trying anything. I don't trust my Git Kung Fu yet. I'll try this later today. Thanks! – martin's Oct 29 '14 at 18:02
  • Thanks. Worked very well and gave me a better understanding of `master`. – martin's Nov 01 '14 at 18:59
  • what does original_idea denotes here?is it the head or origin/master – prasanthMurugan Jan 18 '17 at 06:04
  • I don't understand why I need to merge anything. I have the same situation and I just want master to be on commit 971, I don't care about anything else and I don't want to go and manually resolve all these conflicts. There must be a simpler way to do this. – Bolton Bailey May 18 '19 at 08:00
  • You need to merge to prevent rewriting history. If you know exactly what you are doing you can run `git reset --hard ` with master checked out. – Ed I May 18 '19 at 17:22