Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why did git set us on (no branch)?

Tags:

git

This morning we pulled from our repo, and git put us on (no branch).

I don't understand this, why did this happen? And how to get out of it without losing our changes?

like image 550
e-satis Avatar asked Mar 23 '10 08:03

e-satis


People also ask

What is no branch in git?

The “no branch” state is called a detached HEAD. It is called this because the HEAD ref is not attached to any branch, instead it is pointing directly at a commit. To attach HEAD to a branch that points to the current HEAD commit, use git checkout -b branchname .

Why there is no master branch in git?

The remote you cloned from might still have a master branch (you could check with git ls-remote origin master ), but you wouldn't have created a local version of that branch by default, because git clone only checks out the remote's HEAD .

Why git branch is not showing all branches?

This can happen if your repo has 0 commits. If you make a commit, your current branch will appear when you do: git branch . Show activity on this post. Show activity on this post.

Why do we need a branch in git?

In Git, branches are a part of your everyday development process. Git branches are effectively a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug—no matter how big or how small—you spawn a new branch to encapsulate your changes.


1 Answers

"Not currently on any branch" means you have a detached head, i.e. your HEAD pointer is directly referencing a commit instead of symbolically pointing at the name of a branch.

You can get into this situation by checking out a commit by SHA1, or when you’re in the middle of a rebase, or when a merge fails. It’s hard to say what you may have done to get into this situation by accident.

It’s said that you may lose your changes when you switch from a detached HEAD to some branch, but the reflog will always keep track of where your HEAD moved. In fact, Git 1.7.5 will warn you when switching from a detached HEAD will lose commits. The only time you can really lose work is when you have uncommitted changes, which you may want to commit or stash.

A simple way to see what happened is git reflog or git log -g --decorate for a more verbose listing. The --decorate option will label every SHA1 with the names of all the branches that point at it. If the SHA1 of your current HEAD is exactly the same as master, then you don’t have to do anything but git checkout master to get back on track. Otherwise, see if the SHA1 is pointed to by some other branch. If not, you may wish to create a branch to hang on to it.

Another good command is git branch -av, which will similarly list all branches and what they point to, so you can see what your (no branch) is really supposed to be.

like image 88
Josh Lee Avatar answered Sep 28 '22 10:09

Josh Lee