Aaron Digulla
Aaron Digulla

Reputation: 328614

fatal: The upstream branch of your current branch does not match the name of your current branch

After doing a checkout of the remote branch releases/rel_5.4.1 using the Git GUI, I'm seeing this unexpected error message when I try to push:

fatal: The upstream branch of your current branch does not match
the name of your current branch.  To push to the upstream branch
on the remote, use

    git push origin HEAD:releases/rel_5.4.1

To push to the branch of the same name on the remote, use

    git push origin rel_5.4.1

I don't know what Git is talking about. I probably want to push to origin releases/rel_5.4.1 since that's the branch which I checked out. So neither option seems correct to me.

git status says I'm on branch rel_5.4.1.

Here is the branch as it appears in my .git/config:

[branch "rel_5.4.1"]
    remote = origin
    merge = refs/heads/releases/rel_5.4.1

What is going on?

Upvotes: 175

Views: 194715

Answers (8)

rebinnaf
rebinnaf

Reputation: 513

Probably you have renamed your branch in local(and even pushed it) but you haven't set the new branch as upstream branch so you need to set it:

git push -u origin new-branch

Upvotes: 0

zabi club
zabi club

Reputation: 31

git push --set-upstream origin new-branch

You can use your own branch name in place of new-branch.

Upvotes: 3

Akshay Kamble
Akshay Kamble

Reputation: 55

When we faced above issue while pushing the data in specific branch:

Before applying the fix make sure that you commit and merge the data locally first. Use git status to check for any modified files, etc.

Upvotes: -2

Tino
Tino

Reputation: 10469

ATTENTION! While this answer has the most votes and is technically correct, it suggests that the problem is the push.default option, when usually the real problem is an unintended mismatch between the names of the local branch and the upstream branch. blindly following the instructions in this answer may cause your changes to be pushed to the wrong branch! For a safe quick fix, please see https://stackoverflow.com/a/24865780/2279059 instead.

For the benefit of the readers who might miss the probably most important detail, well hidden in the comments:

This is due to the git config push.default setting. It defines what git does when you enter git push (see link).

In the question, apparently the setting was set to simple (which is the default for git v2), probably with

git config --global push.default simple

This means, that git refuses to push when the local and remote branch do not match exactly.

As @TomSpurling notes, above setting is safer and recommended for normal use, because usually you want the same names for your local and remote branches.

However in certain situations, when your local branch is tracking some different remote branch with a different name, then you want to change that:

To allow to push to the tracking branch on a per-git basis, thus make git pull and git push symmetric, use

git config push.default upstream

Note: To globally set this for all of your gits, use git config --global push.default upstream
However it is probably better to leave it to git config --global push.default simple and only set this option in those workloads, where it is really required.

Upvotes: 117

gpap
gpap

Reputation: 2864

This happens if the name of the upstream branch and local branch do not match, which sometimes happens, and usually is unwanted:

> git status
On branch release-1.2.3
Your branch is up to date with 'origin/master'.

To solve this, run:

git branch --unset-upstream

Then, once you run git push again, you will be asked to use the --set-upstream option to set the upstream branch correctly.

Upvotes: 264

s.meijer
s.meijer

Reputation: 3909

This error can be fixed for once and all, with:

git branch releases/rel_5.4.1 -u origin/releases/rel_5.4.1

It changes the upstream of the branch, to match the correct remote (again).

Upvotes: 46

Aaron Digulla
Aaron Digulla

Reputation: 328614

Your local branch is called rel_5.4.1 but the remote branch is releases/rel_5.4.1 (as far as Git is concerned, the / has no special meaning in branch names except to make them easier to read for the human eye).

When you push, Git is wary whether you want to push your branch to releases/rel_5.4.1 (the name of the remote branch) or whether you want to create a new remote branch. It does notice the similarity of names, though.

Unless you want to create a new branch, the correct command is

git push origin HEAD:releases/rel_5.4.1

You could also use

git push origin rel_5.4.1:releases/rel_5.4.1

To fix the warning once and for all, rename your local branch to match the remote name:

git branch -m releases/rel_5.4.1

Upvotes: 74

hek2mgl
hek2mgl

Reputation: 158020

Seems like having a local branch name which is different than the remote is not what Git likes too much. You will need to issue:

git push origin HEAD:releases/rel_5.4.1

explicitely on every push

Upvotes: 3

Related Questions