views:

42

answers:

3

How can I clone git repository with specific revision/changeset? Something like I usually do in Mercurial: hg clone -r 3 /path/to/repository

Thanks!

+2  A: 

Cloning a git repository, aptly, clones the entire repository: there isn't a way to select only one revision to clone. However, once you perform git clone, you can checkout a specific revision by doing checkout <rev>.

Mark Trapp
I don't want to clone only one revision. I just want to specify the limit of cloning. Other words, I want to clone everything up to the specified revision.
John
You can't do that. `git clone` grabs the whole repository. Once you have it, you can then checkout a specific revision.
Mark Trapp
In other words, John, that's not how Git works.
Amber
Shame. But thank you guys!
John
One thing to note; Git is generally pretty efficient about storing history, so it's not as if you'd save massive amounts of space by only cloning half the revisions.
Amber
+3  A: 

If you don't want to fetch the full repository then you probably shouldn't be using clone. You can always just use fetch to choose the branch that you want to fetch. I'm not an hg expert so I don't know the details of -r but in git you can do something like this.

# make a new blank repository in the current directory
git init

# add a remote
git remote add origin url://to/source/repository

# fetch a commit (or branch or tag) of interest
# Note: the full history of this commit will be retrieved
git fetch origin <sha1-of-commit-of-interest>

# reset this repositories master branch to the commit of interest
git reset --hard FETCH_HEAD
Charles Bailey
A: 

If you mean you want to fetch everything from the beginning up to a particular point, Charles Bailey's answer is perfect. If you want to do the reverse and retrieve a subset of the history going back from the current date, you can use git clone --depth [N] where N is the number of revs of history you want. However:

--depth

Create a shallow clone with a history truncated to the specified number of revisions. A shallow repository has a number of limitations (you cannot clone or fetch from it, nor push from nor into it), but is adequate if you are only interested in the recent history of a large project with a long history, and would want to send in fixes as patches.

Walter Mundt