2

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: git remote I have also forked the main repo, and need to add that fork locally, which succeeds: remote added and fetched 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: checkout -> detached head 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: git 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?

Dr. Andrew
  • 2,532
  • 3
  • 26
  • 42

1 Answers1

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.

  • 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