I cloned a remote git repository about a month ago. The remote repository has undergone many changes and has now become unstable. Now I need another copy of the repository, version identical to the one I cloned a month ago.
How do I do this?
I cloned a remote git repository about a month ago. The remote repository has undergone many changes and has now become unstable. Now I need another copy of the repository, version identical to the one I cloned a month ago.
How do I do this?
If that version you need to obtain is either a branch or a tag then:
git clone -b branch_or_tag_name repo_address_or_path
uploadpack.allowReachableSHA1InWant
Since Git 2.5.0 this configuration variable can be enabled on the server, here the GitHub feature request and the GitHub commit enabling this feature.
Bitbucket Server enabled it since version 5.5+.
Usage:
# 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"
You can solve it like this:
git reset --hard sha
where sha e.g.: 85a108ec5d8443626c690a84bc7901195d19c446
You can get the desired sha with the command:
git log