Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>...]' 
like image 403
Roman Avatar asked Mar 15 '18 10:03

Roman


People also ask

Is not a commit and a branch Cannot be created from it git?

Git error - fatal: 'origin/[YOUR_BRANCH]' is not a commit and a branch '[YOUR_BRANCH]' cannot be created from it. You need to fetch all the origin branches from remote in order to checkout, Simply run the following command; git fetch --all.

Can we create a branch from a commit?

In order to create a Git branch from a commit, use the “git checkout” command with the “-b” option and specify the branch name as well as the commit to create your branch from. Alternatively, you can use the “git branch” command with the branch name and the commit SHA for the new branch.

Is branch a commit?

Instead of copying files from directory to directory, Git stores a branch as a reference to a commit. In this sense, a branch represents the tip of a series of commits—it's not a container for commits. The history for a branch is extrapolated through the commit relationships.

Can two branches have the same commit?

Yes, two branches can point to the same commits.

Why can't I create a branch from'local-branch-name'in Git?

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: If you run git checkout -b local-branch-name origin/remote-branch-name without fetch ing first, you can run into that error.

Why does Git say this is not a commit?

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.

Why can't Git fetch--all resolve the branch I provide?

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.

How do I create a new branch in GitHub?

Do command "Git: Checkout To...". Select "Create Branch From...". Type a new branch name, such as "main2". Select an existing branch such "main". Sign up for free to join this conversation on GitHub .


2 Answers

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.

like image 174
J.D. Mallen Avatar answered Sep 24 '22 00:09

J.D. Mallen


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'.

like image 25
Jeroen Vermeulen - MageHost Avatar answered Sep 22 '22 00:09

Jeroen Vermeulen - MageHost