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

Git

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

You can configure it with git config using git config --global push.default current.

Docs: https://git-scm.com/docs/git-config/#Documentation/git-config.txt-pushdefault

Solution 2 - Git

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

Solution 3 - Git

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'

Solution 4 - Git

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 :)

Solution 5 - Git

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) )

Solution 6 - Git

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

Solution 7 - Git

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

Solution 8 - Git

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.

Solution 9 - Git

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.

Solution 10 - Git

Simply:

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

Solution 11 - Git

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

Solution 12 - Git

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.

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
QuestionJohnView Question on Stackoverflow
Solution 1 - GitAndrea BergonzoView Answer on Stackoverflow
Solution 2 - GitmechanicalfishView Answer on Stackoverflow
Solution 3 - GitVonCView Answer on Stackoverflow
Solution 4 - GitFrexuzView Answer on Stackoverflow
Solution 5 - GitMarkView Answer on Stackoverflow
Solution 6 - GitBoris DalsteinView Answer on Stackoverflow
Solution 7 - GitJT JobeView Answer on Stackoverflow
Solution 8 - GitgazdagergoView Answer on Stackoverflow
Solution 9 - GitkoronaView Answer on Stackoverflow
Solution 10 - GitdjanowskiView Answer on Stackoverflow
Solution 11 - GitjveneziaView Answer on Stackoverflow
Solution 12 - GitiamfrankView Answer on Stackoverflow