Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git branch (no branch)

Tags:

git

well, i have just made a huge mistake.

I am working on a project, and thought i was working in the master branch.
So i added the files i needed, commited them and when i tried to push to origin i got

Everything is up to date

I used git branch and got

* (no branch)
* master

So, in stress, i stashed the pending changes in (no branch) and checked out master. Now i lost all my changes, because i can't checkout the branch "that shall not be named". When i list the branches i only have

* master
like image 671
André Alçada Padez Avatar asked Aug 09 '12 11:08

André Alçada Padez


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 git branch is not showing 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 . Save this answer.

What does no branch mean?

"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.


1 Answers

You haven't lost the changes if they were committed. You just don't have a named reference to them.

Try running git reflog and look for a line near the top that will say something like checkout: moving from <commit-id> to master. You can then use that <commit-id> to create a temp branch to inspect your changes and bring them over to the master branch:

git checkout -b temp-branch <commit-id>

You can merge this branch, or just cherry-pick the necessary commits.

I hope this helps.

like image 72
mamapitufo Avatar answered Oct 08 '22 13:10

mamapitufo