Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to checkout remote branch with git clone --depth 1?

Tags:

git

I downloaded last revision of a big repository, where I need to get a latest revision of one branch so that I can test it.

How do I do that?

When I did git clone --depth 1 url I got last revision of master and the branch doesn't seem to exist?

petanb@petrbena:~/Documents/mh$ git checkout flaggedrevs
error: pathspec 'flaggedrevs' did not match any file(s) known to git.
like image 623
Petr Avatar asked Jun 08 '14 15:06

Petr


People also ask

What does depth 1 do in git clone?

git clone --depth=1 <url> creates a shallow clone. These clones truncate the commit history to reduce the clone size. This creates some unexpected behavior issues, limiting which Git commands are possible.

Can you checkout a remote branch in git?

In order to checkout a remote branch you have to first fetch the contents of the branch. In modern versions of Git, you can then checkout the remote branch like a local branch.

Does git clone do a checkout?

The Git clone command will create a new local directory for the repository, copy all the contents of the specified repository, create the remote tracked branches, and checkout an initial branch locally.


2 Answers

git clone --depth 1 implicitly carries a --single-branch option, which defaults to the primary branch, which is origin/master by default.

If you want to clone a different branch, tell git which one you want to clone.

git clone --depth 1 --branch <branch> url

like image 63
masonk Avatar answered Oct 16 '22 20:10

masonk


You can directly fetch anything you want:

git fetch --depth 1 origin flaggedrevs:flaggedrevs  #  `origin` can be a URL too

That will create a normal (albeit shallow) branch from what you fetched. To get more conventional results, git fetch --depth 1 origin flaggedrevs:refs/remotes/origin/flaggedrevs. "copy origin's flaggedrevs to my refs/remotes/origin/flaggedrevs".

Haul up .git/config and look at it, or say git config --get-regexp fetch. If you don't explicitly tell it what to fetch, that's what it fetches.

like image 45
jthill Avatar answered Oct 16 '22 19:10

jthill