ChatGPT解决这个技术问题 Extra ChatGPT

Can't push to remote branch, cannot be resolved to branch

I migrated my repos from Bitbucket or Github. I don't think this matters but it's the only thing different. For a little while, I had two remotes set up:

origin: bitbucket
github: github

Then I removed both and pointed origin to github:

git remote remove origin
git remote remove github
git remote add origin https://github....

Test push of develop branch:

git push origin develop

Everything up to date, ok, good.

Create a new branch for some work as per usual:

git checkout -b Feature/Name

Update a file or two. Attempt to push to remote:

git push origin Feature/Name

This results in the error:

fatal: Feature/Name cannot be resolved to branch

Search online for this issue, find some stuff about ensuring HEAD is correct, others about making sure I've got my branch name case correct (though, at this point the branch doesn't exist on the remote yet). Unable to resolve.

Ran this command:

git push --all -u

This got my Feature/Name branch to github, but still see same behavior as prior:

git push origin develop
git push origin Feature/Name

The first works while the second throws the same error. Why?

What branch were you on when you made Feature/Name? Are you sure Feature/Name exists and that's the checked out branch? Check with git branch.
@Schwern - Only three branches existed (locally and on remote): develop, test and master. Once a branch is cleaned up and merged back to develop I delete them locally (and remotely if applicable). I'm certain there were only my three - I haven't opened the project in a while and first thing I did was check and make sure I had no loose branches.
Does that mean you ran git branch to verify Feature/Name exists locally? Don't trust a GUI or IDE. Also, did you get the case right?
How about git push origin Feature/Name:Feature/Name?
Now I'm pissed... not sure why it didn't work in the first place, but after using git push --all -u I have the new branch in github, but still couldn't push from local, right? Here's what happened with that... the actual branch name is SQLMigration/ReportFixes and what is in github is SqlMigration/ReportFixes. So, now I can git push origin SqlMigration/ReportFixes - whytf does github change casing for me? Agh.

T
Ty Le

I was having this issue as well, and it was driving me crazy. I had something like feature/name but git branch -a showed me FEATURE/name. Renaming the branch, deleting and recreating it, nothing worked. What finally fixed it:

Go into .git/refs/heads

You'll see a FEATURE folder. Rename it to feature.


