John
John

Reputation: 30195

How to configure git push to automatically set upstream without -u?

I want git push origin to automatically set the upstream reference when I push a locally-created branch for the first time.

I know about git push -u, but I don't want to have to think about whether or not I've used -u before or otherwise set an upstream reference. In other words, I want git push to automatically have the effect of git push -u on any push of a branch that doesn't already have an upstream.

Is this possible? If it requires an alias or utility script, that's fine.

Upvotes: 260

Views: 88184

Answers (13)

Andrea Bergonzo
Andrea Bergonzo

Reputation: 5170

You can configure it with git config --global push.default current (docs) to make it push the current branch to update a branch with the same name.

2022 Update (git>=2.37.0)

git config --global --add --bool push.autoSetupRemote true achieves the same while ALSO setting up the upstream tracking (docs).

Config options we are using:

  • --global sets the config option in ~/.gitconfig rather than in our repository direcly, and it will be applied to all other repositories, unless we have overwritten in our repository;
  • --add - adds a new line to the option without altering any existing values;
  • --bool - historical options for selecting a type specifier, which is bool (true or false) in our case;
  • push.autoSetupRemote true the option we are setting to true in this command, enables a feature where pushing a new branch to a remote repository automatically sets up that branch to track the remote branch with the same name.

Upvotes: 298

VonC
VonC

Reputation: 1325047

2022: Git 2.37 proposes:

git config --global push.autoSetupRemote true

push.autoSetupRemote

If set to "true" assume --set-upstream on default push when no upstream tracking exists for the current branch;

This option takes effect with push.default options 'simple', 'upstream', and 'current'.

It is useful if by default you want new branches to be pushed to the default remote (like the behavior of 'push.default=current') and you also want the upstream tracking to be set.
Workflows most likely to benefit from this option are 'simple' central workflows where all branches are expected to have the same name on the remote.


2013: Note: the fact that the new default push policy "simple" relies on a branch having an upstream one means that:
setting an upstream branch is viewed as a voluntary step, not an hidden automated one

When "git push [$there]" does not say what to push, we have used the traditional "matching" semantics so far (all your branches were sent to the remote as long as there already are branches of the same name over there).

We will use the "simple" semantics that pushes the current branch to the branch with the same name, only when the current branch is set to integrate with that remote branch.
There is a user preference configuration variable "push.default" to change this.


So building up from mechanicalfish's answer, you can define an alias, with the right double quotes (") escaped (\"):

git config alias.pu "![[ $(git config \"branch.$(git rev-parse --abbrev-ref HEAD).merge\") = '' ]] && git push -u || git push"

git pu origin

Sc0ttyD proposes in the comments the following alias:

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

In multiple lines:

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

Upvotes: 104

Achim Abeling
Achim Abeling

Reputation: 46

Today I came across (a new?) option "push.autoSetupRemote". git config help says:

If set to "true" assume --set-upstream on default push when no upstream tracking exists for the current branch; this option takes effect with push.default options simple,
       upstream, and current. It is useful if by default you want new branches to be pushed to the default remote (like the behavior of push.default=current) and you also want
       the upstream tracking to be set. Workflows most likely to benefit from this option are simple central workflows where all branches are expected to have the same name on
       the remote.

Upvotes: 3

Frexuz
Frexuz

Reputation: 4933

I've had the same problem. I've made this alias (from my .gitconfig)

[alias]
    track = "!git branch --set-upstream-to=origin/`git symbolic-ref --short HEAD`"

Usage:

  1. Once per new branch (currently checked out): git track
  2. Push as normal :)

Upvotes: 24

korona
korona

Reputation: 2229

The only completely honest answer to this is "you can't".

I've read all the responses in this, and other questions that ask the same thing.

Every answer posted still requires you to pass special parameters on your first push to a new branch.

Upvotes: 5

gazdagergo
gazdagergo

Reputation: 6691

If you wanna use the built-in git features only with the less possible keypress, just type:

$ git push -u o tab H tab

and the autocomplete will give you $ git push -u origin HEAD

To enable autocomplate on OSX set up a ~/.git-completition.bash file with this content and add the following lines to your ~/.bash_profile file and restart your terminal:

# git branch autocomplete
if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

It affects built-in terminals too, like the one in vscode etc.

