1610

I create a new branch in Git:

git branch my_branch

Push it:

git push origin my_branch

Now say someone made some changes on the server and I want to pull from origin/my_branch. I do:

git pull

But I get:

You asked me to pull without telling me which branch you
want to merge with, and 'branch.my_branch.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.

If you often merge with the same branch, you may want to
use something like the following in your configuration file:

    [branch "my_branch"]
    remote = <nickname>
    merge = <remote-ref>

    [remote "<nickname>"]
    url = <url>
    fetch = <refspec>

See git-config(1) for details.

I learned that I can make it work with:

git branch --set-upstream my_branch origin/my_branch

But why do I need to do this for every branch I create? Isn't it obvious that if I push my_branch into origin/my_branch, then I would want to pull origin/my_branch into my_branch? How can I make this the default behavior?

Ram Rachum
  • 71,442
  • 73
  • 210
  • 338
  • 22
    The default for `branch.autosetupmerge` means that the upstream configuration for a new branch is only automatically set when creating a branch from a remote-tracking branch (e.g. `/`) (see *[git-config(1)](http://www.kernel.org/pub/software/scm/git/docs/git-config.html)*). You are probably creating your branches from existing local branches. If you are effectively branching directly from the tip of a remote branch (despite being on a local branch), then you can use `git branch my_branch /` to automatically setup the upstream configuration. – Chris Johnsen May 23 '11 at 04:16
  • 21
    FYI, the `--set-upstream` option is deprecated. You should use `--track` or `--set-upstream-to` instead. – Sean the Bean Aug 12 '14 at 15:00
  • 164
    if `--set-upstream` is deprecated, then perhaps the git devs should remove it from the help message that gets displayed when you run `git push` with no options and no upstream is set? – Christopher Hunter Sep 15 '16 at 00:15
  • 18
    @ChristopherHunter It's been over a year since your comment and it **still** says that. Is it just a sloppy feedback or perhaps is there a technically wise reason to keep it around that we're ignorant about? – Konrad Viltersten Nov 13 '16 at 15:19
  • 5
    For others looking for a more recent one-liner (post git 2.0): `BRANCH=$(git symbolic-ref --short HEAD) && git branch --set-upstream-to=origin/$BRANCH $BRANCH` – jrhorn424 Apr 12 '17 at 00:02
  • @ChristopherHunter: You meant `git pull` rather than `git push`, right? – LarsH Mar 02 '18 at 21:48
  • @LarsH, no I mean `git push`. Specifically the modern usage to set the upstream branch to which you want to push, is `git push -u origin branchname`, as described in the answer by @Mark Longair. With `git pull`, the `-u` does a different thing, and I find that I would rather specify each time what branch to pull from anyway. – Christopher Hunter Mar 02 '18 at 22:56
  • 24
    @ChristopherHunter `git branch --set-upstream` is deprecated. `git push --set-upstream` is not. – Brian Gordon Sep 19 '18 at 00:24
  • @RamRachum consider changing the correct answer to be https://stackoverflow.com/a/22933955/4035845 – AnthumChris Mar 07 '19 at 22:43
  • Git stopped supporting the `--set-upstream` option. `branch --set-upstream` has been deprecated in Git 1.8 and had finally been retired in Git 2.15 on Nov 2017. – Jarek Przygódzki Nov 21 '19 at 10:18
  • @JarekPrzygódzki presumably you mean `git branch --set-upstream-to`? Or perhaps `git push --set-upstream`? I'm not sure ... but my installed Git 2.25 doesn't seem to be aware that this has been retired – 0xC0000022L Oct 06 '20 at 16:00
  • @jrhorn424 excellent, I'll train my dog to roll over the keyboard correctly to enter this quickly – 0xC0000022L Oct 06 '20 at 16:01

22 Answers22

1640

A shortcut, which doesn't depend on remembering the syntax for git branch --set-upstream 1 is to do:

git push -u origin my_branch

... the first time that you push that branch. Or, to push to the current branch to a branch of the same name (handy for an alias):

git push -u origin HEAD

You only need to use -u once, and that sets up the association between your branch and the one at origin in the same way as git branch --set-upstream does.

Personally, I think it's a good thing to have to set up that association between your branch and one on the remote explicitly. It's just a shame that the rules are different for git push and git pull.


1 It may sound silly, but I very frequently forget to specify the current branch, assuming that's the default - it's not, and the results are most confusing :)

Update 2012-10-11: Apparently I'm not the only person who found it easy to get wrong! Thanks to VonC for pointing out that git 1.8.0 introduces the more obvious git branch --set-upstream-to, which can be used as follows, if you're on the branch my_branch:

git branch --set-upstream-to origin/my_branch

... or with the short option:

git branch -u origin/my_branch

This change, and its reasoning, is described in the release notes for git 1.8.0, release candidate 1:

It was tempting to say git branch --set-upstream origin/master, but that tells Git to arrange the local branch origin/master to integrate with the currently checked out branch, which is highly unlikely what the user meant. The option is deprecated; use the new --set-upstream-to (with a short-and-sweet -u) option instead.

dahlbyk
  • 67,544
  • 8
  • 95
  • 120
Mark Longair
  • 385,867
  • 66
  • 394
  • 320
  • 97
    Also note that even if you forget the `-u` the first time you push, you can run the push again with that flag and it will start tracking. – Henrik N Jan 12 '13 at 17:33
  • 74
    None of these satisfy the use-case of using git push without arguments. It remains that I still have to remember to 'git push -u origin my-branch' when moving my new branch to the remote for the first time. – Karl the Pagan May 09 '13 at 16:15
  • 1
    @KarlthePagan When you create a remote-tracking local branch, you tell git you want to create a new branch, and you name the remote repo and branch. When you create a tracked remote branch, you tell git you want to create a new branch, and you name the remote repo and branch. In both cases, git properly defaults the branch name. Localized conveniences like what you're asking for is exactly what [git aliases](https://git.wiki.kernel.org/index.php/Aliases) are for. – jthill Jun 06 '13 at 23:41
  • 9
    Note to self: a more complete explanation for the first `git push -u origin master`: http://stackoverflow.com/a/17096880/6309 – VonC Jun 14 '13 at 19:56
  • 22
    I hate remembering that syntax as well, so I created the following alias: `alias gpo="git push --set-upstream origin $(git branch | awk '/^\* / { print $2 }')"` – lillialexis Oct 16 '13 at 18:44
  • 1
    Here I always set it with my first push this way : `git push -u origin --all` which makes my life much easier... – MensSana Oct 23 '13 at 14:12
  • 118
    This is all fine, but I still think the OP's complaint is valid. You start a local branch, work on it, push it to origin to share (with no args); why shouldn't that set the upstream? Is it actually desirable for some reason NOT to set upstream when pushing a new branch to a remote? – GaryO Nov 15 '13 at 16:30
  • 2
    @lillialexis, thanks for that tip. I hate seeing `Branch foo set up to track remote branch foo from origin.` every time, so I came up with `alias gu="git push --set-upstream origin $(git branch | awk '/^\* / { print $2 }') >> /dev/null"` You can still see the output, but you don't see the redundant messages. – weisjohn Dec 04 '13 at 17:44
  • 5
    A more correct answer to the question asked is given below by @Zamith. Set your global config so that you don't have to type anything. – Nicholas Shanks Apr 28 '14 at 16:02
  • 1
    Since I haven't seen it stated here, you can also define the local and remote branches to associate without checking out the desired tracking branch like so. `git branch --set-upstream `. For example `git branch --set-upstream master origin/production` Should result in `Branch master set up to track remote branch production from origin.` – Will B. Jun 16 '15 at 03:55
  • @fyrye I'm wondering if and how it's possible without using `--set-upstream` which is deprecated now? – Piotr Dobrogost Oct 11 '15 at 10:54
  • 1
    @PiotrDobrogost The syntax for 1.8+ is `git branch [-u|--set-upstream-to]` for the alternative. As stated in the answer. My comment is about assigning the associated branch without checking out the desired tracking branch. So `git branch --set-upstram-to=origin/production master` or `git branch -u origin/production master` – Will B. Oct 12 '15 at 11:27
  • 3
    one further shortcut/tweak is `git push -u origin HEAD` ... This allows you to skip typing your branch name. You *do* need to make sure you are on the right branch. – Daniel Wabyick Jan 08 '16 at 18:05
  • 29
    Totally not worth dev time. Why doesn't it just do it automatically? – sudo Aug 29 '17 at 23:16
  • What you said may not be correct on git 2.15.1, I got MacBook-Pro:autoservice$ git branch -u origin/restrict error: the requested upstream branch 'origin/restrict' does not exist hint: hint: If you are planning on basing your work on an upstream hint: branch that already exists at the remote, you may need to hint: run "git fetch" to retrieve it. hint: hint: If you are planning to push out a new local branch that hint: will track its remote counterpart, you may want to use hint: "git push -u" to set the upstream config as you push. – user204069 Jun 29 '18 at 23:44
  • @user204069 Have you tried what the error message you got suggests? (That is, either: running `git fetch origin` before trying that command, if the branch `restrict` already exists in the origin repository, or pushing it with `git push -u origin restrict` instead, in which case you wouldn't need the `git branch -u`.) – Mark Longair Jul 01 '18 at 19:38
  • @GaryO it is desirable not to mess the remote repository. See an explanation of why `git push -u origin master` is needed in this answer https://stackoverflow.com/a/17096880/2641825 also mentioned by VonC above. – Paul Rougieux Aug 08 '18 at 08:28
  • @sudo as you say, improving usability is not worth any Git dev's time – 0xC0000022L Oct 06 '20 at 16:03
1532

You can make this happen with less typing. First, change the way your push works:

git config --global push.default current

This will infer the origin my_branch part, thus you can do:

git push -u

Which will both create the remote branch with the same name and track it.

Zamith
  • 20,044
  • 2
  • 21
  • 15
  • 5
    How come can git infer `origin` when running `git push -u` for newly created branch in newly created repository? Is the assumption that repository had been cloned thus current branch has its remote set to `origin`? – Piotr Dobrogost Oct 11 '15 at 11:03
  • `git push -u` set tracking to local branch with the same name, not even remote. – Nakilon Jan 21 '16 at 12:10
  • Did you set the `push.default` config? Also, do you have the remote set up? – Zamith Jan 21 '16 at 15:58
  • 92
    This should be the default. So many things in git could be more user-friendly if it just shipped with better defaults. – phreakhead Feb 11 '16 at 22:29
  • 14
    Be aware that 'current' is slightly unsafer than using 'simple' to do the same thing, see http://stackoverflow.com/questions/23918062/simple-vs-current-push-default-in-git-for-decentralized-workflow – Air Mar 24 '16 at 00:28
  • 31
    It does, but then when you try to `pull` you'll have to specify from where. The `-u` sets up the branch tracking between origin and your local repo. – Zamith May 29 '17 at 15:02
  • @mottlard It also doesn't answer the OP's actual question, which is (paraphrased) _"How does one make git default to tracking the remote branch that's created when one pushes a local branch to the remote for the first time?"_. The `-u` option is all very well but what's the rationale for not having this as the default behaviour in the first place? – Rob Gilliam Jun 14 '17 at 09:22
  • 1
    @PiotrDobrogost adding phreakhead answer, when cloning a repo, git also automatically add the URI as remote with the name origin. I think origin is some defaults for remote repository – Yana Agun Siswanto Jul 28 '17 at 02:34
  • 2
    @Yana Yes, you can name a remote whatever you want. `origin` happens to be the default that git uses. – Zamith Aug 31 '17 at 10:38
  • 1
    There is a further [SO answer](https://stackoverflow.com/a/948397/175584) that explains that `simple` is the default config value so beginners don't mess things up. Therefore for more advanced usage setting it to `current` then makes sense as it's `simple` mode with the addition of creating an upstream branch, if doesn't exist. – Cas Dec 12 '17 at 22:17
  • @pstryk Yes it will work without `-u` but then it would not be tracking the upsteam branch for any changes, depends whether you care about that or not... – Cas Dec 12 '17 at 22:20
  • 1
    I tried every single answer in this post (many of them don't work). I've concluded that this is the easiest method. – wisbucky May 10 '18 at 23:31
  • 1
    This should be the accepted answer - setting this default will make `git push` work as they expected with a new local branch. And as mentioned above, this will work without `-u` – Jamie Doyle Jun 27 '18 at 15:23
  • 21
    While marginally convenient, this *still* mandates that a different command be run for the first and only `push`– which defeats the entire point of this question. In short, **there is no good answer.** That Git developers insist on retaining this Awkward User eXperience (AUX) in the face of widespread community dissent is... enlightening. And discouraging. (Mostly discouraging.) – Cecil Curry Dec 14 '18 at 05:55
  • why use global config if you have multiple git projects set up in your machine? – Paramvir Singh Karwal May 05 '19 at 04:49
  • @ParamvirSinghKarwal you can add it as a config for each project. I however, want this to be the default behavior in all my projects, thus the global setting. – Zamith Jun 07 '19 at 19:12
  • the number of times I've returned to this answer makes me disappointed in the state of user experience for developer tools ‍♂️ – trisweb Aug 12 '20 at 00:28
  • 1
    `git config --global push.default upstream` i think is more what we're looking for here. – Keith Tyler Aug 28 '20 at 02:42
135

This is my most common use for The Fuck.

$ git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

$ fuck
git push --set-upstream origin master [enter/↑/↓/ctrl+c]
Counting objects: 9, done.
...

Also, it's fun to type swear words in your terminal.

Tamlyn
  • 18,041
  • 10
  • 96
  • 117
96

You can simply

git checkout -b my-branch origin/whatever

in the first place. If you set branch.autosetupmerge or branch.autosetuprebase (my favorite) to always (default is true), my-branch will automatically track origin/whatever.

See git help config.

Tobu
  • 23,001
  • 3
  • 85
  • 97
cdunn2001
  • 15,667
  • 7
  • 51
  • 42
  • 5
    This produces "fatal: Cannot update paths and switch to branch 'my-branch' at the same time." – Karl the Pagan May 09 '13 at 15:55
  • @Karl, that rare error is explained [here](http://stackoverflow.com/questions/945654/git-checkout-on-a-remote-branch-does-not-work). – cdunn2001 May 10 '13 at 04:34
  • 12
    By the way, I usually just `git checkout -t origin/whatever`, which also chooses `whatever` as the new branch-name. Very convenient! – cdunn2001 May 10 '13 at 04:35
  • 2
    @cdunn This one is ideal, but hardly consistent. The flag should be called `-u`/`--set-upstream`. – Tobu Jun 06 '13 at 22:38
  • 1
    `git checkout -t origin/whatever` doesn't work for me when trying to create a new branch: `fatal: Cannot update paths and switch to branch 'whatever' at the same time.` – wisbucky May 10 '18 at 22:53
  • 1
    `git checkout -b my-branch origin/whatever` also has the same error (I'm trying to create a new branch that doesn't exist on local or remote): `fatal: Cannot update paths and switch to branch 'whatever' at the same time.` – wisbucky May 10 '18 at 23:26
  • 1
    `branch.autosetupmerge = false` did not work either. It set the upstream tracking to local `whatever` branch instead of remote `origin/whatever`. – wisbucky May 10 '18 at 23:27
86

You can set upstream simpler in two ways. First when you create the branch:

git branch -u origin/my-branch

or after you have created a branch, you can use this command.

git push -u origin my-branch

You can also branch, check out and set upstream in a single command:

git checkout -b my-branch -t origin/my-branch

My personally preference is to do this in a two-step command:

git checkout -b my-branch
git push -u origin my-branch
Tzen
  • 1,280
  • 11
  • 11
  • 1
    Great answer! Addresses both common use cases. After running `git branch -u origin/my-branch` I can run `git pull` to pull down my changes. – Benjamin Atkin Dec 02 '16 at 22:22
  • 5
    "git checkout -b my-branch -t origin/my-branch" this doesn't work if 'origin/my-branch' doesn't exist yet. – Spongman Nov 16 '17 at 18:43
  • 1
    You actually can just do `git checkout -t origin/my-branch` without the `-b my-branch`, it will just automatically infer `my-branch` for the local branch name. However, as @Spongman mentioned, this command doesn't work if `origin/my-branch` doesn't exist first. – wisbucky May 10 '18 at 23:11
  • 1
    Yes, will work @wisbucky, -t works just fine. Personally though, even two years after I wrote that reply though, I still prefer splitting in two lines with checkout -b and push -u. It is more explicit and no error on checkout -b when I don't have remote - which happens quite often when experimenting :) – Tzen May 15 '18 at 07:32
  • 2
    `git push -u origin/my-branch` fails for me with `fatal: 'origin/my-branch' does not appear to be a git repository`. This works: `git push -u origin my-branch` – stason Oct 26 '18 at 20:29
  • You are absolutly correct @stason. My bad, should be a space there. Guess I were typing too fast back in '15 :) I still have my personal preference though. Dunno why, make me feel like I'm more in control I guess. – Tzen Oct 31 '18 at 13:10
60

You can use:

git config --global branch.autosetupmerge always

which will link the upstream branch each time you create or checkout a new branch.

See https://felipec.wordpress.com/2013/09/01/advanced-git-concepts-the-upstream-tracking-branch/

This also works with branch.autosetuprebase, if you follow a more rebase focused workflow, but don't use this unless you know what you're doing, as it will default your pull behavior to rebase, which can cause odd results.

vy32
  • 24,271
  • 30
  • 100
  • 197
Daniel
  • 2,579
  • 3
  • 21
  • 17
  • 14
    Doesn't work, I still get the `--set-upstream` message – Dorian Jul 29 '15 at 15:16
  • 4
    @Dorian, You have to set this before you create the branch. See http://stackoverflow.com/a/9753268/263998 – cdunn2001 Jun 22 '16 at 15:52
  • I get: fatal: The upstream branch of your current branch does not match the name of your current branch... – Riscie Sep 01 '16 at 10:48
  • 8
    but this does not set the tracking branch as the remote with the same branch, but to the current local branch.. so when you do push it will try to push to the LOCAL branch you was before creating the new branch.. – Arnold Roa Aug 20 '17 at 22:10
  • 1
    This has even weirder behavior than the default. If you base work off a branch, it acts really strangely. – Beefster Jun 25 '18 at 15:56
  • 10
    **Be Careful** with this setting!! After setting it, you get this behaviour. 1. Switch to `master`. 2. Run `git checkout -b new_branch`. 3. Add a commit to that branch. 4. `git push origin new_branch`. **This pushes that commit to the `master` branch on origin** (rather than to a new branch on origin called `new_branch`). – stwr667 Dec 06 '19 at 03:30
  • @stwr667 love it, now why would you do `git checkout` to create a branch? ... oh, never mind. – 0xC0000022L Oct 06 '20 at 16:05
41

By the way, the shortcut to pushing the current branch to a remote with the same name:

$ git push -u origin HEAD
djanowski
  • 4,951
  • 1
  • 23
  • 15
31

I personally use these following alias in bash

in ~/.gitconfig file

[alias]
    pushup = "!git push --set-upstream origin $(git symbolic-ref --short HEAD)"

and in ~/.bashrc or ~/.zshrc file

alias gpo="git pushup"
alias gpof="gpo -f"
alias gf="git fetch"
alias gp="git pull"
Jeff Rosenberg
  • 3,402
  • 1
  • 16
  • 37
Amrit Shrestha
  • 1,645
  • 20
  • 23
  • 1
    I only needed to hcange .gitconfig, then I could use the command `git pushup` which always pushes the current branch to the origin. I can always just use `git pushup` instead of `git push` – thespacecamel Oct 12 '18 at 20:52
  • 1
    just expanding on this answer to setup a global git alias (custom commands) - `git config --global alias.pushup "!git push --set-upstream origin $(git symbolic-ref --short HEAD)"` – Naren Nov 10 '20 at 20:50
25

If the below doesn't work:

git config --global push.default current

You should also update your project's local config, as its possible your project has local git configurations:

git config --local push.default current
youngrrrr
  • 2,285
  • 3
  • 20
  • 39
  • 2
    More explanations would be great. What does the first line do? – papillon Sep 03 '19 at 07:40
  • 4
    This answer is the one that feels legit. All the ones proposing aliases are dumb workarounds. And the other ones justifying memorizing long command sequences are pedantic. – MarkHu Nov 22 '19 at 09:23
12

For what it is worth, if you are trying to track a branch that already exists on the remote (eg. origin/somebranch) but haven't checked it out locally yet, you can do:

$ git checkout --track origin/somebranch

Note: '-t' is the shortened version of '--track' option.

This sets up the same association right off the bat.

mattacular
  • 1,839
  • 13
  • 17
  • 5
    You can actually just checkout to the branch. So `git checkout somebranch` is equivalent. – Zamith Apr 08 '14 at 10:15
  • 2
    @Zamith Doesn't that only work after having called `git fetch` immediately beforehand? – Walter Roman Mar 10 '15 at 20:37
  • 2
    Not immediately, but yes, you do need to have a reference to that branch on your local repo, which happens whenever you call `git fetch` or `git pull`. I've never found that to be an issue, though. – Zamith Mar 12 '15 at 13:14
10
git branch --set-upstream-to=origin/master<branch_name>
10

You can also explicitly tell git pull what remote branch to pull (as it mentions in the error message):

git pull <remote-name> <remote-branch>

Be careful with this, however: if you are on a different branch and do an explicit pull, the refspec you pull will be merged into the branch you're on!

mtbkrdave
  • 2,570
  • 3
  • 21
  • 24
9

I use this Git alias instead of copy/pasting the suggestion from Git every time: https://gist.github.com/ekilah/88a880c84a50b73bd306

Source copied below (add this to your ~/.gitconfig file):

[alias]
  pushup = "!gitbranchname() { git symbolic-ref --short HEAD; }; gitpushupstream() { git push --set-upstream origin `gitbranchname`; }; gitpushupstream"
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
manroe
  • 1,335
  • 16
  • 28
7

You can set up a really good alias that can handle this without the overly verbose syntax.

I have the following alias in ~/.gitconfig:

po = "!git push -u origin \"$(git rev-parse --abbrev-ref HEAD)\""

After making a commit on a new branch, you can push your new branch by simply typing the command:

git po
123
  • 8,116
  • 10
  • 45
  • 87
  • why `po`? `push origin`? what happen if this is ran multiple times? – Arnold Roa Aug 20 '17 at 21:20
  • Yes, as in push origin. Nothing happens if it's run multiple times. I also have a `git push -f` alias set up to `git pf`, so I use that once the origin has already been pushed. – 123 Aug 20 '17 at 21:57
  • see [djanowski's comment](https://stackoverflow.com/questions/6089294/why-do-i-need-to-do-set-upstream-all-the-time#39513531), you can directly use `HEAD` – arhak Oct 10 '17 at 08:46
4

For those looking for an alias that works with git pull, this is what I use:

alias up="git branch | awk '/^\\* / { print \$2 }' | xargs -I {} git branch --set-upstream-to=origin/{} {}"

Now whenever you get:

$ git pull
There is no tracking information for the current branch.
...

Just run:

$ up
Branch my_branch set up to track remote branch my_branch from origin.
$ git pull

And you're good to go

jchavannes
  • 1,930
  • 19
  • 12
2

Because git has the cool ability to push/pull different branches to different "upstream" repositories. You could even use separate repositories for pushing and pulling - on the same branch. This can create a distributed, multi-level flow, I can see this being useful on project such as the Linux kernel. Git was originally built to be used on that project.

As a consequence, it does not make assumption about which repo your branch should be tracking.

On the other hand, most people do not use git in this way, so it might make a good case for a default option.

Git is generally pretty low-level and it can be frustrating. Yet there are GUIs and it should be easy to write helper scripts if you still want to use it from the shell.

Rolf
  • 4,872
  • 4
  • 36
  • 52
2

You can also do git push -u origin $(current_branch)

1

We use phabricator and don't push using git. I had to create bash alias which works on Linux/mac

vim ~/.bash_aliases

new_branch() {
    git checkout -b "$1"
    git branch --set-upstream-to=origin/master "$1"
}

save

source ~/.bash_aliases
new_branch test #instead of git checkout -b test
git pull
om471987
  • 4,741
  • 4
  • 27
  • 39
1

Here is a bash alias for git push which is safe to run for every push and will automatically switch between setting upstream for the first push and then doing normal pushes after that.

alias gpu='[[ -z $(git config "branch.$(git symbolic-ref --short HEAD).merge") ]] && git push -u origin $(git symbolic-ref --short HEAD) || git push'

Original Post

Loren
  • 3,421
  • 2
  • 31
  • 43
1

All i wanted was doing something like this:

git checkout -b my-branch
git commit -a -m "my commit"
git push

Since i didn't found a better solution, i've just created an bash alias on ~/.bashrc:

alias push="git push -u origin HEAD"

now just doing a push command does the job (you can add this alias on ~/.gitconfig too with another name, such as pushup)

Maxwell s.c
  • 1,281
  • 11
  • 25
0

I sort of re-discovered legit because of this issue (OS X only). Now all I use when branching are these two commands:

legit publish [<branch>] Publishes specified branch to the remote. (alias: pub)

legit unpublish <branch> Removes specified branch from the remote. (alias: unp)

SublimeGit comes with legit support by default, which makes whole branching routine as easy as pressing Ctrl-b.

Benny K
  • 859
  • 9
  • 8
0

99% of the time I want to set the upstream to a branch of the same name, so I use this (in *nix or Git Bash):

git branch --set-upstream-to=origin/`git branch --show-current`

It's nice because it's branch agnostic. Note the sub-command git branch --show-current prints your current branch name, or nothing if you are detached.

Side note: I have my config set such that I can use git push -u, so I rarely need to do this. But I still do sometimes, and it's usually when I decide I want to reset my local changes to whatever's on the remote, and at that moment I realize I previously pushed without -u. So, typically the next command I'm going to run after setting my upstream, is resetting to the remote branch:

git reset --hard @{u}

Which also happens to be branch agnostic. (Maybe I just really dislike typing in my branch name.)

TTT
  • 7,472
  • 5
  • 41
  • 50