简体   繁体   English

如何重写git历史记录以合并提交?

[英]How to rewrite git history to combine commits?

I've been trying to Google this but I think I have a special case and I'm not grasping if there's a way to do this. 我一直在尝试使用Google,但我认为我有一个特殊情况,我不知道是否有办法做到这一点。

I started a project in a master branch and when it was stable enough, it was duplicated to a staging branch. 我在master分支中启动了一个项目,当它足够稳定时,将其复制到staging分支中。 Not long after, I decided to create a dev branch from master . 不久之后,我决定从master创建一个dev分支。 Now at this point, I was looking into a CI build service and was pounding away at code trying to make my staging branch build correctly. 现在,我正在研究CI构建服务,并且正在研究试图正确构建我的staging分支的代码。 So now my staging branch is 31 commits ahead of my dev branch (oops) but it's worth noting that each branch is its own environment and I didn't ignore certain config files before doing this branching. 因此,现在我的暂存分支比我的dev分支(oops)提前31个提交,但值得注意的是每个分支都是其自己的环境,在执行此分支之前,我没有忽略某些配置文件。 So I have a few files that I'm not trying to overwrite, which is why I want to tell Git/Bitbucket something like " Dev, Staging and Master are where they should be and we will track every commit from now on...forget the past. " Is there a way to do this? 因此,我有一些我不想覆盖的文件,这就是为什么我要告诉Git / Bitbucket之类的东西,例如“ Dev,Staging和Master在它们应该在的位置,并且我们将从现在开始跟踪每次提交...忘记过去。 “有没有办法做到这一点?

I looked into git rebase but it was confusing since I'm dealing with so many commits that are merged. 我研究了git rebase,但由于我要处理的合并的提交数量众多,这令人困惑。

Screenshot from Bitbucket Bitbucket的屏幕截图 在此处输入图片说明

If there are few commits on branches , you can use git rebase -i --root to squash commits on the branches. 如果分支上的提交很少 ,则可以使用git rebase -i --root分支上的提交。

If there are large number of commit on branch , you'd better use the below as below to squash commits: 如果branch上有大量提交 ,则最好使用以下方法来压缩提交:

Assume the commit history as below: 假定提交历史如下:

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

First squash part of commits on staging and production branches which are ahead of dev branch: 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

Now the commit history will be: 现在的提交历史将是:

              S  staging, temp
             /
A---B---…---X  dev
             \
              P  production

If this is what you need, you can delete temp branch by git branch -D temp and then push to remote by git push -f --all . 如果需要,可以通过git branch -D temp删除temp分支,然后通过git push -f --all推送到remote。

If you still need to squash the commit for dev , staging and production branch, you can continue squash by below commands: 如果仍然需要压榨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

Now the commit history will be: 现在的提交历史将是:

  S'  staging
 /
A---X'  dev
 \
  P'  production

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM