I'm seeing very odd behavior when I add a remote to a forked repo to git. This occurs whether I use Linux or my Windows machine (with mingw64). I'm working on this repository, and have a local copy as the remote origin
:
I have also forked the main repo, and need to add that fork locally, which succeeds:
It shouldn't need to be said, but the new fork has all the same branches as the origin. Additionally, the
testing
branch in the new fork has already been caught up-to-date with the testing
branch in the main repo with a merged PR. The odd behavior occurs when I simply try to checkout a branch in the newly-added remote:
The command clearly is to check out an existing branch on an existing fork. Yet, git seems to have instead checked out the latest commit on that branch. You can see in the output from the
git branch
command that git really does know about the correct branch:
This is not a problem just with the newly added fork. The other remotes shown in the first screenshot are also forks from the main repo and have the same problem. How can I get git to correctly checkout the branch?
Asked
Active
Viewed 2,802 times
2

Dr. Andrew
- 2,532
- 3
- 26
- 42
-
1Read this post it will answer your question: http://stackoverflow.com/questions/34519665/how-to-move-head-back-to-a-previous-location-detached-head/34519716#34519716 – CodeWizard Sep 27 '16 at 08:09
-
This post is irrelevant, because I have done nothing to the branch. I have merely checked it out and gotten this, as my screenshots show. – Dr. Andrew Sep 29 '16 at 06:21
-
@Dr.Drew Your PS1 is really cool... where can I get that? mine only shows the branch – Ray Foss Jan 27 '17 at 19:45
-
Hi Ray. What is PS1? – Dr. Andrew Jan 31 '17 at 18:03
1 Answers
1
You have checked out a remote branch. You first need to create a local branch before you can commit to it (this is described in the message git printed to you on the checkout). Once you have your local branch and did some commits to it, you can push those commits to the remote.

Martin Hierholzer
- 897
- 4
- 13
-
But I copied down the remote branch first - see the first command. When I originally clone the main repo with `git clone`, I don't have this issue... – Dr. Andrew Oct 02 '16 at 04:59
-
It still is a remote branch which you cannot commit to. You only fetched it. It is locally cached but still an exact read-only copy of the remote branch. Use "git checkout -b" to make a local branch from it (read the git man page for proper usage of the command). – Martin Hierholzer Oct 04 '16 at 09:22
-
Thanks Martin. The way this works is rather odd - when I clone the repo, I don't have to use `checkout -b`, but if I add a remote of the same repo, I do. – Dr. Andrew Oct 06 '16 at 10:36
-
This is because cloning already includes the creation of the local master branch (actually HEAD, which typically refers to the branch named master), but not of any other branches. You will get used to it eventually :-) – Martin Hierholzer Oct 07 '16 at 14:38