如何将整个存储库压缩到第一次提交?
我可以将base转换为第一次提交,但这将留给我2次提交。 有没有办法在第一个提交之前引用这个提交?
如何将整个存储库压缩到第一次提交?
我可以将base转换为第一次提交,但这将留给我2次提交。 有没有办法在第一个提交之前引用这个提交?
当前回答
这对我来说效果最好。
git rebase -X ours -i master
这将使git更喜欢你的特性分支;避免艰苦的合并编辑。您的分支需要与主机同步。
ours
This resolves any number of heads, but the resulting tree of the merge is always that of the current
branch head, effectively ignoring all changes from all other branches. It is meant to be used to
supersede old development history of side branches. Note that this is different from the -Xours
option to the recursive merge strategy.
其他回答
如果您想要做的只是将所有提交压缩到根提交,那么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)手动页面。
echo "message" | git commit-tree HEAD^{tree}
这将创建一个带有HEAD树的孤立提交,并在stdout上输出其名称(SHA-1)。然后重置你的分支。
git reset SHA-1
要在一个步骤中完成上述工作:
git reset $(git commit-tree HEAD^{tree} -m "Initial commit.")
从git 1.6.2开始,你可以使用git rebase——root -i
对于除第一次提交外的每个提交,将pick更改为squash。
假设你有这些从06年到10年的提交。
您希望将06年后的提交合并为一个提交Id。
commit 10
commit 09
commit 08
commit 07
commit 06
你可以软复位git。
git reset --soft "06"
然后,运行下面的命令将这些更改推送到远程分支。
git push origin HEAD --force
现在,您之前所做的所有提交都应该作为您的本地更改可用,您可以将所有这些提交合并到单个提交中。
现在,新的提交结构应该如下所示:
commit <new_commit_containing_all_7_8_9_10>
commit 06
最简单的方法是使用'plumbing'命令update-ref删除当前分支。
你不能使用git branch -D,因为它有一个安全阀来阻止你删除当前的分支。
这会让你回到“初始提交”状态,在那里你可以开始一个新的初始提交。
git update-ref -d refs/heads/master
git commit -m "New initial commit"