Git

git 從遠端儲存庫中獲取特定版本

  • December 11, 2019

我們有一個遠端 git 儲存庫,我們通常git push在我們的開發伺服器上使用它然後git pull在我們的實時伺服器上部署它以獲得最新的推送版本的儲存庫。

但是,如果我們已經送出並推送了一些修訂(git pull在實時伺服器上沒有 a),我們怎麼能做一個git pull引用我們想要的舊送出的 a 呢?

即類似的東西git pull -r 3ef0dedda699f56dc1062b5dcc2c59f7ad93ede4

拉取儲存庫後,您應該能夠:

git checkout 3ef0d...

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"

引用自:https://serverfault.com/questions/117255