Anand Devaraj
Anand Devaraj

Reputation: 745

Pushing to remote master branch from local branch

I am using Git as my VCS. I have checkout the master and created the local branch from the master to work for specific ticket(for eg. fileuploadbranch). Made code changes, added the files changed to index. Then commit the changes using

git commit -m "xxxx"

Now i tried to push to the remote master using

git push -u origin --all

So in my bitbucket hosting services, i can able to see two branches namely master and fileuploadbranch.

I realised it was due to giving the git push with "--all" option. This makes all the branches to be pushed to remote.

So now i tried with push with

git push -u origin master

I believe it will be moved only to remote master. But in the bitbucket commits view, it shows the local branch name(fileuploadbranch) on the right hand side and it indicated me that the push is from the local branch.

  1. Whether this changes will be available in remote master even we pushed it from the local branch?
  2. why it shows the local branch name(RHS) in commits view in bitbucket?

Any help will be appreciated!!!...

Thanks

Upvotes: 0

Views: 623

Answers (1)

torek
torek

Reputation: 490068

If you want to push your local branch which you've called file_upload_branch (I added underscores here for readability—note that this affects every occurrence below), but call it master on the remote:

git push origin file_upload_branch:master  # see below about -u

The last argument to push here is called a "refspec", and you spell it out with two parts: a local-side identifier (typically a branch name), and a remote-side identifier (another, possibly different, branch name). The colon character : serves to separate the two.

If you use only one name, leaving out the colon, git assumes you want the same name on each side. (Which you just said is not what you wanted.)

If you leave out the refspec entirely (and don't use options like --all), git chooses what to push based on various configuration variables. If you have not set any of them, all current versions of git use the method called matching:1 your git, doing the push operation, asks the other side's git "what branches do you have now", takes the resulting list and sees which local branches you have that match, and pushes those.

With --all as a (pseudo) refspec, your side just asks to push every branch name you have, to the same name on the remote (as you discovered).

(The syntax :branchname, given as a refspec, asks the remote to delete the named branch. You will probably want to use this to delete file_upload_branch on the remote.)

If you set the configuration variable push.default to upstream,2 this changes the default action for push with no refspec to use the "upstream" name of the current branch. That is, if you're on file_upload_branch and its "upstream" name on the remote (origin) is master, a plain:

git push origin

will act as if you wrote:

git push origin file_upload_branch:master

This is where the -u argument comes in: after file_upload_branch is successfully pushed to master on remote origin, git push will set file_upload_branch's "upstream" to origin/master. You only need to do this once per branch; having set the upstream, and set push.default to upstream, future pushes will automatically "do what you wanted".


1In git version 2.0, the default value for remote.pushdefault is going to change to simple. If you set your --global remote.pushdefault to simple now, you will be prepared for (and thus unaffected by) this change. Or, if you prefer upstream as a personal default, you can set that and it should still work in 2.0.

2Note that remote.pushdefault can be set in both your per-user (i.e., --global) git configuration and also in a per-repo configuration. The per-repo configuration will override the global configuration whenever both are set. You can also set a rather dizzying array of additional configurations, though I wouldn't do any of that just yet :-) : remote.origin.push, branch.file_upload_branch.remote, and branch.file_upload_branch.pushremote can all affect the behavior of git push. See the git config documentation for full details.

Upvotes: 1

Related Questions