How to move master to HEAD?

Git

Git Problem Overview


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

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmartin'sView Question on Stackoverflow
Solution 1 - GitVonCView Answer on Stackoverflow
Solution 2 - GitEd IView Answer on Stackoverflow