461

I just encountered a problem when merging a branch into master in git. First, I got the branch name by running git ls-remote. Let's call that branch "branch-name". I then ran git merge branch-name command and got the following result:

fatal: branch-name - not something we can merge

How do I resolve this error?

Brian
  • 14,610
  • 7
  • 35
  • 43
  • The only thing at worked for me was using command line auto-complete. I typed git and auto-completed merge and branch-name. Even copy pasting the branch name failed so it's possible there were hidden characters and this got rid of them. I'm on bash 4.4 and not sure if this works with all shells. Auto-completing git commands is a new thing to me. – Mote Zart Mar 01 '22 at 22:06

33 Answers33

570

As shown in How does "not something we can merge" arise?, this error can arise from a typo in the branch name because you are trying to pull a branch that doesn't exist.

If that is not the problem (as in my case), it is likely that you don't have a local copy of the branch that you want to merge. Git requires local knowledge of both branches in order to merge those branches. You can resolve this by checking out the branch to merge and then going back to the branch you want to merge into.

git checkout branch-name
git checkout master
git merge branch-name

This should work, but if you receive an error saying

error: pathspec 'remote-name/branch-name' did not match any file(s) known to git.

you need to fetch the remote (probably, but not necessarily, "origin") before checking out the branch:

git fetch remote-name
Community
  • 1
  • 1
Brian
  • 14,610
  • 7
  • 35
  • 43
  • 5
    This happened to me after adding a new remote - I needed to do a `git fetch` first before merging the remote branch in. – Jason May 13 '14 at 19:18
  • fetching and checkout out branch from remote. git fetch && git checkout BranchName – Juni Brosas Jun 17 '16 at 02:04
  • 2
    This happened to me when I was cd'd into the wrong project (i.e. it was a different repo that didn't even have the branch I wanted to merge) – JoelFan Nov 23 '16 at 16:58
  • 1
    If you're trying to sync a fork at the command line (https://help.github.com/articles/syncing-a-fork/) this error is probably because you missed step 0. What? There's no step 0 listed? Yes, that's why it's easy to miss. "Before you can sync your fork with an upstream repository, you must configure a remote that points to the upstream repository in Git." <-- that is step 0. If you skip that step you get the above error, which you probably put into Google, leading you here. :-) – Steve Bonds Mar 13 '18 at 00:16
  • In my case I had a branch name `v1.0(1)` and changing it to `v1.0-1` resolved the error. – Manuel Jun 03 '18 at 12:25
  • 3
    `Git requires local knowledge of both branches in order to merge those branches` – Gangadhar Jannu Mar 20 '19 at 07:47
  • 1
    Worked for me (i have cloned from repository) – yasin-munshi Sep 29 '19 at 02:43
  • This occurred when I fetched but **did not checkout** the desired branch. – Silidrone Mar 22 '20 at 06:14
  • Worked like a charm! I didn't have a local version of my remote branch. – Taslim Oseni Aug 17 '20 at 11:54
  • I was trying to merge a branch with name in lowercase (the real name was in uppercase) and got merge: origin/branch_name - not something we can merge – Igor Vuković Dec 15 '20 at 13:17
  • I was using the wrong casing – Ian Mar 09 '22 at 18:21
  • Simply you can use `git branch` to list branches after that you can use `git merge BRANCH_NAME` – Omid Ostovari Sep 08 '22 at 12:34
162

It's a silly suggestion, but make sure there is no typo in the branch name!

pkamb
  • 33,281
  • 23
  • 160
  • 191
endless
  • 3,316
  • 4
  • 26
  • 33
100

When pulling from a remote upstream, git fetch --all did the trick for me:

git remote add upstream [url to the original repo]
git checkout [branch to be updated]
git fetch --all
git merge upstream/[branch to be updated]

In other cases, I found the "Not something we can merge" error will also happen if the remote (origin, upstream) branch does not exist. This might seem obvious, but you might find yourself doing git merge origin/develop on a repo that only has master.

Eneko Alonso
  • 18,884
  • 9
  • 62
  • 84
  • 7
    I seriously don't know why this answer has not garnered as many votes as the above one. 'git fetch -all' is the command that one typically misses to run before merging a remote branch and that solved the problem for me. – Dayanand Gowda Mar 31 '16 at 16:34
  • 2
    Because fetch does not pull automatically and you have to do this manually. So a pair fetch --all & pull --all will do the trick. – danielpopa May 18 '16 at 11:27
  • 1
    Thank you! The `git remote add upstream` was the important thing I was missing that solved it for me. I think a common mistake is assuming a fork automatically knows where it was forked from. – Brent May 26 '16 at 20:43
37

I had this issue as well. The branch looked like 'username/master' which seemed to confuse git as it looked like a remote address I defined. For me using this

git merge origin/username/master

worked perfectly fine.

spekulatius
  • 1,434
  • 14
  • 24
31

The below method works for me every time.

