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.ΒΉ
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.
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.
Top comments (0)