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

我怎么做呢?


当前回答

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"

其他回答

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 checkout  commithash

在这个序列中

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

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

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

git reset --hard -#commit hash-

使用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.

这样你就不会丢失任何信息,因此当它变得稳定时,你可以转移到一个更新的版本。

你可以“重置”你的存储库到任何你想要的提交(例如1个月前)。

使用git-reset:

git clone [remote_address_here] my_repo
cd my_repo
git reset --hard [ENTER HERE THE COMMIT HASH YOU WANT]