git checkout master
git pull
git checkout branch-name-to-be-merged
git pull
git checkout branch-name
git pull
git merge branch-name-to-be-merged
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
R.N.V.
  • 339
  • 3
  • 3
  • This worked for me thank you. I this is essentially the right solution: basically make sure you have the source branch checked out before trying to merge it in. – a2f0 Jun 03 '19 at 21:31
  • This worked for me, but not sure why? Can you explain? – tarekahf Jan 10 '22 at 19:32
16

It may happen because that branch is not on your local. before merging use

git fetch origin
Alok Kamboj
  • 1,017
  • 11
  • 12
16

This answer is not related to the above question, but I faced a similar issue, and maybe this will be useful to someone. I am trying to merge my feature branch to master like below:

$ git merge fix-load

for this got the following error message:

merge: fix-load - not something we can merge

I looked into above all solutions, but not none of the worked.

Finally, I realized the issue cause is a spelling mistake on my branch name (actually, the merge branch name is fix-loads).

yala ramesh
  • 3,362
  • 1
  • 22
  • 35
10

You might also encounter this error if you are not using origin keyword and the branch isn't one of your own.

git checkout <to-branch> 
git merge origin/<from-branch>
voidone
  • 333
  • 1
  • 3
  • 12
9

I had this issue today when I was merging another branch into mine after directly pulling from master, I had to checkout and pull the branch I was merging first, then I could merge this branch into mine successfully.

git checkout branch-to-merge
git pull
git checkout my-branch-name
git merge branch-to-merge
David B
  • 889
  • 2
  • 11
  • 29
8

You are getting this error because the branch you want to merge doesn't exist on your local repository.

So, first checkout the brach you want to merge into master branch by the following command:

git checkout branch_name_to_merge

After this try to merge it with master branch by the following command:

git merge branch_name_to_merge
Bilal Ahmed Yaseen
  • 2,506
  • 2
  • 23
  • 48
  • 2
    This can also happen if you aren't paying attention to the name of your branch (i.e. misspelled) :) – Matt Borja Sep 29 '16 at 20:47
  • This worked for me. When I am working in Atom with the Git tab and I switch branches with the drop down, sometimes I have to go to the command line and `checkout` the branch – nzaleski Mar 05 '18 at 16:11
7

This error suggest that the branch from where you want to merge changes (i.e. in you case branch-name) is not present in you local, so you should checkout the branch and fetch the local changes. Checkout to your master branch and fetch, then follow the below steps:

git checkout branch-name
git pull
git checkout new-branch-name
git merge branch-name
Amar Magar
  • 840
  • 1
  • 11
  • 15
5

I must suggest to check all branches or the branch you are looking for is available at first

git branch -r

check from the list

origin/HEAD -> origin/main
origin/feature/branch_1
origin/feature/branch_2
origin/feature/branch_3
origin/feature/branch_4
origin/feature/your branch

Suggestion is to copy the listing from origin itself then do git merge origin/feature/branch_2. Copy pasting will remove typo error.

Optimist Rohit
  • 428
  • 6
  • 24
3

I got this error when I did a git merge BRANCH_NAME "some commit message" - I'd forgotten to add the -m flag for the commit message, so it thought that the branch name included the comment.

3

In my opinion i had missed to map my local branch with remote repo. i did below and it worked fine.

git checkout master
git remote add origin https://github.com/yourrepo/project.git
git push -u origin master
git pull
git merge myBranch1FromMain
slfan
  • 8,950
  • 115
  • 65
  • 78
2

If the string containing the reference is produced by another Git command (or any other shell command for that matter), make sure that it doesn't contain a return carriage at the end. You will have to strip it before passing the string to "git merge".

Note that it's pretty obvious when this happens, because the error message in on 2 lines:

merge: 26d8e04b29925ea5b59cb50501ab5a14dd35f0f9
 - not something we can merge
ocroquette
  • 3,049
  • 1
  • 23
  • 26
  • 3
    Please provide a comment when you downvoted an answer. Maybe it was not the issue the original poster had, but it's a possible cause of the error message (I had the problem myself). – ocroquette Jan 16 '15 at 09:04
2

We got this error because we had a comma (,) in the branch name. We deleted the local branch, then re-checked it under a new name without the comma. We were able to merge it successfully.

Doug
  • 21
  • 2
2

In My case, the Problem was in Branch Name. My new branch name contains brackets, Once I renamed it and remove brackets, It's sorted.

PoojaArora
  • 31
  • 3
2

I know it is late to answer but I also faced the same issue so I ran following commands

git checkout master

and then

git pull origin branch-name

It solved my problem

Vikas Chauhan
  • 1,276
  • 15
  • 23
2

to avoid typo error you can do

git fetch --all -v

then copy the name of your branch and do

git checkout <branch-name>
git checkout master
git merge <branch-name>
Adán Escobar
  • 1,729
  • 9
  • 15
1

For posterity: Like AxeEffect said... if you have no typos check to see if you have ridiculous characters in your local branch name, like commas or apostrophes. Exactly that happened to me just now.

Bennett Elder
  • 118
  • 1
  • 8
1

