1

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.

dilmali
  • 41
  • 2
  • 5
  • "my changes have been already merged into (remote) master"! How is it possible if you not merge `your-branch` changes with `local-master` and push `remote-master`. Or, Create a pull request and merge to remote master? Can you attach your commands you've given ? – Sajib Khan Dec 10 '16 at 03:15
  • "git checkout -b my-local-branch" "git branch --set-upstream-to master" "git add -A" "git commit" "git push origin my-local-branch" – dilmali Dec 10 '16 at 21:21
  • ok. I got your scenario now. You are doing `--set-upstream-to master` so, `local-branch` integrated with `remote master` (default `current-branch`) & when you are pushing your changes it's updating `remote master`. I've attached an answer details. – Sajib Khan Dec 11 '16 at 02:41
  • I don't think so guys. I think you're reading the documentation wrong. Upstream is not equivalent to remote. Besides, this is what I see when I run set-upstream-to: "Branch my-local set up to track local branch master." It clearly says "local branch master" and not the remote master. – dilmali Dec 11 '16 at 03:29

4 Answers4

1

You can try passing the -u options:

git push -u origin <branch>
Alexander Luna
  • 5,261
  • 4
  • 30
  • 36
1
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.

1615903
  • 32,635
  • 12
  • 70
  • 99
0
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.

chaoluo
  • 2,596
  • 1
  • 17
  • 29
0
  • 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

Community
  • 1
  • 1
Sajib Khan
  • 22,878
  • 9
  • 63
  • 73
  • No it doesn't. What you say is "When you are doing set-upstream-to master, your local-branch start tracking to remote branch master" but documentation doesn't say that. upstream is not equivalent to remote. Besides, when I run my set-upstream-to in my local repo what I get is this message: "Branch my-local set up to track local branch master." Git is clearly saying my branch is set to track local master and not remote master. – dilmali Dec 11 '16 at 16:45
  • Yes. Your `local branch` tracking `local master` and your `local-master` tracking `remote-master`. So, when pushing, it is finding remote default-branch (`remote master`). – Sajib Khan Dec 11 '16 at 16:51