我如何用特定的版本克隆git存储库,就像我通常在Mercurial中做的那样:
hg clone -r 3 /path/to/repository
我如何用特定的版本克隆git存储库,就像我通常在Mercurial中做的那样:
hg clone -r 3 /path/to/repository
当前回答
在一个特定的分支或标签上只克隆一个特定的提交:
git clone --depth=1 --branch NAME https://github.com/your/repo.git
不幸的是,NAME只能是分支名称或标记名称(不能提交SHA)。
省略——depth标志来下载整个历史记录,然后签出分支或标记:
git clone --branch NAME https://github.com/your/repo.git
这适用于最新版本的git(我使用2.18.0版本)。
其他回答
我的版本是公认的答案和最多赞的答案的结合。但它有点不同,因为每个人都使用SHA1,但没有人告诉你如何获得它
$ git init
$ git remote add <remote_url>
$ git fetch --all
现在你可以看到所有的分支&提交
$ git branch -a
$ git log remotes/origin/master <-- or any other branch
最后,您知道了所需提交的SHA1
git reset --hard <sha1>
DR -只需在源存储库中针对您想要克隆到的提交创建一个标记,并在fetch命令中使用该标记。您可以稍后从原始回购中删除标记以进行清理。
好吧,现在已经是2014年了,看起来查尔斯·贝利2010年的答案已经过时了,而且大多数(所有?)其他答案都涉及克隆,许多人都希望避免克隆。
下面的解决方案实现了OP和其他许多人正在寻找的东西,这是一种创建存储库副本(包括历史记录)的方法,但仅限于特定的提交。
以下是我在git 2.1.2版本中使用的命令,用于克隆一个本地回购(即。在另一个目录中的存储库)到某一点:
# in the source repository, create a tag against the commit you want to check out
git tag -m "Temporary tag" tmptag <sha1>
# create a new directory and change into that directory
cd somewhere_else;mkdir newdir;cd newdir
# ...and create a new repository
git init
# add the source repository as a remote (this can be a URL or a directory)
git remote add origin /path/to/original/repo
# fetch the tag, which will include the entire repo and history up to that point
git fetch origin refs/tags/tmptag
# reset the head of the repository
git reset --hard FETCH_HEAD
# you can now change back to the original repository and remove the temporary tag
cd original_repo
git tag -d tmptag
希望这个解决方案能继续工作几年!: -)
# clone special tag/branch without history
git clone --branch=<tag/branch> --depth=1 <repository>
# clone special revision with minimal histories
git clone --branch <branch> <repository> --shallow-since=yyyy-MM-ddTHH:mm:ss # get the commit time
cd <dir>
git reset --hard <revision>
如果不设置uploadpack,就不能获得没有历史记录的修订。在服务器端allowReachableSHA1InWant=true,而您可以为它创建一个标记并克隆特殊标记。
克隆单个分支的完整工作流程,选择提交,然后检查特定的提交…此方法需要git版本2.28.0或更高的版本才能使用选项——no-write-fetch-head,使用版本2.35.3进行测试。(如果你已经知道你想要提交的完整sha1哈希值,请跳过最后一个代码块中的第二个方法)
#Create empty git repo
mkdir repo && cd repo && git init
#add remote, configure it to track <branch>
git remote add --no-tags -t <branch> -m <branch> origin <url>
#fetch objects from remote repo
git fetch --no-write-fetch-head
#examine commits and logs to decide which one we will use
git log --oneline origin
#Once you have found the commit of interest copy the abbreviated hash or save as variable
commit=<sha1>
#rename our default branch to match remote branch
git branch -m <branch>
#set branch head to desired commit
git branch <branch> $commit
#set remote branch as upstream for <branch>
git branch -u origin <branch>
#All done time to checkout
git checkout
可以选择截断本地分支的历史记录:
git fetch --no-write-fetch-head --depth <n> ./ <branch>
要截断远程分支历史记录,可以执行以下命令,但请记住,如果你将历史记录截断为一个比你签出的提交更新的提交,git状态会告诉你,你已经与远程分支分离了<n>次提交
git fetch --no-write-fetch-head --depth <n>
如果你不需要远程跟踪,并且已经知道完整的提交散列:
mkdir repo && cd repo && git init
git remote --no-tags add origin <url>
git fetch --depth 1 --no-write-fetch-head origin <sha1>
#Set default local branch (master in this case) head to <sha1>
git branch master <sha1>
git checkout
在我看来,这个方法更好的地方在于它真正只获取一个提交。我们还避免创建FETCH_HEAD或ORIG_HEAD,使.git目录保持干净。这也使得reflog干净(只有一个条目),而不是由于git重置而有两个条目——硬提交 不需要远程跟踪和使用fetch—depth 1,它创建尽可能小的克隆(浅克隆)。
我可以使用git clone——config选项来完成这一点,这是我从下面的回答中学到的: https://stackoverflow.com/a/43759576/1330650
我的场景涉及Azure DevOps管道中的稀疏签出,其中我需要使用提交散列(而不是分支名称)克隆一个回购。clone命令不接受提交哈希作为参数。解决方法是设置一个包含refspec的配置变量(-c),因为该refspec可以使用提交哈希而不是分支名称:
git clone -c remote.origin.fetch=+<commit hash>:refs/remotes/origin/<commit hash> <repo_url> --no-checkout --progress --depth 1
git sparse-checkout init --cone
git sparse-checkout set <file list>
git checkout <commit hash>