Git Push New
Tags: git, shell, alias, piping
Published: 2019-08-03 02:34:26

---
title: Git Push New
published: true
tags: git, shell, alias, piping,
canonical_url: https://stephencharlesweiss.com//2019-08-02/git-push-new/
---

Every time I cut a new branch in Git and am ready to submit a PR for review, I get an annoying error reminding me that I need to set the upstream destination.

```
stephen /Users/Stephen/_coding/app_name ➾ git push
fatal: The current branch feature/813/feature-description has no upstream branch.
To push the current branch and set the remote as upstream, use

  git push —set-upstream origin feature/813/feature-description
```

Taking a page from “Manual Work is a Bug” I decided that this time, I’d figure out how to fix it.¹

## <svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>Summarizing The Problem

Before I could figure out a solution, I needed to make sure I understood the problem.

When I tell git to _push_ my branch to a remote, it needs to know where. And, if it doesn’t exist, I need to tell it to create an upstream branch in my remote repository.

Git kindly provides the command for this, which I’ve been copying and pasting every time I’ve ever gotten it.

As someone who hates lifting their hands off the keyboard, each instance was a reminder of a potential optimization.

## <svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>Create A Solution

Having previously found the power of shell aliases and function, I knew I had the tools to make this as easy as any other git command.²

What I didn’t know was how to put the pieces together. In pseudo code, you can imagine a solution like:

```
getCurrentBranch() => {/* … */}

pushNewBranch() => {
  curBranch = getCurrentBranch
  git push --set-upstream origin ${curBranch}
}
```

Fortunately, I’d recently come across _how_ to find my current branch: `git branch --show-current` (in v2.22.0+) or `git branch | grep \* | cut -d ‘ ‘ -f2` for lower versions.

At this point, I could pipe my answer into a git push function, or use a subroutine. I opted for the latter and I now have a new alias in my `~/.zshrc` file:

```
alias gpnew='git push —-set-upstream origin $(git branch --show-current)’
```

This is a _really_ small thing, but it makes the developing experience slightly more enjoyable by eliminating one piece of redundancy.

## <svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>Footnotes

- ¹ [Manual Work is a Bug | ACM Queue](https://queue.acm.org/detail.cfm?id=3197520)
- ² [Next Level Shell: Aliases And Functions](https://www.stephencharlesweiss.com/2019-02-13/next-level-shell-aliases-and-functions/)