0

I have this:

git clone --depth=1 <repo> app
cd app
git fetch origin
git checkout a119b1076dd45a88a3609c4f7893ef3d82f9a4ee

but it says:

fatal: reference is not a tree: a119b1076dd45a88a3609c4f7893ef3d82f9a4ee

if I use the name of the branch:

 git checkout me/work

I get:

error: pathspec 'me/work' did not match any file(s) known to git.

is it because I did a shallow clone? don't make much sense to me. The commit is on the remote, at the very least the branch/commit with that name is on the remote.

Update:

So I added an --all to git fetch --all and then ran git branch -vv --all and I see:

* master                4761f83 [origin/master] timeline event update date should not be the review date.  Every time it is inserted or updated the update date should be the current utc date
  remotes/origin/HEAD   -> origin/master
  remotes/origin/master 4761f83 timeline event update date should not be the review date.  Every time it is inserted or updated the update date should be the current utc date

so the branch is not in that list, if that helps someone help me.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

5 Answers5

4

As the docs for --depth says,

Implies --single-branch unless --no-single-branch is given

so if you want

to fetch the histories near the tips of all branches

give --no-single-branch on your clone, or for one-off correction do the fetch yourself,

git fetch --depth=1 origin +refs/heads/*:refs/remotes/origin/*

or to retroactively shut off the single-branch setup

git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*

and then git fetch.

jthill
  • 55,082
  • 5
  • 77
  • 137
4

A shallow clone is also, by default, a single-branch clone:

--depth <depth>
Create a shallow clone with a history truncated to the specified number of commits. Implies --single-branch unless ...

A single-branch clone is just what it says: a clone that copies only one branch from the upstream. (The underlying mechanism is via the default fetch refspecs, so you can temporarily override this for one fetch by supplying refspecs.)

If you want a shallow clone to copy more than one branch, you must convert it to a non-single-branch clone, or start it out as a non-single-branch clone. To start out as one, keep reading the rest of the quoted sentence. To change an existing single-branch clone to a two, three, or all-branch clone, see How do I "undo" a --single-branch clone?

torek
  • 448,244
  • 59
  • 642
  • 775
  • yeah that makes sense, my answer with FETCH_HEAD seems to work tho – Alexander Mills May 16 '19 at 23:21
  • 1
    Yes: you're overriding the default fetch refspec. The refspec you gave said to grab their `me/work`, remembering the commits temporarily via your `FETCH_HEAD` file, without creating any other name. The `git checkout -b` then creates a new local branch name using the data remembered (until the next `fetch` anyway) in `.git/FETCH_HEAD`. – torek May 16 '19 at 23:23
2

When you're doing a shallow clone you may want to specify a branch you are going to fetch:

git clone --depth=1 --branch=me/work <repo> app
1

I think this way works if you do a shallow clone

git fetch origin me/work
git checkout -b temp FETCH_HEAD
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
1

An alternative would be to fetch and create explicitly the remote tracking branch (instead of relying on FETCH_HEAD)

And use the git switch guess mode to create the local branch.

git fetch me/work:me/work
git switch me/work
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250