Home > other >  Push a local branch as a new remote branch that was based on a remote branch
Push a local branch as a new remote branch that was based on a remote branch

Time:08-27

I'm new to version control and having a hard time on this one.

So, I have this branch in the remote called check-fallback,

and I wanted to create a local branch based on that one because my task is somewhat close and requires most works on that branch.

So what I did was, git checkout -b check-fallback-subtask origin/check-fallback

Now, I have check-fallback-subtask as a local branch, worked on it, git add ., and git commited. Now, when I push, git returns the following instructions, which I'm not really sure which to follow.

fatal: The upstream branch of your current branch does not match
the name of your current branch.  To push to the upstream branch
on the remote, use

    git push origin HEAD:check-fallback

To push to the branch of the same name on the remote, use

    git push origin HEAD

To choose either option permanently, see push.default in 'git help config'.

What I just want is to peacefully(lol) push my local check-fallback-subtask into the remote repo as a new branch.

How do I do that? Please bare with me since I'm new to all of this.

CodePudding user response:

It sounds like you never checked out check-fallback locally before. So when you created check-fallback-subtask, git automatically associated your local branch with origin/check-fallback. To change this association, you just need to supply some details to git push and override the defaults:

git push --set-upstream origin check-fallback-subtask:check-fallback-sutask

You only need to do this the once. Then you can git push as usual. See git help push for more details.

To avoid issues like these, I generally create branches on the remote (GitHub, GitLab, BitBucket, etc.) through their web UI. Then I will git fetch locally and git checkout the branch I created.

CodePudding user response:

Code-Apprentice's answer is correct, but if you only need a one-off push of a local branch to a specific remote branch, you can you the following command:

git push origin local:remote

e.g.

git push origin check-fallback-subtask:check-fallback-subtask

CodePudding user response:

The problem is with this command:

git checkout -b check-fallback-subtask origin/check-fallback

When you create a new branch and specify to start from a remote tracking branch (in this case origin/*) then by default it will track that branch. You can specify to not track it like this:

git checkout -b check-fallback-subtask origin/check-fallback --no-track

And then it will not be tracking anything yet, and act just like you had branched off of local branch, such as your local check-fallback.

Note, when you created your branch, you probably saw a message like this:

Switched to a new branch 'check-fallback-subtask'
branch 'check-fallback-subtask' set up to track 'origin/check-fallback'.

Next time you see that it might remind you that you need to change your upstream branch to something else. You can also type git status at any time to see if your local branch is tracking a remote branch yet.

  •  Tags:  
  • git
  • Related