Fix Git's "fatal: no upstream branch" error quickly example

Fatal Git Error: Current branch has no upstream

Branch based development is one of Git’s most beneficial features. It allows developers to experiment in isolated development spaces that nobody else can see unless the branch is explicitly shared with others. Unfortunately, when a developer creates a new branch and pushes to their shared GitHub, GitLab or BitBucket repository, they often encounter the following, dreaded error:

git@upstream-error  /c/branch/push (new-branch)
$ git push origin
fatal: The current branch has no upstream branch.
To push the current branch and set the remote as upstream, use

git push --set-upstream origin new-branch

Set GitHub or GitLab as an upstream

Git’s upstream branch error messages is somewhat self-explanatory. Git’s error message also fairly succinctly describes the solution to the problem. Before you push a new branch to GitHub or GitLab, you must tell your local Git installation about the remote Git repository your branch should be pushed to. The simple solution to the current problem is easily solved by issuing the following Git push upstream command:

git@upstream-error  /c/branch/push (new-branch)
$ git push --set-upstream origin new-branch
Enumerating objects: 3, done.
* [new branch] new-branch -> new-branch
Branch 'new-branch' set up to track remote branch 'new-branch' from 'origin'.

A quick refresh of the account which represents origin in this upstream branch example shows the new branch on GitHub with the all of the content from the latest commit.

git no upstream branch error fix

Git’s ‘no upstream branch’ error can easily be fixed by setting the upstream branch to origin.

Branch has no upstream fix commands

The full set of commands performed in this Git push example that fixes the no upstream-branch error are as follows:

git@upstream-error  /c/branch/push (new-branch)
git push --set-upstream origin new-branch
cd git*
git switch -c new-branch
git branch -a
touch devo.html
git add .
git commit -m "Are we not men?"
git push --set-upstream origin new-branch
touch echo.html
git add .
git commit -m "Echo done"
git push origin

Most modern software development teams only have one central repository to which developers push and pull, and the alias for that central team repository is almost always origin.

So this step seems set the upstream repository the first time a new Git branch is pushed seems both unnecessary and non-intuitive to many. But as confusing as this required step may be, just think of it as a reminder of the fact that Git is a very powerful, distributed version control tool that has the ability to push and pull to many upstream repositories, not just the one, central, GitHub or GitLab server most teams use for their project.