合并 Git 存储库的前两次提交?
-
22-07-2019 - |
题
假设您有包含三个提交的历史记录 甲、乙 和 C:
A-B-C
我想合并这两个提交 A 和 乙 一次提交 AB:
AB-C
我试过
git rebase -i A
这将打开我的编辑器,其中包含以下内容:
pick e97a17b B
pick asd314f C
我将其更改为
squash e97a17b B
pick asd314f C
然后 Git 1.6.0.4 说:
Cannot 'squash' without a previous commit
有办法还是这根本不可能?
其他提示
您尝试:
git rebase -i A
有可能的,如果你继续edit
开始一样,而不是squash
:
edit e97a17b B
pick asd314f C
然后运行
git reset --soft HEAD^
git commit --amend
git rebase --continue
完成。
A
是最初的承诺,但现在你想B
是最初的承诺。 GIT中提交是即使它们通常在它们引入的diff来描述和查看整个树,不DIFFS。
此配方工程,即使有A和B,B和C之间的多个的提交。
# Go back to the last commit that we want
# to form the initial commit (detach HEAD)
git checkout <sha1_for_B>
# reset the branch pointer to the initial commit,
# but leaving the index and working tree intact.
git reset --soft <sha1_for_A>
# amend the initial tree using the tree from 'B'
git commit --amend
# temporarily tag this new initial commit
# (or you could remember the new commit sha1 manually)
git tag tmp
# go back to the original branch (assume master for this example)
git checkout master
# Replay all the commits after B onto the new initial commit
git rebase --onto tmp <sha1_for_B>
# remove the temporary tag
git tag -d tmp
在互动底垫的情况下,你有之前做它,这样的名单将是:
pick A
pick B
pick C
成为:
pick A
squash B
pick C
如果A是初始提交,你必须有一个不同的初始提交之前A. GIT中认为在差异,它将在(A和B)和(B和C)之间的差工作。因此,壁球未在您的示例的工作。
如果您有数百或数千次提交,请使用 科斯特莫的回答 的
git rebase -i --root
可能不切实际且缓慢,因为 rebase 脚本必须处理大量提交 两次, ,一次生成交互式变基编辑器列表(您可以在其中选择每次提交要采取的操作),一次实际执行提交的重新应用。
这是一个 替代解决方案 这将避免生成交互式变基编辑器列表的时间成本 不使用交互式变基 首先。这样,就类似于 查尔斯·贝利的解决方案. 。您只需创建一个 孤儿分支 从第二次提交开始,然后将其上的所有后代提交变基:
git checkout --orphan orphan <second-commit-sha>
git commit -m "Enter a commit message for the new root commit"
git rebase --onto orphan <second-commit-sha> master
文档
在一个相关的问题,我设法想出一种不同的方法,有必要对挤压的第一提交,其是,井,以使其在第二个。
如果你有兴趣:的 GIT中:如何插入提交作为第一,所有其他移动
有小队GIT中的命令: GIT中变基-i HEAD〜[提交的数]
假设你有下面的git提交历史:
结果,挑5152061技艺:增加了保存图像的支持。 (一种) 结果挑39c5a04修复:bug修复。 (B) 结果挑839c6b3修复:冲突解决。 (C)
现在要压扁A和B AB,下面的步骤进行:
结果,挑5152061技艺:增加了保存图像的支持。 (一种) 点击小号39c5a04修复:bug修复。 (B) 结果挑839c6b3修复:冲突解决。 (C)
注:挤压承诺我们可以用南瓜或S。 最终的结果将是: 结果挑5152061壮举:增加了对保存图像的支持。 (AB) 结果挑839c6b3修复:冲突解决。 (C)
您必须执行一个比特的命令行的魔法。
git checkout -b a A
git checkout B <files>
git commit --amend
git checkout master
git rebase a
这应该留给你有AB和C作为犯的一个分支。