簡體   English   中英

如何將淺克隆推送到新的存儲庫?

[英]How to push a shallow clone to a new repo?

我希望擺脫很多我的 repo 的舊歷史,所以我做了一個淺層克隆,只獲取最后 50 次提交:

git clone --depth=50 https://my.repo

這工作正常,但是當我創建一個新的 Gitlab 存儲庫並嘗試推送它時,我收到一個錯誤:

git remote remove origin
git remote add origin https://my.repo
git push -u origin --all
[...]
 ! [remote rejected] master -> master (shallow update not allowed)

但我只希望這 50 次提交出現在我的新倉庫的歷史記錄中。 我如何告訴 git 它應該將這 50 次提交視為新存儲庫中的唯一提交?

這就是我最終做的 - 它完美地工作。 請注意,我正在從舊主機 (Bitbucket) 遷移到新主機 (Gitlab)。 我的評論高於命令:

# First, shallow-clone the old repo to the depth we want to keep
git clone --depth=50 https://...@bitbucket.org/....git

# Go into the directory of the clone
cd clonedrepo

# Once in the clone's repo directory, remove the old origin
git remote remove origin

# Store the hash of the oldest commit (ie. in this case, the 50th) in a var
START_COMMIT=$(git rev-list master|tail -n 1)

# Checkout the oldest commit; detached HEAD
git checkout $START_COMMIT

# Create a new orphaned branch, which will be temporary
git checkout --orphan temp_branch

# Commit the initial commit for our new truncated history; it will be the state of the tree at the time of the oldest commit (the 50th)
git commit -m "Initial commit"

# Now that we have that initial commit, we're ready to replay all the other commits on top of it, in order, so rebase master onto it, except for the oldest commit whose parents don't exist in the shallow clone... it has been replaced by our 'initial commit'
git rebase --onto temp_branch $START_COMMIT master

# We're now ready to push this to the new remote repo... add the remote...
git remote add origin https://gitlab.com/....git

# ... and push.  We don't need to push the temp branch, only master, the beginning of whose commit chain will be our 'initial commit'
git push -u origin master

在那之后,我對新的 repo 做了一個新的克隆,我只得到了包含 50 個最近提交的 master 分支——正是我想要的! :-) 提交歷史已經從 250MB 增加到 50MB。 嗚。

您不能將淺克隆推入新的遙控器。 你必須先unshallow你的克隆。 使用--unshallow參數從舊遙控器--unshallow數據:

git fetch --unshallow old

你應該能夠推送到你的新遙控器。 請注意,您需要先添加回舊遙控器才能從中獲取。

但...

但這不是你想要的。 要從完整克隆中刪除歷史記錄,您需要使用git rebase來有效刪除舊歷史記錄。 還有其他方法,但由於您只需要最后 50 次提交,這將是最簡單的解決方案。 假設master分支:

git rebase --onto master~y master~x master

其中x是要保留的第一個提交的編號, y是您要刪除的第一個提交的編號。 此時,您可以僅將要保留的歷史記錄推送到新遙控器。 請注意,您需要自己枚舉git log的提交編號,因為它需要一個索引(從 1 開始)而不是提交哈希。


請注意,因為在 Git 中重寫歷史記錄可能是一件危險的事情,並且您還需要考慮其他影響。 還要確保不要將更改推送到舊遙控器,除非您還想刪除那里的舊歷史記錄。

來源: https : //www.clock.co.uk/insight/deleting-a-git-commit

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM