Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detached HEAD Issue in Android Studio

I am new to Android Studio and have began developing a Navigation Drawer app.

After having made a mistake in a commit, I played around with the Version control's "Checkout Revision" and now I can no longer push my project onto Github due to a "Detached HEAD".

How can I fix this problem? My app runs perfectly fine in the emulator.

Thanks

like image 638
Adam Arcaro Avatar asked Oct 09 '16 00:10

Adam Arcaro


People also ask

How do you fix a detached head error?

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.

Why is git head detached?

When you use the git checkout command to view a commit, you'll enter “detached HEAD state”. This refers to when you are viewing a commit that is not the most recent commit in a repository. Detached HEAD state is not an error nor is it a problem. When you are ready, you can navigate back to the HEAD in your repository.

How do I undo a detached head state git?

Note that once Git prunes your detached HEAD state commits, there is no way to get them back. However, if they have not been deleted, you can check out to that SHA-1 commit hash, create a branch, and merge it to the desired branch to preserve the changes.

What is head detached at origin master?

The Detached HEAD state warns that your activity is “detached” from the project's development. It allows checking out commits and examining the repository's older state without creating a local branch. The HEAD updates the git checkout command to point the specified branch or commit.


2 Answers

you can go to VCS menu then Git, Branches, then in Git Branches dialog click on item below local branches then checkout branches and then accept your default branches. it will connect your project to it's default branch and you can commit your project.

like image 101
Meysam Hadigheh Avatar answered Sep 19 '22 20:09

Meysam Hadigheh


If you already made a lot of changes and want to commit and push online but out of sudden got this detached head issue, here are steps you can do for Git through Android Studio:

  1. Go to VCS -> Git -> Branches...
  2. Click the +New Branch, just put any temporary name.
  3. Then commit your changes to the new branch (don't push yet, since it'll create new branch online).
  4. Go to VCS -> Git -> Branches... again and now select the previous branch(local) with the detached head issue and click checkout branch.
  5. Go to VCS -> Git -> Branches... again and now select the newly created branch(local) and select merge into current.
  6. After success merge, double check if your code is the latest one. Now you can push the branch(previously got issue) without detached head issue. If you click commit at this point there'll be nothing to be committed since you have merged the latest changes. All you need to do is just push online. By this you prevent yourself from creating new branch online just to fix this issue.
  7. For cleaning purpose, once you managed to push the branch online, just select again the newly created branch(local) and click delete branch.
like image 23
Cousin Roy Avatar answered Sep 20 '22 20:09

Cousin Roy