This was the correct answer for me. Was using gitbash on windows, and had created feature/some-feature and Feature/some-feature.
I owe you beer for this answer! :D
Wow, nice catch. Saved me a whole lot of time. Thanks !
@Pegues - done. I don't think this answer existed until long after I had the issue, but it seems to be the popular one, so there ya go.
I just created a new branch with lowercase and a subtle difference on the name and it worked (just changing the name but keeping the uppercase didn't work)
C
Community

Based on my own testing and the OP's comments, I think at some point they goofed on the casing of the branch name.

First, I believe the OP is on a case insensitive operating system like OS X or Windows. Then they did something like this...

$ git checkout -b SQLMigration/ReportFixes
Switched to a new branch 'SQLMigration/ReportFixes'

$ git push origin SqlMigration/ReportFixes
fatal: SqlMigration/ReportFixes cannot be resolved to branch.

Note the casing difference. Also note the error is very different from if you just typo the name.

$ git push origin SQLMigration/ReportFixme
error: src refspec SQLMigration/ReportFixme does not match any.
error: failed to push some refs to 'git@github.com:schwern/testing123.git'

Because Github uses the filesystem to store branch names, it tries to open .git/refs/heads/SqlMigration/ReportFixes. Because the filesystem is case insensitive it successfully opens .git/refs/heads/SqlMigration/ReportFixes but gets confused when it tries to compare the branch names case-sensitively and they don't match.

How they got into a state where the local branch is SQLMigration/ReportFixes and the remote branch is SqlMigration/ReportFixes I'm not sure. I don't believe Github messed with the remote branch name. Simplest explanation is someone else with push access changed the remote branch name. Otherwise, at some point they did something which managed to create the remote with the typo. If they check their shell history, perhaps with history | grep -i sqlmigration/reportfixes they might be able to find a command where they mistyped the casing.


I ran into this problem when I changed the case of characters in the branch name on OS X. Changing them back resolved the issue.
This can also happen when you have a previous branch, say AM-xxx/some_branch, and then create another one AM-XXX/another_branch, git will allow the differing cases locally and fail to pair the two remotely.
Yes it is possible to check out in the wrong mixed case but not check in .. Just messy.
m
moggers

Git will let you checkout the current branch with a different casing, and it will fail to find a ref on the remote.

Just found out the hard way.


this was my issue. I would suggest doing a quick > git branch and verify that your branch has a * next to it.
This happened to me as well. @AndyDangerGagne, I'm glad you suggested this -- there was no * next to the branch that I was on, so I checked it out again, this time in lower case.
I'm not exactly sure I understand what happened. I have a new branch that I couldn't push because of the same reason and thought I would try changing the first letter of the branch name to uppercase and it worked. I'm guessing the branch already existed and I didn't realize it, although i didn't see it.
J
JGL

A similar thing happened to me. I created a branch called something like "Feat/name". I tried to pushed it using :

git push --set-upstream origin Feat/name

I got the same fatal error as you :

fatal: Feat/name cannot be resolved to branch

To solve it I created a new branch as I had very few files impacted. Then I listed my branches to delete the wrong one and it displayed with no cap :

feat/name

I had used caps before but never on the first caracter. It looks like git doesn't like it...


I had the same case THX :D
for my case, this won't work because I have a branch called develop, so ?
S
Sher

It's case-sensitive, just make sure created branch and push to branch both are in same capital.

Example:

git checkout -b "TASK-135-hello-world"

WRONG way of doing:

git push origin task-135-hello-world     #FATAL: task-135-hello-world cannot be resolved to branch

CORRECT way of doing:

git push origin TASK-135-hello-world

M
Mahesh R

Please use small alphabet letters for branch name, don't use capital letters. It will work.


Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
S
Shayan Shafiq

Had the same problem with different casing.

Did a checkout to development (or master) then changed the name (the wrong name) to something else like test.

git checkout development
git branch -m wrong-name test

then change the name back to the right name

git branch -m test right-name

then checkout to the right-name branch

git checkout right-name

then it worked to push to the remote branch

git push origin right-name

D
David

For my case, I used to have branch folder (or whatever it is called) with capital letters, then I create a new one with difference casing (lowercase) but git actually create the branch with capital.

I have created a branch like feature-ABC/branch1 before and pushed it. Then I create a branch feature-abc/branch2 (notice the lower-case ABC), and try to push it to remote using git push --set-upstream origin feature-abc/branch2 and get the 'cannot be resolved to branch' error. So I git branch and see that it actually created feature-ABC/branch2 instead of feature-abc/branch1 for me. I checkout again with git checkout feature-ABC/feature2 and push it using the uppercase (feature-ABC/feature2) to solve it.


j
julianm

I solved this in Windows 10 by using cmd instead of GitBash.

It has to do with character case and how git and command lines handles them.


j
jojo

The cause in my case was that the correct branch name was in uppercase, but the branch name specified in the push command was in lowercase.

$ git branch --contains=HEAD

The above command will tell you the correct branch name, so push it.


S
Shah

I checked out from Feature/name to feature/name and it resolved my issue.


j
joker

Maybe your forgot to run git fetch? it's required to fetch data from the remote repo! Try running git fetch remote/branch


C
Corbin Hudson

I faced the same issue which was due to going to branch with wrong casing. git let me switch to branch with incorrect casing ie feature/Name instead of feature/name. Found an easier solution than listed above just:

commit your changes to 'feature/Name'

git checkout master (or develop)

git checkout feature/name < with correct casing

git push


I
Ivan Nieto

For me git status was giving me the incorrect branch name, hotFix/issue-233 instead of hotfix/issue-233. git branch did display the correct branch name.


I wonder why does git status not show the correct branch name? Very weird.
l
loko

You might have created similar branch but different case-sensitive-wise, then you have to run:

git branch -D <name-of-different-case-branch>

and then try to push again.


r
ryzhman

Slightly modified answer of @Ty Le:

no changes in files were required for me - I had a branch named 'Feature/...' and while pushing upstream I changed the title to 'feature/...' (the case of the first letter was changed to the lower one).


W
Waswa Rodgers

I had the same problem but was resolved. I realized branch name is case sensitive. The main branch in GitHub is 'master', while in my gitbash command it's 'Master'. I renamed Master in local repository to master and it worked! 😀😀


L
Luca Filip

My 2 cents... This problem occurred in my case because of a typo (uppercase letter) in the branch name. I had 2 branches with almost identical names.


Ω
ΩmegaMan

If one is using folders such as Omegaman/BugFix make sure the case is correct. It seems one can checkout an existing branch as lowercase omegaman/BugFix and attempt to push, it will fail.

Recheckout with the proper casing such as git checkout Omegaman/BugFix to resolve.


H
Hui Li

I ran into the same problem caused by incasesensitive in windows system. Use git checkout -b your-new-branch from you current branch and push to remote, then you can find commits in both.


R
Ronaldo Albertini

If you are in local branch, could rename the branch "Feature/Name" to "feature/Name"

git -m feature/Name

if you have problems to make a git push make a checkout in other branch (ex develop) and return to renamed branch

git checkout feature/Name

and try again your git push


P
Peter Boomsma

I just had this issue as well and my normal branches start with pb-3.1-12345/namebranch but I accidental capitalized the first 2 letters PB-3.1/12345/namebranch. After renaming the branch to use lower case letters I could create the branch.


C
Community

for me I was naming branch as

Rel4.6/bug/Some-short-description

all I had to do is when using

git push origin Relx.x/bug/Some-short-description

to write

git push origin relx.x/bug/Some-short-description

as I used to create branches using small letter r in rel.

so, what caused this issue?

when I listed .git/refs/heads content I found

drwxr-xr-x  4 eslam_khoga  staff   128B Sep 22 20:22 relx.x

but no Relx.x!

and inside it bug and inside bug my branch's name.

So, git try to create a dir with same name but different case letters

but system is not case sensitive.

That's what caused this issue!


b
bediV5

I ran into the same issue and noticed that I had mixed up the casing while checking out the branch. I checked out branchName instead of BranchName and when I tried to push to remote, I got the same error.

The fix:

git push --set-upstream origin BranchName

By setting upstream to the correct name, the correct branch was updated on github and I was then able to checkout the correct branch name with

git checkout BranchName 

And it should be up to date with your last push.


b
bigtheo

it seems that you try to rename your master branch to Main. by using this command git branch -M Main where you were on master branch. execute this git command, il will work :

git push --all -u

after this you can run git branch to see your branches then you can delete the master branch like this :

git branch -D master

S
Shayan Shafiq

For me, the issue was that I had git and my macOS filesystem set to two different case sensitivities. My Mac was formatted APFS/Is Case-Sensitive: NO but I had flipped my git settings at some point trying to get over a weird issue with Xcode image asset naming so git config --global core.ignorecase false. Flipping it back aligned the settings and recreating the branch and pushing got me back on track.

git config --global core.ignorecase true

Credit: Git is case-sensitive and your filesystem may not be - Weird folder merging on Windows


D
David MD

Know that branch letters are case sensitive, that's what I face, I tried pushing to "header" instead of "Header"


Please add further details to expand on your answer, such as working code or documentation citations.
C
CodeSamurai

After having the similar problem, I decided to post what worked for me.

I tried to push new branch to the remote repository with the command:

git push --set-upstream origin <branch name copied from Git console after navigating to the repository location>

and got the following status message:

warning: redirecting to <myRepositoryAdress>

fatal: <branch> cannot be resolved to branch

First of all, we migrated Git and I thought that might be the issue, but not.

The actual problem was:

Instead of having branch named: bugFix/UserName/BranchName, it was written as bugfix/UserName/BranchName in the Git console (notice lowercase f here). I figured that out by typing git branch -a and comparing all existing branches with the one that I am checked out to / want to push. How it happened that the console had lowercase f, I still don’t know. Of course, that the name cannot be resolved to a branch if the name of the actual local branch is different from the name you typed when pushing!

In my SmartGit GUI commit was on the correct branch, but I prefer console and pushing from there, so SmartGit was more like a step to check log of the local state and compare if there is some error in the console.

What I learned from this:

Don’t use git push --all –u as some people suggest in the posts that relate to this error if your goal is to push only one local branch.

Better try to figure out what is exactly wrong and why. Then search for the solution. Maybe you also have a typo or some similar inconsistency.


S
Suibur R.

Try this for the error: (feature/test is local branch name)

git branch --set-upstream-to=origin/feature/test feature/test