Why is it not a commit and a branch cannot be created from it?
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'.
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 fetch
ing 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.