Zepee
Zepee

Reputation: 1880

Git push not creating remote branch, pushing to master

I'm having an issue where I'm trying to push a local branch to a remote branch so that I can then create a pull request into master. I'm using:

git push origin brachname

This seems to fail, and instead is pushing to the master remote branch (which isn't great). If I reference the local and remote branches explicitly (ie: branchname:branchname) it works fine. It seems that either I have something off with my git config, or there's something wrong with the way I'm creating my local branches.

Any help would be appreciated.

Upvotes: 3

Views: 2965

Answers (3)

mikemtnbikes
mikemtnbikes

Reputation: 572

I ran into a similar problem after renaming a local instance of a new and recently pulled branch from develop to origin-develop. The branch was fetched from the remote repo origin.

After the rename, a generic push commands I gave would always try to push to origin/master. i.e.,

$ git push
To github.com:<USER>/<REPO>.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'github.com:<USER>/<REPO>.git'

I tried using git branch origin develop, but that didn't fix the problem. Instead, I got the error

$ git push origin develop
error: src refspec develop does not match any
error: failed to push some refs to 'github.com:<USER>/<REPO>.git'

Using the command git remote show origin allowed me to see that the while the local branch origin-develop was set up correctly for pulling (presumably because I created the local branch using the pull command), it was missing the push setting.

Local branches configured for 'git pull':
<snip>
    origin-develop                      merges with remote develop
<snip>
 Local refs configured for 'git push':
<snip>

I was able to fix my problem by using the command

git push origin origin-develop:develop

This worked, but I don't consider it solved because I have to use this command every time I want to push this branch to the remote. If I just use git push I get the ! [rejected] master -> master (non-fast-forward) error.

My understanding is that my pull command set up the remote for pulling, but not pushing. Because no remote push was defined, it defaulted to pushing to master.

What I don't understand is how to permanently change my configuration so that git push works as expected. I've tried using git branch -u , but get the same behavior (i.e. can't push). I wonder if somehow the push -all option is now the default and the error message is independent of the branch origin-develop.

Upvotes: 0

CodeWizard
CodeWizard

Reputation: 141946

Looks like you are on the master branch and trying to push it to another branch.

How to be able to push to different branch name?

Set/Update the tracking branch

git < 1.8:
git branch --set-upstream branch_name remote/branch_name

git > 1.8:
git branch branch_name --set-upstream-to remote/branch_name

Your current tracking information is inside your .git/config file


Create a new branch with the desired name

git checkout -b <new_branch>
git push origin <new_branch>

git will push to the remote branch your current branch name (assuming you did not changed the tracking branch)


Renaming the branch (so the tracking branch will be updated too)

Or you can use the -u switch:
git branch branch_name -u remote/branch_name


Upvotes: 2

Mayur Nagekar
Mayur Nagekar

Reputation: 811

Try the below

 git push -u origin <branch_name>

If you have permission to push a local branch onto remote repo, it should go through

Upvotes: 0

Related Questions