4

Here is the thing:

$ git checkout -b new-branch
Switched to a new branch 'new-branch'

$ git push
...
 * [new branch]      new-branch -> new-branch

$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to rebase against.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> new-branch

I've seen other questions, and the answers suggest doing git push -u when doing the first push. But I always forget doing this.

Is there a configuration option to automatically set the upstream tracking branch when a new branch is pushed from my repo to the remote repo?

chwarr
  • 6,777
  • 1
  • 30
  • 57
warvariuc
  • 57,116
  • 41
  • 173
  • 227

3 Answers3

3

Is there a configuration option to [imply -u when the branch gets created by git push]?

There probably should be, but there isn't. You can, however, fix things up afterward using git branch --set-upstream-to, just as in the Git error message.

As in the other answers, you can set up some alias(es). To expand a bit on fracz's answer, it is safe use the -u option every time: this will set the upstream on every push, but assuming you are always pushing to the same place, you will "change" the upstream from origin/branch to origin/branch each time, i.e., not actually change anything.

Except for the progress output, there's not even a way to tell whether git push actually created the branch on the remote. (The push command itself can tell, but it only reports this as that message, [new branch] or not [new branch].) You can get fairly close by running git ls-remote before the push, but there is a race here: if ls-remote reports that the branch did not exist, and the push succeeds, you don't know for sure that your push created it, because there's a very slight possibility that someone else created it, but your push succeeded anyway.

It is, however, easy to tell whether there is an upstream before your git push started.

Here's a (somewhat imperfect and totally untested) script that does what I think git push should probably actually do.

#! /bin/sh
#
# Push, and set upstream if the push succeeds and
# there was no upstream set before the push started,
# based on "push.set-upstream" setting

die() {
    printf "%s\n" "$1" 1>&2
    exit 1
}

get_current_branch() {
    git symbolic-ref -q --short HEAD ||
        die "fatal: not currently on a branch"
}

get_remote() {
    git config --get branch."$1".remote || echo origin
}

upstream_is_set() {
    git rev-parse -q --verify "$1@{u}" >/dev/null 2>&1
}

# arguments are: [remote [branch]]
# we do not accept arbitrary refspecs here!

case $# in
0)  branch=$(get_current_branch) || exit $?
    remote=$(get_remote "$branch")
    ;;
1)  remote="$1"
    branch=$(get_current_branch) || exit $?
    ;;
2)  remote="$1" branch="$2";; # should check $2 ref format?
*)  die "usage: gitpush [remote [branch]]";;
esac

how_to_set=$(git config --get push.set-upstream || echo auto)

case "$how_to_set" in
always|true) do_set=true;;
false) do_set=false;;
auto|only-if-unset) if upstream-is-set "$branch"; then
        do_set=false
    else
        do_set=true
    fi;;
*) die "fatal: don't understand push.set-upstream=$how_to_set";;
esac

# figured everything out, so now push; fail if push fails
git push "$remote" "$branch" || exit $?

# last, set upstream if still needed
if $do_set; then git branch --set-upstream-to "$remote/$branch"; fi
Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775
  • > You can, however, fix things up afterward using git branch `--set-upstream-to`, just as in the Git error message. < Well, if at least the message would be ready to be copied/pasted. But I have to manually replace ``. – warvariuc Jun 23 '16 at 05:04
  • > It is, however, easy to tell whether there is an upstream before your git push started. < Why not pushing, checking for `[new branch]` in the stdout and doing `--set-upstream-to`? It solves the race condition and is faster -- only the first time is done. – warvariuc Jun 23 '16 at 05:09
  • Because (a) it's not a message to stdout (it goes to stderr); (b) it's not produced in some cases (though you can force it with `--progress`); and (c) it's merely a side effect of the implementation; it is internationalized (so you would have to override `$LANG`); and it has changed formats in the past, so it may change format again in the future. In other words, it's not terribly reliable. – torek Jun 23 '16 at 05:55
2

You can achieve something you can live with by creating an alias that would automatically use the -u switch. For example:

git config --global alias.pu "push -u"

And then use it like

git pu

Unfortunately, you can't override any original git command with a git alias. You can try to shadow git push command with a bash alias and add the -u switch by default this way.

Community
  • 1
  • 1
fracz
  • 20,536
  • 18
  • 103
  • 149
-1

Use git push -u origin <branch> as your first push. this will set up tracking.

mkeil
  • 134
  • 6