tekin.co.uk

Git tip: create a Git upstream alias

Here’s a simple git alias to make your life a little easier if you work with feature branches:

  $ git config --global alias.upstream '!git push -u origin HEAD'

Now running git upstream will:

  1. push the branch to the remote origin
  2. set the local branch to track the remote branch so that future git push and git pulls will automatically happen against the remote

When is this alias useful?

This alias will be useful if you’ve ever tried to push up a new feature branch and this has happened:

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

      git push --set-upstream origin feature-x

Git is telling us that it doesn’t know where this branch should be pushed to because (at least by default) it doesn’t make assumptions about where a given branch should be pushed to or pulled from.

Helpfully, Git has told us the command we probably want to run to push to the remote origin with the same branch name and set that as the upstream branch to track.

Our new alias essentially does the same thing, but instead of giving a specific branch name we use HEAD, which is a reference to the tip of the current branch and means the command will work without having to explicitly use the name of the branch:

  $ git upstream
  Enumerating objects: 6, done.
  Counting objects: 100% (6/6), done.
  Delta compression using up to 4 threads
  Compressing objects: 100% (4/4), done.
  Writing objects: 100% (4/4), 1.74 KiB | 297.00 KiB/s, done.
  Total 4 (delta 2), reused 0 (delta 0)
  remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
  ...
   * [new branch]      feature-x -> feature-x
   Branch 'feature-x' set up to track remote branch 'feature-x' from 'origin'.

Alternatively, do it with a config setting

There is a config setting within Git that would let you to run git push -u to achieve the same thing as our new alias: push.default. If you configure that to current, Git will assume you want to push to the same branch on origin as the one you are currently on. Personally I prefer not to use this setting because git push (without -u) will still push the branch but without setting the upstream tracking, and a subsequent git pull will fail unless you specify the branch name explicitly or set tracking:

  $ 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> feature-x

I’ve got a bunch more Git-related tips in the pipeline. If you’re looking to power up your Git skills and understanding, sign up to my mailing list below!

More articles on Git

Authored by Published by

Get more fab content like this straight to your inbox

I send an email whenever I have a new insight or tip to share. Zero spam, and you can unsubscribe whenever you like with a single click.