如何重写git历史记录来组合提交?

时间:2018-01-04 02:29:56

标签: git version-control bitbucket

我一直试图谷歌这个,但我认为我有一个特殊情况,如果有办法做到这一点,我不会抓住。

我在master分支中启动了一个项目,当它足够稳定时,它被复制到staging分支。不久之后,我决定从dev创建一个master分支。现在,在这一点上,我正在研究一个CI构建服务,并且正在试图使我的staging分支构建正确。所以现在我的临时分支在我的dev分支(oops)之前提交了31次提交,但是值得注意的是每个分支都是它自己的环境,在进行这种分支之前我没有忽略某些配置文件。所以我有一些我不想覆盖的文件,这就是为什么我想告诉Git / Bitbucket类似于" Dev,Staging和Master应该在哪里,我们将跟踪从现在开始的每一次承诺......忘记过去。"有没有办法做到这一点?

我研究了git rebase,但由于我处理了很多合并的提交,所以很容易让人感到困惑。

Bitbucket的截图 enter image description here

1 个答案:

答案 0 :(得分:0)

如果分支上的提交很少,您可以使用git rebase -i --root来压缩分支上的提交。

如果分支上有大量提交,您最好使用以下内容来压缩提交:

假设提交历史记录如下:

              S1---S2---…---S31  staging
             /
A---B---…---X  dev
             \
              P1---P2---P3---P4---P5  production

在[{1}}分支之前的stagingproduction分支上提交第一个提交部分:

dev

现在提交历史记录将是:

git checkout -b temp dev
git merge production --squash
git branch -f production
git checkout -B temp dev
git merge staging --squash
git branch -f staging

如果这是您所需要的,您可以按 S staging, temp / A---B---…---X dev \ P production 删除临时分支,然后按git branch -D temp推送到远程。

如果你仍然需要压缩git push -f --alldevstaging分支的提交,你可以通过以下命令继续压缩:

production

现在提交历史记录将是:

git checkout -B temp <commit id for A>
git merge production --squash
git branch -f production
git checkout -B temp <commit id for A>
git merge staging --squash
git branch -f staging
git checkout -B temp <commit id for A>
git merge dev --squash
git branch -f dev
git branch -D temp
git push -f --all