我有一个网站,当我开始开发它的时候,我已经克隆了和它提交的所有内容。现在我要删除所有原来的承诺,只留下我的。有办法这样做吗?我尝试了git重基,但它需要手动选择所有6k提交,并从最新版本中删除提交。(我需要从最老的)。
发布于 2020-03-05 14:57:04
我认为您可以通过签出特定的提交,在提交时创建一个孤立的分支,然后重新定位到分支上来完成这一任务。
git checkout <commit hash before your first commit>
git checkout --orphan foo
git commit -m 'Squashed!'
git checkout <original branch name>
git rebase foo
我用一个在master上使用5提交的存储库来测试这个
* 40f82e6 - 5
* 78deb2c - 4
* aff34f8 - 3
* 15cd469 - 2
* 6e420cb - 1让我们来压缩前三个(输出缩写):
$ git checkout aff34f8
Note: checking out 'aff34f8'.
HEAD is now at aff34f8 3
$ git checkout --orphan foo
Switched to a new branch 'foo'
$ git commit -m 'Squashed!'
[foo (root-commit) 933b62d] Squashed!
$ git checkout master
Switched to branch 'master'
$ git rebase foo
First, rewinding head to replay your work on top of it...
Applying: 1
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
No changes -- Patch already applied.
Applying: 2
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
No changes -- Patch already applied.
Applying: 3
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
No changes -- Patch already applied.
Applying: 4
Applying: 5在进行此更改之后,师父现在看起来如下:
* 5d908c9 - 5
* 477ee53 - 4
* 933b62d - Squashed!发布于 2020-03-05 14:28:45
如果您愿意丢失提交,您可以创建一个新的孤立分支。
在本例中,分支将被称为“新鲜”:
git checkout --orphan fresh阅读有关git checkout --orphan在this question中的更多信息。
发布于 2020-03-05 14:37:35
您可以使用git rebase,但是使用6k左右的提交就可以压缩,这可能非常麻烦。
另一种选择可以是做以下工作:
从您的最后提交开始,创建一个新分支:git checkout -b my-branch
git reset --soft [hash of initial commit]git commit来提交这些更改。现在,在这个新创建的分支上,在initial之后有一个提交,它包含对原始6k提交的所有更改。
https://stackoverflow.com/questions/60547591
复制相似问题