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

问题描述 投票:1回答:1

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

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

我查看了git rebase,但由于我正在处理那么多合并的提交,所以它很混乱。

截图来自Bitbucket enter image description here

git version-control bitbucket
1个回答
0
投票

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

如果分支上有大量的提交,你最好使用下面的方法来压缩提交:

假设提交历史如下:

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

第一个壁球部分提交在staging分支之前的productiondev分支:

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 --all推送到远程。

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

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

现在提交历史将是:

  S'  staging
 /
A---X'  dev
 \
  P'  production
© www.soinside.com 2019 - 2024. All rights reserved.