Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Git tell me "Not currently on any branch" after I run "git checkout origin/<branch>"?

Tags:

I was trying to follow the instructions from Git: "Not currently on any branch." Is there an easy way to get back on a branch, while keeping the changes? but git checkout appears to be broken:

$ git checkout origin/web-zach HEAD is now at 1366cb1... Changed so css files not ignored  $ git status # Not currently on any branch. # Untracked files: #   (use "git add <file>..." to include in what will be committed) # #       .cordova/config.xml #       www/languages/pt/sounds/ nothing added to commit but untracked files present (use "git add" to track) 

More specifically, I'm worried about the "Not currently on any branch" message. git checkout doesn't seem to do anything here... Isn't the entire purpose of that command to put me on a branch? How can I get back on a branch and commit/push again?

like image 361
hubatish Avatar asked Sep 04 '14 16:09

hubatish


People also ask

Does git checkout use current branch?

The "checkout" command can switch the currently active branch - but it can also be used to restore files. The most common use case for "checkout" is when you want to switch to a different branch, making it the new HEAD branch.

How do I force git checkout to another branch?

You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD ). Basically, it can be used to throw away local changes.

How do I pull a git branch from Origin?

In case you are using the Tower Git client, pulling from a remote is very easy: simply drag the remote branch and drop it onto your current HEAD in the sidebar - or click the "Pull" button in the toolbar.


1 Answers

The output of git status indicates that your working directory is clean; good.

Now, by running

git checkout origin/web-zach 

you are attempting to check out a remote-tracking branch, called origin/web-zach; it's a special type of branch, local to your repo, that keeps track of the corresponding branch, web-zach, living in the remote repository called origin.

However, the HEAD reference (which you can think of as a "You Are Here" marker on a map) cannot point to a remote-tracking branch; only to a "normal" local branch, or to a commit directly. When you attempt to check out a remote-tracking branch, the HEAD reference ends up pointing directly at the tip of the remote-tracking branch (i.e. the commit to which that remote-tracking branch points):

enter image description here

When HEAD does not point to a "normal" local branch, but points to a commit directly, you end up in so-called "detached-HEAD state". It's not the end of the world, but avoiding landing in that state as much as possible (at least at the beginning of your Git learning) will likely spare you some surprises.

To remedy the situation, you need to reattach HEAD to some local branch. Here, you may want to create and check out such a local branch, by running, for instance

git checkout -b web-zach 

HEAD will then be pointing at the newly created local branch called web-zach:

enter image description here

Then, you should get

$ git status On branch web-zach Untracked files:   (use "git add <file>..." to include in what will be committed)       .cordova/config.xml       www/languages/pt/sounds/ nothing added to commit but untracked files present (use "git add" to track) 

After that, you'll be free to make changes, stage them, commit, and (if you have write access to the remote repo corresponding to origin and no one else has pushed anything to origin/web-zach since your last git fetch), push, using

git push -u origin web-zach 
like image 198
jub0bs Avatar answered Oct 08 '22 20:10

jub0bs