22

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?

Zoe
  • 27,060
  • 21
  • 118
  • 148
hubatish
  • 5,070
  • 6
  • 35
  • 47
  • What branch do you want to checkout? When you refer to "changes", do you mean the untracked files? – Tom Fenech Sep 04 '14 at 16:28
  • I was trying to push to origin/web-zach, and the "changes" in the other question (and previously in mine before I lost them - luckily backed them up with old-fashioned copy-paste) referred to some changes I was trying to commit. I don't really care abou the untracked files, and should probably add them to my .gitignore. – hubatish Sep 04 '14 at 17:16

3 Answers3

52

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
jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • 2
    thanks, this is really helpful! I followed your instructions and everything seems to be working as you say. I am on local branch web-zach - is it not possible to be "on" the remote branch? – hubatish Sep 04 '14 at 16:56
  • 1
    No, you can't check out `origin/web-zach` as you would `web-zach`. They're different types of branches. You can think of a remote-tracking branches (such as `origin/web-zach`) as an enemy piece in a chess game: you can see where the piece is, but you're not allowed to move it. – jub0bs Sep 04 '14 at 16:59
  • 1
    Thanks for the explanation and making me go read http://git-scm.com/book/en/Git-Branching-Remote-Branches#Tracking-Branches That makes sense now – hubatish Sep 04 '14 at 17:09
  • 1
    @hubatish For information, I was utterly confused by the term "tracking" when I started learning about Git. It's used in different contexts for completely different things. Here is good blogpost that touches on this topic: http://longair.net/blog/2009/04/16/git-fetch-and-merge/ – jub0bs Sep 04 '14 at 18:06
  • @jub0bs Your explanation is great but when you said "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"." Did you mean, you end up in so-called "not currently on any branch"? After all that is what this question is about... – KansaiRobot May 07 '23 at 02:59
0

I wanted to also add in order for command to work, you have to commit changes, before pushing.

git checkout -b web-zach
git status
git commit -a -m "Added new commands Changes"
git push -u origin web-zach
-1

Use:

git switch web-zach

Result:

Branch 'web-zach' set up to track remote branch 'web-zach' from 'origin'.        
Switched to a new branch 'web-zach'
Christian
  • 4,902
  • 4
  • 24
  • 42
  • git switch: Optionally a new branch could be created with either -c, -C, automatically from a remote branch of same name exactly like: git checkout -b web-zach..... is another option – francesco freddi Apr 04 '22 at 14:27
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 04 '22 at 15:45