Why is it not a commit and a branch cannot be created from it?
Asked Answered
R

19

105

I need to work with an intricate configuration of repositories. I have 5 of them:

  1. A remote central repository on machine 1.
  2. My local repository on my notebook (machine 2).
  3. A bare repository on machine 3.
  4. A repository on machine 3.
  5. A repository on machine 4 where we do code review.

So, my understanding that it works this way:

  1. On my laptop (machine 2) I clone / pull from the central repository located on machine 1.
  2. I push the local repo to the machine 3 (using the bare repository as a "intermediate").

Now I did some changes on the machine 3 and I want to push these changes to machine 4. Here are the instructions that I need to follow:

  1. On machine 3 do all work in your test-branch, commit.
  2. Push to your bare repo on machine 3: git push origin test-branch
  3. On your laptop: fetch new commits from the machine-3 repo: git fetch machine3
  4. Check out your branch from machine 3: git checkout -b test-branch machine-3/test-branch
  5. Fetch commits from machine-4: git fetch origin
  6. git rebase origin/master
  7. git push origin HEAD:refs/for/master

I have problems with step 4. I get the following error:

fatal: 'machine3/test-branch' is not a commit and a branch 'test-branch' cannot be created from it

ADDED

When I execute

git rev-parse machine3/test-branch

On my laptop (machine 2) I get:

machine3/test-branch

fatal: ambiguous argument 'machine3/test-branch': unknown revision or path not in the working tree.

Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
Reposeful answered 15/3, 2018 at 10:33 Comment(9)
The repo of Machine 3 is bare, which means it does not have any working tree. git checkout -b test-branch machine-3/test-branch needs a working tree, so it fails. Besides, you need to check if machine3/test-branch exists. mijingo.com/blog/what-is-the-working-tree-in-gitCornie
On machine 3 I have 2 repos (one bare and another one is non-bare). I have the "test-branch" in the non-bare repo on machine 3.Reposeful
Sorry I missed it. What does git rev-parse machine3/test-branch echo?Cornie
In what git should I execute it?Reposeful
In the repo where you run Step 4.Cornie
I have updated my question with the answer to you question.Reposeful
Let us continue this discussion in chat.Cornie
what about git pull?Aitch
@CharlieParker I have addressed your comment in my answer belowPastorship
R
0

The question is complex / convolute, the answer is simple. There was a mismatch between the alias and machine3. The alias for the remote that has been used, was not for machine3. The machine3 had another alias.

Reposeful answered 15/3, 2018 at 13:46 Comment(1)
For me the same error occured when I made a spelling mistake in my remote branch name.Provide
W
205

For those who found this searching for an answer to fatal: 'origin/remote-branch-name' is not a commit and a branch 'local-branch-name' cannot be created from it, you may also want to try this first:

git fetch --all

If you run git checkout -b local-branch-name origin/remote-branch-name without fetching first, you can run into that error.

The reason it says "is not a commit" rather than something clearer like "branch doesn't exist" is because git takes the argument where you specified origin/remote-branch-name and tries to resolve it to a commit hash. You can use tag names and commit hashes as an argument here, too. If they fail, it generates the same error. If git can't resolve the branch you provide to a specific commit, it's usually because it doesn't have the freshest list of remote branches. git fetch --all fixes that scenario.

The --all flag is included in case you have multiple remotes (e.g. origin, buildserver, joespc, etc.), because git fetch by itself defaults to your first remote-- usually origin. You can also run fetch against a specific remote; e.g., git fetch buildserver will only fetch all the branches from the buildserver remote.

To list all your remotes, run the command git remote -v. You can omit the --all flag from git fetch if you only see one remote name (e.g. origin) in the results.

Whitehouse answered 28/3, 2019 at 20:24 Comment(5)
for me git fetch origin git fetch origin master did not help, I had to git fetch origin refs/heads/master:refs/remotes/origin/masterNullity
@SillyFreak that's the only thing that worked for me.Groome
@SillyFreak - I was failing to create an tracking branch for an upstream but it started working after : git fetch upstream refs/heads/master:refs/remotes/upstream/My-Remote-Branch-NameFlabellate
what about git pull?Aitch
@CharlieParker git pull is just a shortcut command for git fetch && git merge origin/master or whatever your current remote name / remote branch is. So, yes, it would work, too.Whitehouse
N
74

We had this error:

fatal: 'origin/newbranch' is not a commit and a branch 'newbranch' cannot be created from it

because we did a minimalistic clone using:

git  clone  --depth 1  --branch 'oldbranch'  --single-branch  '[email protected]:user/repo.git'

For us the minimalistic fix was:

git  remote  set-branches  --add  'origin'  'newbranch'
git  fetch  'origin'
git  checkout  --track  'origin/newbranch'

Assuming the remote is called 'origin' and the remote branch is called 'newbranch'.

Nucleolus answered 8/6, 2021 at 12:29 Comment(5)
Thank you! Been running in circles for an hour till I found your answer :)Drying
I cloned single branch and failed when tracking new branch, this help me so much.Mantel
At least someone understands that git repo can be GINORMOUS and using 'git fetch --all' is not an optionPulchritudinous
In my case, this solution only worked for me among answers of this question.Mastoidectomy
what about git pull?Aitch
H
19

I managed to fix this with this settings, just update the config with this command

git config -e --global

and add this config.

