如何将整个存储库压缩到第一次提交?
我可以将base转换为第一次提交,但这将留给我2次提交。 有没有办法在第一个提交之前引用这个提交?
如何将整个存储库压缩到第一次提交?
我可以将base转换为第一次提交,但这将留给我2次提交。 有没有办法在第一个提交之前引用这个提交?
当前回答
对我来说是这样的: 我总共提交了4次,并使用了交互式rebase:
git rebase -i HEAD~3
第一次提交仍然存在,我最近进行了3次提交。
如果你卡在接下来出现的编辑器中,你会看到smth:
pick fda59df commit 1
pick x536897 commit 2
pick c01a668 commit 3
你必须先做出承诺,然后把其他人推到上面。你需要的是:
pick fda59df commit 1
squash x536897 commit 2
squash c01a668 commit 3
为此,使用INSERT键更改“插入”和“编辑”模式。
使用:wq保存并退出编辑器。如果您的光标位于这些提交行之间或其他位置,请按ESC并重试。
结果我有两次提交:第一次仍然存在,第二次带有“这是3次提交的组合”的消息。
详情请点击此处: https://makandracards.com/makandra/527-squash-several-git-commits-into-a-single-commit
其他回答
以下是我最后是如何做到这一点的,以防其他人也能做到:
记住,做这样的事情总是有风险的,在开始之前创建一个保存分支从来都不是一个坏主意。
从日志开始
git log --oneline
滚动到第一次提交,复制SHA
git reset --soft <#sha#>
将<#sha#>替换为从日志中复制的sha
git status
确保所有内容都是绿色的,否则运行git add -A
git commit --amend
将当前所有更改修改为当前首次提交
现在强制推这个分支,它会覆盖那里的内容。
对我来说是这样的: 我总共提交了4次,并使用了交互式rebase:
git rebase -i HEAD~3
第一次提交仍然存在,我最近进行了3次提交。
如果你卡在接下来出现的编辑器中,你会看到smth:
pick fda59df commit 1
pick x536897 commit 2
pick c01a668 commit 3
你必须先做出承诺,然后把其他人推到上面。你需要的是:
pick fda59df commit 1
squash x536897 commit 2
squash c01a668 commit 3
为此,使用INSERT键更改“插入”和“编辑”模式。
使用:wq保存并退出编辑器。如果您的光标位于这些提交行之间或其他位置,请按ESC并重试。
结果我有两次提交:第一次仍然存在,第二次带有“这是3次提交的组合”的消息。
详情请点击此处: https://makandracards.com/makandra/527-squash-several-git-commits-into-a-single-commit
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 log > original.log,然后在新的存储库中编辑你的初始提交消息:
rm -rf .git
git init
git add .
git commit
or
git log > original.log
# edit original.log as desired
rm -rf .git
git init
git add .
git commit -F original.log
这对我来说效果最好。
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.