如何将整个存储库压缩到第一次提交?
我可以将base转换为第一次提交,但这将留给我2次提交。 有没有办法在第一个提交之前引用这个提交?
如何将整个存储库压缩到第一次提交?
我可以将base转换为第一次提交,但这将留给我2次提交。 有没有办法在第一个提交之前引用这个提交?
当前回答
最简单的方法是使用'plumbing'命令update-ref删除当前分支。
你不能使用git branch -D,因为它有一个安全阀来阻止你删除当前的分支。
这会让你回到“初始提交”状态,在那里你可以开始一个新的初始提交。
git update-ref -d refs/heads/master
git commit -m "New initial commit"
其他回答
首先,使用git rebase -interactive将所有提交压缩成一个提交。现在就剩下两个壁球提交了。要做到这一点,请阅读任何一本
如何合并Git存储库的前两次提交? Git:如何压缩前两次提交?
当我从git存储库恢复模板时,我通常会压缩整个树,以获得更干净的历史记录,并确保合法合规。我的工作流程是这样的:
git clone https://git.invalid/my-awesome-template.git my-awesome-foo
cd !$
git branch -M master my-awesome-template/master
git checkout --orphan master
git rm -rf /
git commit --allow-empty --allow-empty-message -m 'Initial commit'
git merge --squash --allow-unrelated-histories my-awesome-template/master
git commit
git branch -D my-awesome-template/master
# you can now `drop' that "Initial commit":
git rebase -i --root
这将把整个历史记录压缩到一个大的提交消息中。
在这个例子中:
Master是工作分支 My-awesome-template /master是一个中间分支
如果您想要做的只是将所有提交压缩到根提交,那么while
git rebase --interactive --root
可以工作,但对于大量提交(例如,数百个提交)来说是不切实际的,因为在生成交互式的重基编辑器提交列表以及运行重基本身时,重基操作可能会运行得非常慢。
当你压缩大量的提交时,这里有两个更快更有效的解决方案:
替代解决方案#1:孤儿分支
你可以简单地在你当前分支的顶端(即最近的提交)创建一个新的孤儿分支。这个孤儿分支形成了一个全新的独立提交历史树的初始根提交,这实际上相当于压缩了你所有的提交:
git checkout --orphan new-master master
git commit -m "Enter commit message for your new initial commit"
# Overwrite the old master branch reference with the new one
git branch -M new-master master
文档:
git-checkout(1)手册。
备选方案2:软复位
另一个有效的解决方案是简单地使用混合或软重置到根提交<root>:
git branch beforeReset
git reset --soft <root>
git commit --amend
# Verify that the new amended root is no different
# from the previous branch state
git diff beforeReset
文档:
git-reset(1)手动页面。
要做到这一点,你可以将本地git存储库重置为第一个commit标签,这样你在提交后的所有更改都将被取消,然后你可以使用——modify选项提交。
git reset your-first-commit-hashtag
git add .
git commit --amend
然后编辑第一个提交nam如果需要和保存文件。
我通常是这样做的:
Make sure everything is committed, and write down the latest commit id in case something goes wrong, or create a separate branch as the backup Run git reset --soft `git rev-list --max-parents=0 --abbrev-commit HEAD` to reset your head to the first commit, but leave your index unchanged. All changes since the first commit will now appear ready to be committed. Run git commit --amend -m "initial commit" to amend your commit to the first commit and change the commit message, or if you want to keep the existing commit message, you can run git commit --amend --no-edit Run git push -f to force push your changes