[remote "origin"]
    url = https://git.example.com/example.git (you can omit this URL)
    fetch = +refs/heads/*:refs/remotes/origin/*

and then you can git fetch --all

Holly answered 13/10, 2020 at 16:11 Comment(2)
It worked for me too. I had remove my only remote to get rid of all tracking branches at once, and that line was gone.Neilson
thanks, it solves my issue for fetching remote branches.Process
T
6

I found this question troubleshooting simpler problem: I recieved same error trying to create a lobal branch from remote. I resolved it by creating branch from commit hash:

git ls-remote origin remote-branch
returned
<hash>  refs/heads/remote-branch

git checkout -b local-branch <hash>
Therewithal answered 26/10, 2021 at 20:45 Comment(0)
R
3

If you're checking out a branch from a tag (like git checkout -b XXXX v0.1.1) , you can try git fetch --tags first.

Rubio answered 13/12, 2019 at 20:49 Comment(0)
B
1

We were having this exact error in a Windows machine running gitbash on a folder synced with google drive.

It was caused by having the feature "Enable experimental built-in file system monitor" enabled.

After uninstalling and reinstalling gitbash with this feature disabled it was fixed.

enter image description here

Blane answered 21/10, 2021 at 17:4 Comment(0)
R
0

The question is complex / convolute, the answer is simple. There was a mismatch between the alias and machine3. The alias for the remote that has been used, was not for machine3. The machine3 had another alias.

Reposeful answered 15/3, 2018 at 13:46 Comment(1)
For me the same error occured when I made a spelling mistake in my remote branch name.Provide
G
0

For this issue:

fatal: 'machine3/test-branch' is not a commit and a branch 'test-branch' cannot be created from it

For me, I should have checked out to test-branch first, then it worked fine and created a new branch from test-branch.

Gratian answered 1/2, 2021 at 13:24 Comment(0)
R
0

I had the problem where I git checkout name-branch and it was created but when i check the branch with git branch nothing happens after hours of trying to figure out I tried to run the command git push GitHub-URL name-branch and it pushes the commit directly to the branch hope this helps

Radnorshire answered 19/7, 2022 at 8:22 Comment(1)
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.Dividivi
T
0

My issues was I had a space in my new branch name

Issue:

git checkout -b my-new-branch name

instead of

git checkout -b my-new-branch-name

Toponym answered 19/7, 2022 at 15:45 Comment(0)
G
0

The branch does not exist on the remote origin specified. Double check: You might be trying to pull oranges from a grapes tree.

Galvin answered 24/8, 2022 at 19:54 Comment(0)
S
0

Check your git config in .git folder, and validate this content

[core]
    repositoryformatversion = 0
    fileMode = false
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = https://github.com/ABC/project0001.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
    remote = origin
    merge = refs/heads/main
[pull]
    ff = no
[branch "develop"]
    remote = origin
    merge = refs/heads/develop

[remote "origin"] <-- this section

Sodalite answered 13/10, 2022 at 3:29 Comment(0)
G
0

For those who found this searching for an answer to fatal: 'origin/remote-branch-name' is not a commit and a branch 'local-branch-name' cannot be created from it, another possibility we just found is the remote branch has been merged already :embarrassed:

Gillan answered 17/11, 2022 at 13:44 Comment(0)
B
0

I just encountered this when I used the "copy" tool on GitHub next to the branch name of a coworker's PR. His branch was named like coworker/ticket-number and git checkout --track coworker/ticket-number was what came out of the paste.

When I went back to my usual workflow, using git branch -r | grep ticket-number I found that the actual branch name I needed to use was origin/coworker/ticket-number.

Bael answered 22/12, 2022 at 18:27 Comment(0)
N
0

I faced the same error and the fix was as simple as creating teh branch in two steps:

  1. git checkout the existing branch from where I want to create my new branch. e.g. git checkout dev This brings you to the dev branch.
  2. create the new branch from dev branch while sitting on the dev branch e.g. git checkout -b <new_feature_branch> dev
Newsstand answered 28/9, 2023 at 16:7 Comment(0)
B
0

I generally create the branches via UI on gitlab/github and then switch to it in local.

I was running this command

git fetch origin
git checkout --track origin/my-branch

And I got the same error fatal: 'origin/my-branch' is not a commit and a branch 'my-branch' cannot be created from it

It turns out, my-branch didn't exist on the remote repository. I had mistakenly created it on another repo via UI.

Bibber answered 12/12, 2023 at 9:5 Comment(0)
P
-1

The solution from J.D. Mallen involves git fetch --all

Starting with VSCode 1.53 (Jan. 2021), you will have (issue 110811 and PR 111090):

Changing autofetch to a string config which has "current", "all" and "off".

This allows users to fetch from all remotes instead of just their origin.
For developers working on a Fork, this should make extensions work more intuitively.

So if you set the VSCode setting git.autofetch to "all", you won't even have to type git fetch --all.


Charlie Parker adds in the comments:

What about git pull?

That supposes you are on a current branch with a remote tracking branch already set up.

The original question does not have such a branch in place, which means git pull would not know what to merge from fetched branches.

Pastorship answered 17/1, 2021 at 0:32 Comment(0)
M
-2

I used git workflow in visual studio code as shown in the below diagram to solve mine:

create a feature branch using git on vscode

Maurreen answered 13/12, 2020 at 13:23 Comment(1)
why is the answer voted down pls? I'd like to know, afterall I only mentioned the way I solve a similar problem.Maurreen
K
-2

General Solution:

git fetch origin <base_branch>

git checkout -b <new_brach> origin/<base_branch>

Your issue might be solved in this way:

git fetch machine-3 test-branch

git checkout -b local-test-branch machine-3/test-branch
Kerbela answered 9/5 at 0:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.