大约一个月前,我克隆了一个远程git存储库。远程存储库经历了许多更改,现在变得不稳定了。现在我需要存储库的另一个副本,与我一个月前克隆的版本相同。

我怎么做呢?


当前回答

你可以简单地使用

git checkout  commithash

在这个序列中

git clone `URLTORepository`
cd `into your cloned folder`
git checkout commithash

提交哈希看起来像这样“45ef55ac20ce2389c9180658fdba35f4a663d204”

其他回答

你需要的源树在git存储库中仍然可用,但是,你将需要你感兴趣的提交的SHA1。我假设你可以从当前的克隆你有SHA1 ?

如果您可以获得这个SHA1,那么您可以在那里创建一个分支/重置以拥有相同的存储库。

命令按照睿的回答

也许git重置可以解决你的问题。

git reset --hard -#commit hash-

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"

与集中式版本控制系统不同,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 reset --hard sha

例如:85a108ec5d8443626c690a84bc7901195d19c446

您可以通过以下命令获取所需的sha:

git log