Home > Software design >  Git checkout new upstream branch that is not in remote origin and then push to remote origin
Git checkout new upstream branch that is not in remote origin and then push to remote origin

Time:11-20

The current upstream repo, says main/repo, has a branch newBranch. I have another fork of that repo myFork/repo which does not have newBranch

Now locally, I want to checkout that upstream/newBranch to my PC and then push that to origin/newBranch (which is my fork).

I'm trying with git checkout upstream/newBranch but I got the below message:

Note: switching to 'upstream/newBranch'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

I would like my local branch to track the origin branch, not the upstream branch

What is the correct sequence of command to do? Thank you

CodePudding user response:

This is not an error but a warning. This warning says that you're on a detached HEAD. This means that when pushing, no branch will follow your commit and it will be collected by the GC if you don't attach a branch or merge it into an existing branch.

What you can do, is create a branch named newBranch at the same point as upstream/newBranch with the following command:

git checkout -b newBranch upstream/newBranch

And when pushing, you must specify the origin where to push

git branch --set-upstream newBranch origin/newBranch
git push origin newBranch
  • Related