How do you push just a single Git branch (and no other branches)?

GitGit Push

Git Problem Overview


I am working on a local git repository. There are two branches, master and feature_x.

I want to push feature_x to the remote repo, but I do not want to push the changes on the master branch.

Will a git push origin feature_x from my feature_x branch (feature_x branch already exists on remote) work?

I do not want to test this on my box, because I cannot push to master right now.

Git Solutions


Solution 1 - Git

yes, just do the following

git checkout feature_x
git push origin feature_x

Solution 2 - Git

By default git push updates all the remote branches. But you can configure git to update only the current branch to it's upstream.

git config push.default upstream

It means git will update only the current (checked out) branch when you do git push.

Other valid options are:

  • nothing : Do not push anything (error out) unless a refspec is explicitly given. This is primarily meant for people who want to avoid mistakes by always being explicit.
  • matching : Push all branches having the same name on both ends. (default option prior to Ver 1.7.11)
  • upstream: Push the current branch to its upstream branch. This mode only makes sense if you are pushing to the same repository you would normally pull from (i.e. central workflow). No need to have the same name for local and remote branch.
  • tracking : Deprecated, use upstream instead.
  • current : Push the current branch to the remote branch of the same name on the receiving end. Works in both central and non-central workflows.
  • simple : [available since Ver 1.7.11] in centralized workflow, work like upstream with an added safety to refuse to push if the upstream branch’s name is different from the local one. When pushing to a remote that is different from the remote you normally pull from, work as current. This is the safest option and is suited for beginners. This mode has become the default in Git 2.0.

Solution 3 - Git

Minor update on top of Karthik Bose's answer - you can configure git globally, to affect all of your workspaces to behave that way:

git config --global push.default upstream

Solution 4 - Git

Better answer will be

git config push.default current

upsteam works but when you have no branch on origin then you will need to set the upstream branch. Changing it to current will automatically set the upsteam branch and will push the branch immediately.

Solution 5 - Git

To push your current branch no matter what config you have:

git push origin $(git branch --show-current)

Solution 6 - Git

So let's say you have a local branch foo, a remote called origin and a remote branch origin/master.

To push the contents of foo to origin/master, you first need to set its upstream:

git checkout foo
git branch -u origin/master

Then you can push to this branch using:

git push origin HEAD:master

In the last command you can add --force to replace the entire history of origin/master with that of foo.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRafael MuellerView Question on Stackoverflow
Solution 1 - GitcpjolicoeurView Answer on Stackoverflow
Solution 2 - GitKarthik BoseView Answer on Stackoverflow
Solution 3 - GitBhaskarView Answer on Stackoverflow
Solution 4 - GitPlayer1View Answer on Stackoverflow
Solution 5 - GitDANView Answer on Stackoverflow
Solution 6 - GitMathKidView Answer on Stackoverflow