【发布时间】:2020-03-05 14:26:51
【问题描述】:
我有一个网站,当我开始开发它时,我已经克隆了 Laravel github repo 及其所有提交。现在我想删除所有原始提交,只留下我的。有没有办法做到这一点?我尝试了 git rebase,但它需要手动选择所有 6k 提交并从最新的提交中删除。 (我需要从最老的开始)。
【问题讨论】:
标签: git
我有一个网站,当我开始开发它时,我已经克隆了 Laravel github repo 及其所有提交。现在我想删除所有原始提交,只留下我的。有没有办法做到这一点?我尝试了 git rebase,但它需要手动选择所有 6k 提交并从最新的提交中删除。 (我需要从最老的开始)。
【问题讨论】:
标签: git
我认为您可以通过检查特定提交、在该提交处创建一个孤立分支,然后重新定位到该分支来做到这一点。
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
更改后,master 现在看起来像这样:
* 5d908c9 - 5
* 477ee53 - 4
* 933b62d - Squashed!
【讨论】:
如果您愿意丢失提交,您可以创建一个新的孤立分支。
在本例中,分支将被称为“新鲜”:
git checkout --orphan fresh
在this question 中阅读有关git checkout --orphan 的更多信息。
【讨论】:
你可以使用git rebase,但是有6k左右的commits to squash,这可能很麻烦。
另一种选择可能是执行以下操作:
从您的最终提交中,创建一个新分支:git checkout -b my-branch
“重置”您的暂存文件,使其成为第一次提交和当前状态之间的差异:git reset --soft [hash of initial commit]
现在,您的暂存文件是初始提交和最后一次提交之间的所有差异。发送git commit 以提交这些更改。
现在,在这个新创建的分支上,您在初始之后有一个提交,其中包含对那些原始 6k 提交的所有更改。
【讨论】: