How to Fix Fatal: The Current Branch Master Has No Upstream Branch Error in Git

John Wachira Feb 15, 2024
How to Fix Fatal: The Current Branch Master Has No Upstream Branch Error in Git

This article outlines the different methods we can use to solve the fatal: The current branch master has no upstream branch error in Git. We usually get this error when we create a new local branch, make some commits, and attempt a push to the remote repository.

By default, Git is not configured to create corresponding branches in the remote repository when we create them in the local repo.

Let us look at three methods you can use to resolve the error.

Fix the fatal: The current branch master has no upstream branch Error in Git

For an easy walkthrough, we will simulate a scenario where we get the fatal: The current branch master has no upstream branch error on Git by creating a new branch in our local repository, making some commits, and trying to attempt a push.

$ git checkout -b Dev2.1

We made a few changes and committed. Let’s try to push to the remote repository.

$ git push

fatal error after git push

Here are three methods we can use to solve this error.

The first method is the one suggested by Git. However, we highly do not recommend using it, as we will see shortly, but first, let’s see how this command works.

To solve the error, we will run:

$ git push --set-upstream origin Dev2.1

git push set upstream

The command sets up the Dev2.1 branch in our remote repository, which will track the local branch. However, this method can lead to unexpected and weird consequences in your repository.

Let’s check out the second method.

You can also use:

$ git push -u origin Dev2.1

This command will create a remote branch with the same name as our local branch. If you encounter errors, add the all flag to the command, as shown below.

$ git push -u origin --all

We prefer using the -u flag instead of the git push --set-upstream command.

The above methods fix the current issue. What if we want a permanent solution?

Luckily, Git is very malleable; we can tweak the Git configuration file to get a permanent solution which takes us to our third method.

Run the command below to configure Git to create remote branches when we create local branches.

$ git config --global push.default current

We can now use the git push command alone without ever dealing with this annoying error again.

In conclusion, we get the fatal: The current branch master has no upstream branch error when we have not configured Git to create corresponding remote branches when we create local branches.

We have gone through the various methods we can employ to solve this error. You now know what to avoid.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Error