Upvotes: 5

Raphael
Raphael

Reputation: 562

If, for whatever reason, none of the other answers work for you, then you can substitute git push with this bash function to automatically re-send the push request with the correct flags when necessary.

gitpush()
{
    git push -v 2>&1 | # perform push command, pipe all output
    tee /dev/tty | # keep output on screen and pipe it forward
    (
     cmd=$(sed -n "s/^.*\(git push --set-upstream origin .*\)$/\1/p");
     [[ -n "${cmd// }" ]] && (echo "> $cmd"; eval $cmd);
    ) # if we get output that matches the command to perform, execute it
}

You will be sacrificing the progress portion of the push output, but other than that everything works as expected.

Personally, I will be using JT Jobe's answer.

Upvotes: 1

Boris Dalstein
Boris Dalstein

Reputation: 7748

Short answer

If you actually like to be explicit and use the -u option when necessary, but just don't want to type the whole:

git push -u origin foo

Then you can use the following alias:

[alias]
    push-u = !git push -u origin $(git symbolic-ref --short HEAD)

And simply type:

git push-u

Long answer

Typically, the need for -u (shorthand for --set-upstream) is when we have just created a new local branch and commit, and we want to push it upstream. The remote repository doesn't yet have the new branch, so we need to tell git to create and track the remote branch before pushing the commit. This is only necessary for the first push on the branch. Here is a typical scenario:

git checkout -b foo         # Create local branch
git commit -m "Foo"         # Create local commit
git push -u origin foo      # Create and track remote branch, and push commit
git commit -m "Bar"         # Create local commit
git push                    # Push commit

Personally, I do like the need to be explicit with git push -u when creating the remote branch: it's a pretty significant operation, sharing a whole new branch to the world.

However, I hate that we have to explicitly write git push -u origin foo. Not only it is a pain to type, but more importantly, it's quite error-prone! It's easy to make a mistake when typing the branch name, and the new remote branch won't have the same name as your local branch! In most cases, really, you want the upstream repository to be origin, and the upstream branch to have the same name as your local branch.

Therefore, I'm using the following alias in my .gitconfig, which is a subset of the excellent answer provided by Mark:

[alias]
    push-u = !git push -u origin $(git symbolic-ref --short HEAD)

Now, we can do the following, which is still explicit, but less error-prone:

git checkout -b foo         # Create local branch
git commit -m "Foo"         # Create local commit
git push-u                  # Create and track remote branch, and push commit
git commit -m "Bar"         # Create local commit
git push                    # Push commit

Upvotes: 9

jvenezia
jvenezia

Reputation: 900

I made a git extension with useful scripts, including this one:

usage: git line push

Push the current branch and set an upstream if needed.

https://github.com/jvenezia/git-line

Upvotes: 1

JT Jobe
JT Jobe

Reputation: 113

I solved this issue by using this simple Bash script. It won't work on existing branches, but if you create all of your branches with this function, you'll always have your upstream branch set automatically.

function con { git checkout -b $1 && git push --set-upstream origin $1; }

The $1 represents the first argument you pass after con so it's just like doing:

git checkout -b my-new-branch && git push -u my-new-branch

...by just doing this:

con my-new-branch

Upvotes: 7

djanowski
djanowski

Reputation: 5858

Simply:

$ alias gush="git push -u origin HEAD"

Upvotes: 3

Mark
Mark

Reputation: 1053

The answers by @VonC and @Frexuz are helpful, but both of their solutions produce an error for me. Using both of their answers, I cobbled together something that works for me:

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

This results in executing either git push -u origin $BRANCHNAME or git push, depending on whether its upstream (property branch.$BRANCHNAME.merge) is defined.

Entering this alias on the command line will require escape codes, so it's probably easiest to use an editor to insert into the correct file ($HOME/.gitconfig (global), .git/config (local), or /etc/gitconfig (system) )

Upvotes: 19

mechanicalfish
mechanicalfish

Reputation: 12826

Since I don't think this is possible using git config, here is what you can do in bash:

[[ $(git config "branch.$(git rev-parse --abbrev-ref HEAD).merge") = '' ]] && git push -u || git push

If the current branch has a remote tracking branch, it calls git push otherwise it calls git push -u

Upvotes: 36

Related Questions