我希望摆脱很多我 repo 协议(protocol)的旧历史,所以我做了一个浅表克隆以仅获取最后的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次提交成为我新 repo 协议(protocol)的历史记录。我怎么能告诉git它应该只将这50次提交作为新仓库中的唯一提交?
最佳答案
这就是我最终做的 - 它完美地工作。请注意,我正在从旧主机 (Bitbucket) 迁移到新主机 (Gitlab)。我的评论高于命令:
# First, shallow-clone the old repo to the depth we want to keep
git clone --depth=50 https://[email protected]/....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。呜。
关于git - 如何将浅表克隆推到新的仓库?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50992188/