Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move master to HEAD?

Tags:

git

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

like image 852
martin's Avatar asked Oct 26 '14 05:10

martin's


People also ask

Why did master change to Main?

For those who may not know, the 'master' branch was the default branch name for any git's fresh repository. Therefore, many people often used it as a stable branch. Spurred by the rise in racism cases across the US, GitHub recently renamed its 'master' branch to 'main'. the company said.


2 Answers

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 
like image 122
VonC Avatar answered Sep 28 '22 00:09

VonC


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' 
like image 41
Ed I Avatar answered Sep 28 '22 02:09

Ed I