I suggest checking if you are able to switch to the branch that you are trying to merge with.

I got this error even though the branch I wanted to merge with was in local repository and there were no spelling errors.

I ignored my local changes so that I could switch to the branch (Stash or commit can also be preferred). After this I switched back to the initial branch, and the merge was successful.

eaykin
  • 3,713
  • 1
  • 37
  • 33
1

The branch which you are tryin to merge may not be identified by you git at present so perform git branch and see if the branch which you want to merge exists are not, if not then perform git pull and now if you do git branch, the branch will be visible now, and now you perform git merge <BranchName>

yogeswaran
  • 11
  • 2
1

I had the same problem. I fixed it using the command below:

git checkout main
git fetch
git checkout branch_name
git fetch
git checkout main
git fetch
git merge branch_name
Jaied
  • 900
  • 9
  • 13
1

In my case I was trying to execute git commands from a sub-directory of the git repository. Please make sure its the directory you initialized with git and not its sub-directory.

TonyStark
  • 51
  • 5
1

My goal is to merge Branch2 code into Branch1.

I got the below message while merging.

My branches Branch1 and Branch2. Please avail these branches in your project folder.

Initially i have only Branch1 with maser, in this case i got the below mentioned message.

enter image description here

Now I have checked out into Branch2.

Now Branch1, Brnach2 and master all are available in my project folder.

Now the merging steps are begin:

  1. git checkout Brnach1
  2. git merge Branch2

Finally we merged Brnach2 with Branch1

Naresh
  • 16,698
  • 6
  • 112
  • 113
1

There can be many reasons for the same:

git merge dependabot/npm_and_yarn/storybook/addon-essentials-6.4.19
merge: dependabot/npm_and_yarn/storybook/addon-essentials-6.4.19 - not something we can merge
  1. You are having a branch name with special characters.(as in my case): Solution: Checkout a new branch with easy name from the branch which is not able to merge. That could help.
git checkout -b feature-addon
git checkout dev
git merge feature-addon

  1. This error can arise from a typo in the branch name because you are trying to pull a branch that doesn't exist.

  2. Very silly but it is likely that you don't have a local copy of the branch that you want to merge.. Try git fetch to get it

Piyush Sarin
  • 133
  • 1
  • 5
1

If you are following old tutorials, insted of master check if the branch is named main or something similar.

The error is telling you that branch-name must be the name of one of your branches.

Gabriel Arghire
  • 1,992
  • 1
  • 21
  • 34
0

For me the problem occured when I tried this:

git merge -s ours --no-commit --allow-unrelated-histories <remote name>/develop

So actually I should have written master instead of develop,because master was the branch name of Subtree,not my actual branch.

Levon Petrosyan
  • 8,815
  • 8
  • 54
  • 65
0

This may sounds weird, but remember to setup your git email and name:

git config --global user.email "MY@EMAIL.COM"
git config --global user.name "FIRST_NAME LAST_NAME"
superarts.org
  • 7,009
  • 1
  • 58
  • 44
0

For me, the problem was the 'double quotation marks' into merge message. So when I removed the double mark, all magically worked. I hope to help someone. (Sorry for my poor English)

0

I had a work tree with master and an another branch checked out in two different work folders.

PS C:\rhipheusADO\Build> git worktree list
C:/rhipheusADO/Build         7d32e6e [vyas-cr-core]
C:/rhipheusADO/Build-master  91d418c [master]

PS C:\rhipheusADO\Build> cd ..\Build-master\

PS C:\rhipheusADO\Build-master> git merge 7d32e6e #Or any other intermediary commits
Updating 91d418c..7d32e6e
Fast-forward
 Pipeline/CR-MultiPool/azure-pipelines-auc.yml | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

PS C:\rhipheusADO\Build-master> git ls-remote
From https://myorg.visualstudio.com/HelloWorldApp/_git/Build
53060bac18f9d4e7c619e5170c436e6049b63f25        HEAD
7d32e6ec76d5a5271caebc2555d5a3a84b703954        refs/heads/vyas-cr-core 

PS C:\rhipheusADO\Build-master> git merge 7d32e6ec76d5a5271caebc2555d5a3a84b703954
Already up-to-date

PS C:\rhipheusADO\Build>  git push
Total 0 (delta 0), reused 0 (delta 0)
To https://myorg.visualstudio.com/HelloWorldApp/_git/Build
   91d418c..7d32e6e  master -> master

If you need to just merge the latest commit:

git merge origin/vyas-cr-core 
git push

And is same as what I've always done:

git checkout master # This is needed if you're not using worktrees
git pull origin vyas-cr-core
git push
Vyas Bharghava
  • 6,372
  • 9
  • 39
  • 59
0

git rebase the-branch-to-be-merged

I solved the problem using this git command, but rebase is only suited for some cases.

cat-walk
  • 51
  • 6
0

Weird as it may sound, restarting my computer help solve the issue. I was using terminal from IntelliJ IDEA and autocomplete for the branch names, so I'm pretty confident it was not a typo error on the branch name.