dilmali
dilmali

Reputation: 41

git push origin doesn't create the remote branch

I cloned a repository to my local, made a branch, made the branch to track my local master. After changing, I committed my changes to my branch. and tried the following:

git push origin my-local-branch

but when looking at my remote repo, there is no my-local-branch there and my changes have been already merged into (remote) master. Any idea why this might have happened.

I always do this at work. I wonder if there is some default settings at work that I don't have for myself.

UPDATE: This happened again. FYI, here's my chain of commands:

git checkout -b my-local
git branch --set-upstream-to master
...
git add -A
git commit
git push origin my-local

Again, code was merged directly into remote master and no branch was created in remote. Then, I tried the same thing but this time without git branch --set-upstream-to master. And this actually worked! Why is this happening. It doesn't sound like an expected behaviour.

Upvotes: 1

Views: 3436

Answers (4)

1615903
1615903

Reputation: 34722

git branch --set-upstream-to master

This tells git that the upstream of your my-local-branch is your local master branch - I don't see why you would want to do that in the first place, as your goal seems to be that you have a branch called my-local-branch in your remote as well.

The simplest way is, in my opinion, to not use --set-upstream-to at all, but when you are doing your first push, do it like this:

git push -u origin my-local-branch

This creates a branch called my-local-branch on the remote as well, and sets it as the default upstream for you. Then you can just do git push without any arguments on subsequent pushes.

Upvotes: 1

Sajib Khan
Sajib Khan

Reputation: 24136

  • When you are doing set-upstream-to master, your local-branch start tracking to remote branch master. So, After pushing your local changes it is updating remote master branch instead of origin/your-local-branch.

--set-upstream-to=<upstream>
Set up 's tracking information so is considered 's upstream branch. If no is specified, then it defaults to the current branch.
See More

  • And when you didn't set set-upstream-to master, it defaults to current local branch. Now when you are pushing your changes it is creating origin/your-local-branch and updating with your changes.

If you want to see what tracking branches you have set up, you can use the -vv option.

$ git branch -vv 

Push to Non-default branch:

  • After setting set-upstream-to master (now default tracking branch is remote master), if you want to push other remote branch, need to push with <source-branch>:<dest-branch>.

    $ git push origin <local-branch>:<remote-branch>
    
    # if remote branch doesn't exit, do force push
    $ git push -f origin <local-branch>:<remote-branch>   # create a new branch and push to it
    

    See More

Upvotes: 0

Chao Luo
Chao Luo

Reputation: 2696

git branch -vv

-vv --verbose When in list mode, show sha1 and commit subject line for each head, along with relationship to upstream branch (if any). If given twice, print the name of the upstream branch, as well (see also git remote show )

maybe your local branch is related to remote master.

Upvotes: 0

Alexander Luna
Alexander Luna

Reputation: 5439

You can try passing the -u options:

git push -u origin <branch>

Upvotes: 1

Related Questions