我希望摆脱很多我回购协议的旧历史,所以我做了一个浅表克隆以仅获取最后的50次提交:
git clone --depth=50 https://my.repo
Run Code Online (Sandbox Code Playgroud)
那行得通,但是当我创建一个新的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)
Run Code Online (Sandbox Code Playgroud)
但是我只希望这50次提交成为我的新仓库的历史记录。我怎么能告诉git它应该只将这50次提交作为新仓库中的唯一提交?
您无法将浅克隆推送到新的遥控器中。你必须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
这就是我最终要做的-效果很好。请注意,我正在从旧主机(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
Run Code Online (Sandbox Code Playgroud)
在那之后,我做了一个新仓库的新克隆,而我只得到了具有50个最近提交的master分支-正是我想要的!:-)提交历史记录已从250MB增加到50MB。哇
| 归档时间: |
|
| 查看次数: |
1460 次 |
| 最近记录: |