![](/img/trans.png)
[英]How to git clone a subdirectory of a particular branch of a remote repository?
[英]Git clone particular version of remote repository
我大约一个月前克隆了一个远程git存储库。 远程存储库经历了许多变化,现在变得不稳定。 现在我需要另一个存储库副本,版本与我一个月前克隆的版本相同。
我该怎么做呢?
您可以将您的存储库“重置”为您想要的任何提交(例如1个月前)。
使用git-reset :
git clone [remote_address_here] my_repo
cd my_repo
git reset --hard [ENTER HERE THE COMMIT HASH YOU WANT]
你可以简单地使用
git checkout commithash
在这个序列中
git clone `URLTORepository`
cd `into your cloned folder`
git checkout commithash
commit hash看起来像这样“45ef55ac20ce2389c9180658fdba35f4a663d204”
使用git log
查找要回滚的修订版本,并记下提交哈希值。 之后,您有两个选择:
如果您计划在该修订后提交任何内容,我建议您签出一个新分支: git checkout -b <new_branch_name> <hash>
如果您不打算在该修订版本之后提交任何内容,则只需在没有分支的情况下签出: git checkout <hash>
- 注意:这将使您的存储库处于“分离的HEAD”状态,这意味着它当前未附加到任何分支 - 那么你将有一些额外的工作来将新提交合并到一个实际的分支 。
例:
$ git log
commit 89915b4cc0810a9c9e67b3706a2850c58120cf75
Author: Jardel Weyrich <suppressed>
Date: Wed Aug 18 20:15:01 2010 -0300
Added a custom extension.
commit 4553c1466c437bdd0b4e7bb35ed238cb5b39d7e7
Author: Jardel Weyrich <suppressed>
Date: Wed Aug 18 20:13:48 2010 -0300
Missing constness.
$ git checkout 4553c1466c437bdd0b4e7bb35ed238cb5b39d7e7
Note: moving to '4553c1466c437bdd0b4e7bb35ed238cb5b39d7e7'
which isn't a local branch
If you want to create a new branch from this checkout, you may do so
(now or later) by using -b with the checkout command again. Example:
git checkout -b <new_branch_name>
HEAD is now at 4553c14... Missing constness.
这样你就不会丢失任何信息,因此当它变得稳定时你可以移动到更新的版本。
如果您需要获取的版本是分支或标记,那么:
git clone -b branch_or_tag_name repo_address_or_path
与集中式版本控制系统不同,Git克隆整个存储库,因此您不仅可以获取当前的远程文件,还可以获取整个历史记录。 您本地存储库将包含所有这些。
当时可能有标记标记特定版本。 如果没有,您可以在本地自己创建它们。 一个很好的方法是使用git log
或者更可视地使用gitk
工具(也许是gitk --all
来查看所有分支和标签)。 如果您可以发现当时使用的提交哈希值,您可以使用git tag <hash>
标记它们,然后在新的工作副本中检查它们(例如git checkout -b new_branch_name tag_name
或直接使用哈希而不是哈希值标签名称)。
你可以这样解决:
git reset --hard sha
其中sha
例如: 85a108ec5d8443626c690a84bc7901195d19c446
您可以使用以下命令获得所需的sha:
git log
您需要的源树仍然可以在git存储库中使用,但是,您需要您感兴趣的提交的SHA1。我会假设您可以从当前克隆中获取SHA1吗?
如果你可以获得SHA1,你可以在那里创建一个分支/重置以拥有相同的存储库。
根据Rui的回答命令
可能git reset
解决了你的问题。
git reset --hard -#commit hash-
uploadpack.allowReachableSHA1InWant
从Git 2.5.0开始,可以在服务器上启用此配置变量,这里是GitHub功能请求和启用此功能的GitHub提交 。
用法:
# Make remote with 4 commits, and local with just one.
mkdir server
cd server
git init
touch 1
git add 1
git commit -m 1
git clone ./ ../local
for i in {2..4}; do
touch "$i"
git add "$i"
git commit -m "$i"
done
# Before last commit.
SHA3="$(git log --format='%H' --skip=1 -n1)"
# Last commit.
SHA4="$(git log --format='%H' -n1)"
# Failing control without feature.
cd ../local
# Does not give an error, but does not fetch either.
git fetch origin "$SHA3"
# Error.
git checkout "$SHA3"
# Enable the feature.
cd ../server
git config uploadpack.allowReachableSHA1InWant true
# Now it works.
cd ../local
git fetch origin "$SHA3"
git checkout "$SHA3"
# Error.
git checkout "$SHA4"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.