如何将整个存储库压缩到第一次提交?
我可以将base转换为第一次提交,但这将留给我2次提交。 有没有办法在第一个提交之前引用这个提交?
如何将整个存储库压缩到第一次提交?
我可以将base转换为第一次提交,但这将留给我2次提交。 有没有办法在第一个提交之前引用这个提交?
当前回答
创建备份
git branch backup
重置为指定提交
git reset --soft <#root>
然后将所有文件添加到登台
git add .
提交而不更新消息
git commit --amend --no-edit
推送新分支与压缩提交到回购
git push -f
其他回答
这对我来说效果最好。
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.
要做到这一点,你可以将本地git存储库重置为第一个commit标签,这样你在提交后的所有更改都将被取消,然后你可以使用——modify选项提交。
git reset your-first-commit-hashtag
git add .
git commit --amend
然后编辑第一个提交nam如果需要和保存文件。
我读过一些关于移植的东西,但从来没有深入研究过。
不管怎样,你可以手动压缩最后2次提交,就像这样:
git reset HEAD~1
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
我通常是这样做的:
Make sure everything is committed, and write down the latest commit id in case something goes wrong, or create a separate branch as the backup Run git reset --soft `git rev-list --max-parents=0 --abbrev-commit HEAD` to reset your head to the first commit, but leave your index unchanged. All changes since the first commit will now appear ready to be committed. Run git commit --amend -m "initial commit" to amend your commit to the first commit and change the commit message, or if you want to keep the existing commit message, you can run git commit --amend --no-edit Run git push -f to force push your changes