How many times did you try to make git push for a newly created branch to the remote repository, only to get a strange “no upstream branch” error? Here I will show how to change Git configuration with a single command to get rid of it for good.

This article is part 2 of 3 in the series Basic Git configuration for better and faster work

No upstream branch error

When we create a new branch and try to push it with simple git push command, Git rejects it with a “no upstream branch” error.

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

    git push --set-upstream origin feature-1

This means that Git doesn’t know to which remote repository it should push our branch, and how this branch should be named on that remote repository. But then it suggests a solution. Git suspects that we want to push to default remote, called origin, and name the remote branch exactly the same as a local one.

Traditional solution

So we need to copy or write this longer version of the command, suggested in the error message. Since then Git will know that the “upstream” branch for our local one is an origin/feature-1. We can check it with git branch -vv command. Upstream will be shown in square brackets and blue font.

# create new branch
git checkout -b feature-1

# push with --set-upstream parameter
git push --set-upstream origin feature-1

# show branches and their upstreams
git branch -vv

But in most workflows, we will always want to have the remote branch named exactly the same as the local one. So do we really need to repeat ourselves? Even if it’s only one time, when we try to push a new branch, it’s still distractive. And who likes to get errors like this?

Simplifying Git branch first push

To make push simpler, we set the following configuration with a command.

git config --global push.default current

Alternatively, we can do this by adding lines to ~/.gitconfig file to do the same.

[push]
	default = current

Now, we can create new branch and push it with a little shorter command.

git push -u

-u is a shorter version of the --set-upstream, so the flag still stays. But we don’t need to specify remote and branch names anymore. The configuration we’ve introduced makes Git set default upstream configuration for the branch when we use -u flag, with default parameters. As a result, it will use default remote, origin, and the same branch name as the local one. Sadly, there is no option right now to make Git set tracking information by default. We always have to do it once.

If we push the new branch without the -u flag, it will work as well. However, when we try to pull changes from it later, we will get a familiar error.

There is no tracking information for the current branch.
Please specify which branch you want to merge with.
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-2

Thankfully, even if we push branch without setting upstream and later get this error on pull, we can fix it by simply calling git push -u at any time. Or by calling git branch command suggested by the error message.

Summary

Thanks to setting a simple configuration option to Git push command we avoid Git errors. Now we are able to push newly created branches with a shorter, easier to remember syntax. This small optimization will make our workflow more pleasant and less distractful.

Did you like this article? Join the free newsletter and never miss new valuable content.

Categories: Tools

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Anonymous commenter (not Agnieszka)
Anonymous commenter (not Agnieszka)
4 months ago

Please write something about Scrum. Scrum is the best!

1
0
Comment and share your thoughts!x
()
x