git 如何将浅克隆推送到新的repo?

pjngdqdw  于 2023-11-15  发布在  Git
关注(0)|答案(2)|浏览(181)

我希望摆脱很多我的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次提交作为新仓库中的唯一提交呢?

hc2pp10m

hc2pp10m1#

这是我最终做的-它工作得很完美。请注意,我正在从旧主机(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

字符串
在那之后,我做了一个新的仓库的新克隆,我得到了一个主分支,里面有50个最近的提交--正是我想要的!:-)提交历史从250 MB变成了50 MB。

1mrurvl1

1mrurvl12#

你不能将一个浅克隆推送到一个新的远程。你必须先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

相关问题