Joh*_*ohn 363 git revision git-clone
如何克隆具有特定修订版的git存储库,就像我在Mercurial中通常所做的那样:
hg clone -r 3 /path/to/repository
Run Code Online (Sandbox Code Playgroud)
Vai*_*pai 805
$ git clone $URL
$ cd $PROJECT_NAME
$ git reset --hard $SHA1
Run Code Online (Sandbox Code Playgroud)
再次回到最近的提交
$ git pull
Run Code Online (Sandbox Code Playgroud)
CB *_*ley 178
更新2从Git 2.5.0开始,可以在服务器端使用配置变量启用下面描述的功能uploadpack.allowReachableSHA1InWant,此处GitHub功能请求和启用此功能的GitHub提交.请注意,默认情况下,某些Git服务器会激活此选项,例如Bitbucket Server自5.5+版本启用它.有关如何激活配置选项的详细信息,请参阅Stackexchange上的此答案.
更新1对于Git版本,1.7 < v < 2.5使用git clone和git reset,如Vaibhav Bajpai的回答中所述
如果您不想获取完整的存储库,那么您可能不应该使用它clone.您始终可以使用fetch来选择要获取的分支.我不是一个hg专家,所以我不知道细节,-r但在git你可以做这样的事情.
# 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 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 interest
git reset --hard FETCH_HEAD
Run Code Online (Sandbox Code Playgroud)
小智 51
克隆一个git存储库,恰当地克隆了整个存储库:没有办法只选择一个要克隆的修订版.但是,一旦执行git clone,您可以通过执行检查特定修订checkout <rev>.
Wal*_*ndt 32
如果你的意思是你想要从一开始到特定的地方取得一切,那么Charles Bailey的答案是完美的.如果您想要反向并检索从当前日期返回的历史记录的子集,您可以使用git clone --depth [N] 其中N是您想要的历史记录转数.然而:
- 深度
创建一个浅层克隆,其历史记录被截断为指定的修订数.浅存储库有许多限制(您不能克隆或获取它,也不能从中推送或插入它),但如果您只对历史悠久的大型项目的近期历史感兴趣并且希望将修补程序作为补丁发送.
小智 24
总结一下(git v.1.7.2.1):
git clone你想要回购的常规地点(得到所有东西到目前为止 - 我知道,不是想要什么,我们到达那里) git checkout <sha1 rev> 你想要的转速git reset --hardgit checkout -b masterJam*_*esG 19
TL; DR - 只需在源存储库中根据要克隆的提交创建一个标记,并在fetch命令中使用该标记.您可以稍后从原始仓库中删除标签以进行清理.
好吧,它的2014年和看起来像Charles Bailey从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
Run Code Online (Sandbox Code Playgroud)
希望这个解决方案能够持续工作几年!:-)
Pet*_*vac 18
要在特定分支或标签上仅克隆单个特定提交,请使用:
git clone --depth=1 --branch NAME https://github.com/your/repo.git
Run Code Online (Sandbox Code Playgroud)
不幸, NAME只能是分支名称或标记名称(不能提交SHA)。
省略该--depth标志以下载整个历史记录,然后检出该分支或标记:
git clone --branch NAME https://github.com/your/repo.git
Run Code Online (Sandbox Code Playgroud)
这适用于git的最新版本(我使用version做到了2.18.0)。
M.O*_*man 14
你可以简单地使用 git checkout <commit hash>
在这个序列中
bash
git clone [URLTORepository]
git checkout [commithash]
commit hash看起来像这样"45ef55ac20ce2389c9180658fdba35f4a663d204"
Joh*_*nes 14
无需下载整个历史记录,也无需调用git init:
git clone --depth=1 URL
git fetch --depth=1 origin SHA1
git checkout SHA1
git branch -D @{-1} # if you want to tidy up the fetched branch
Run Code Online (Sandbox Code Playgroud)
对于 CB Baileys 的回答,这有一个缺点,即您仍然会下载 1 个不必要的修订版。但从技术上讲,它是git clone(OP想要的),并且它不会强迫您下载某个分支的整个历史记录。
我能够使用 git clone --config 选项来完成此操作,这是我从这个答案中学到的: https: //stackoverflow.com/a/43759576/1330650
我的场景涉及 Azure DevOps 管道中的稀疏签出,其中我需要使用提交哈希而不是分支名称来克隆存储库。克隆命令不接受提交哈希作为参数。解决方法是设置一个包含 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
433074 次 |
| 最近记录: |