简体   繁体   English

如何在保留“最新”提交的同时保留多个“先前”提交?

[英]How to squash a number of “previous” commits while leaving “latest” commits preserved?

I had cloned one of Github boilerplate project but forgot to reset the history of the original repo and have been creating quite an amount of commits onto its master branch. 我已经克隆了一个Github样板项目,但忘了重设原始仓库的历史,并且一直在其主分支上创建了大量提交。

For now my commit history looks like this. 现在,我的提交历史看起来像这样。

Initial commit - (hundreds of boilerplate commits) - (tens of my commits) 初始提交-(数百个样板提交)-(我的数十个提交)

What I'd like to do is to "squash" (or "combine" if "squash" here isn't an appropriate word) all of the commits from the original repo that I've cloned from while leaving my latest commits on history. 我想做的是“压扁”(如果这里的“压扁”不是一个合适的词,则“组合”)我从原始存储库克隆的所有提交,同时将最新提交保留在历史上。 I was trying to do this by using git rebase --root -i but there're hundreds of commits to squash (the orignal repo) and pick (my own repo) which makes it hard to do it that way. 我试图通过使用git rebase --root -i来做到这一点,但是有数百个提交要压榨(原始回购)和选择(我自己的回购),这很难做到。

Is there a convenient way to achieve this? 有没有方便的方法可以做到这一点? I don't mind if I lose all the history before my own commits start, or if I create a new repo. 我不在乎是否在我自己的提交开始之前丢失了所有历史记录,或者是否创建了新的存储库。

In order to do a git squash follow those steps: 为了做一个git南瓜,请遵循以下步骤:

// X is the number of commits you wish to squash
git rebase -i HEAD~X

Once you squash your commits - choose the s for squash = it will combine all the commits into a single commit. 压缩提交后-选择s为squash =它将把所有提交合并到一个提交中。

Choose pick for the latest commit in order to preserve it. 选择选择最新提交以保留它。

在此处输入图片说明

Assuming the last "boiler plate commit" SHA is abc123 , and your current branch is master , do the following: 假设最后一个“样板提交” SHA为abc123 ,而当前分支为master ,则执行以下操作:

git branch my-new-branch master
git checkout abc123 --orphan temp-branch
git commit -m "Initial boilerplate code"
git rebase --onto temp-branch abc123 my-new-branch
git branch -d temp-branch

This will leave you with with my-new-branch pointing to all your commits followed by a single squash commit of the boiler plate code. 这将使您拥有my-new-branch指向您的所有提交,然后是样板代码的一次压榨提交。 You can push this branch to a new repo, or (if this is a local only repo, or you don't mind doing a force push) removing master and renaming my-new-branch to master , or whatever. 您可以将该分支推送到新的存储库,或者(如果这是本地唯一的存储库,或者您不介意执行强制推送)则删除master并将my-new-branch重命名为master或其他。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM