Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git problem - got detached from branch

Tags:

git

Accidently I got detached from my application branch:

Not currently on any branch. nothing to commit (working directory clean)

How can I return to the branch?

like image 395
Oded Harth Avatar asked Mar 09 '11 20:03

Oded Harth


People also ask

Why is my git branch detached?

However, if you run git checkout on a specific commit, you won't be at the HEAD of the branch, therefore Git can't add any commits you create in the correct place. As a result of this, you'll be "detached" from the end of the branch and you can make changes and commit them, but they won't impact any branches.

How do you fix a detached head branch?

If you want to keep changes made with a detached HEAD, just create a new branch and switch to it. You can create it right after arriving at a detached HEAD or after creating one or more commits. The result is the same. The only restriction is that you should do it before returning to your normal branch.

How do you reattach a detached head?

You must understand that any of your branches will not be affected if you ever get into a detached state . Now, the best way to reattach the HEAD is to create a new branch. We can do it as simple as git checkout -b <branch-name> . This will commit the changes from your temporary branch into the branch you need them.


Video Answer


1 Answers

Try:

git checkout master

... or whatever branch you were previously on. To give some further explanation:

One of the most common uses of git checkout is to switch from one branch to another. e.g. git checkout experiment, git checkout master. However, you can also give it the name of a tag, or the SHA1 sum (object name) of a commit - in those cases, git will change HEAD (which normally points to a branch, indicating that that's your current branch) to point to that tag or commit. This is known as "detached HEAD" or "not being on a branch" - the main difference is that if you create commits when you're in detached HEAD mode, they won't advance a branch, so they're easier to lose track of.

However, this is a very useful thing to be able to do when you want to look at the state of your repository at some random point in the past. (e.g. jumping around in this way is often the first step of trying to find the last good commit for (the awesome) git bisect.)

like image 158
Mark Longair Avatar answered Oct 17 '22 21:10

Mark Longair