How to clone git repository with specific revision/changeset?
UPDATE 2 Since Git 2.5.0 the feature described below can be enabled on server side with configuration variable uploadpack.allowReachableSHA1InWant
, here the GitHub feature request and the GitHub commit enabling this feature. Note that some Git servers activate this option by default, e.g. Bitbucket Server enabled it since version 5.5+. See this answer on Stackexchange for a exmple of how to activate the configuration option.
UPDATE 1 For Git versions 1.7 < v < 2.5
use git clone and git reset, as described in Vaibhav Bajpai's answer
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 directorygit init# add a remotegit remote add origin url://to/source/repository# fetch a commit (or branch or tag) of interest# Note: the full history up to this commit will be retrieved unless # you limit it with '--depth=...' or '--shallow-since=...'git fetch origin <sha1-of-commit-of-interest># reset this repository's master branch to the commit of interestgit reset --hard FETCH_HEAD
To clone only one single specific commit on a particular branch or tag use:
git clone --depth=1 --branch NAME https://github.com/your/repo.git
Unfortunately, NAME
can only be branch name or tag name (not commit SHA).
Omit the --depth
flag to download the whole history and then checkout that branch or tag:
git clone --branch NAME https://github.com/your/repo.git
This works with recent version of git (I did it with version 2.18.0
).