281

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.

Rafael Mueller
  • 6,028
  • 3
  • 24
  • 28
  • Take a look here: [remote](http://git.or.cz/course/svn.html#remote) and here: [push a branch to github](http://github.com/guides/push-a-branch-to-github) Sounds like it would work. – al. May 04 '09 at 14:01

6 Answers6

412

yes, just do the following

git checkout feature_x
git push origin feature_x
cpjolicoeur
  • 12,766
  • 7
  • 48
  • 59
  • 61
    With modern git you should be able to simply "git push origin HEAD", or even "git push HEAD" to push only currently checked-out branch. – Jakub Narębski May 05 '09 at 09:11
  • 3
    Is it necessary to checkout to feature_x ? – hd. Nov 28 '12 at 11:45
  • 7
    yes, because if you are on master it would try to push the local master branch to the remote feature_x branch. to not have to checkout first you would have to do "git push origin feature_x:feature_x" – cpjolicoeur Nov 28 '12 at 21:42
  • 2
    @cpjolicoeur I saw in other questions (e.g: [here](http://stackoverflow.com/questions/2765421/how-to-push-a-new-local-branch-to-a-remote-git-repository-and-track-it-too) ) they are doing `git push -u origin `. However you did not mention it. Is it necessary ? – riroo Jan 11 '17 at 07:25
  • 4
    @miss_R the `-u` option on a `git-push` command will set the upstream reference for tracking the branch just pushed. This will make things like `git-pull` on that branch in the future already know which branch to pull from without specifying it. It is not required as an option to push a single branch, but is widely used because a lot of people do want to make the local branch track the remote branch they are pushing. – cpjolicoeur Jan 11 '17 at 14:05
  • aha I see. Thanks a lot. In my case git push without `-u` was enough. But it's good to know for people who are interested. – riroo Jan 11 '17 at 14:08
  • 1
    Does this mean `git push origin master` only pushes the master branch and not anything else you've been working on? – Kyle Delaney Mar 29 '17 at 12:48
  • Correct, that command would push the local `master` branch to the repository named `origin` – cpjolicoeur Mar 29 '17 at 13:52
  • this doesn't seem correct, still picking up more than one commit – Alexander Mills Apr 14 '18 at 19:17
  • @AlexanderMills This question was about pushing a single git *branch* not a single *commit* – cpjolicoeur Apr 16 '18 at 12:56
  • I think the first time you should use the `-u` once to initiate tracking. Afterward, just use `git push ...` – Jay Patel May 09 '19 at 05:13
  • `fatal: 'origin' does not appear to be a git repository fatal: Could not read from remote repository.` – Dmitriy Ogureckiy Apr 06 '23 at 14:32
  • @DmitriyOgureckiy replace `origin` with whatever the name of your git remote is – cpjolicoeur Apr 12 '23 at 12:43
89

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.
Karthik Bose
  • 33,556
  • 3
  • 33
  • 43
  • 2
    Thanks, `current` was what I was looking for, by default `git push` in the `foo` branch will push it to the `origin/foo` branch. – Dorian Feb 25 '14 at 14:51
  • @Dorian thanks, I agree that `current` makes more sense as a default. – Zoltán Apr 15 '15 at 15:16
  • @Dorian, @Zoltán - I feel `simple` makes more sense as a default. I've updated the answer with 'when to use what'. Pls have a look. – Karthik Bose Apr 16 '15 at 06:55
  • 1
    Note: since git version 2 the default value has changed to `simple`. – Danijel Oct 03 '19 at 14:05
11

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
Community
  • 1
  • 1
Bhaskar
  • 281
  • 3
  • 8
2

Better answer will be

git config push.default current

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

braulio
  • 543
  • 2
  • 13
Player1
  • 2,878
  • 2
  • 26
  • 38
1

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

git push origin $(git branch --show-current)
DAN
  • 507
  • 1
  • 7
  • 16
  • small note: older versions of git don't have the --show-current flag. For example git 1.8 which comes with RHEL7 doesn't have it. It is admittedly an old version of git! – frankster Nov 08 '22 at 13:19
-3

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.

MathKid
  • 1,903
  • 1
  • 21
  • 21