大约一个月前,我克隆了一个远程git存储库。远程存储库经历了许多更改,现在变得不稳定了。现在我需要存储库的另一个副本,与我一个月前克隆的版本相同。
我怎么做呢?
大约一个月前,我克隆了一个远程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 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存储库中仍然可用,但是,你将需要你感兴趣的提交的SHA1。我假设你可以从当前的克隆你有SHA1 ?
如果您可以获得这个SHA1,那么您可以在那里创建一个分支/重置以拥有相同的存储库。
命令按照睿的回答
与集中式版本控制系统不同,Git克隆整个存储库,因此您不仅可以获得当前的远程文件,还可以获得整个历史。您的本地存储库将包括所有这些。
There might have been tags to mark a particular version at the time. If not, you can create them yourself locally. A good way to do this is to use git log or perhaps more visually with tools like gitk (perhaps gitk --all to see all the branches and tags). If you can spot the commits hashes that were used at the time, you can tag them using git tag <hash> and then check those out in new working copies (for example git checkout -b new_branch_name tag_name or directly with the hash instead of the tag name).
你可以简单地使用
git checkout commithash
在这个序列中
git clone `URLTORepository`
cd `into your cloned folder`
git checkout commithash
提交哈希看起来像这样“45ef55ac20ce2389c9180658fdba35f4a663d204”
你可以这样求解:
git reset --hard sha
例如:85a108ec5d8443626c690a84bc7901195d19c446
您可以通过以下命令获取所需的sha:
git log
uploadpack.allowReachableSHA1InWant
因为Git 2.5.0可以在服务器上启用这个配置变量,这里的GitHub特性请求和GitHub提交启用了这个特性。
Bitbucket Server从5.5+版本开始启用它。
用法:
# 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"