简体   繁体   English

git:如何压缩多个合并提交

[英]git: how to squash multiple merge commits

I'm working on a git repository which I forked from an upstream. 我正在研究一个我从上游分叉的git存储库。 There was a feature I wanted to implement and I created a new branch uiTests and did changes there and made a pull request. 有一个我想要实现的功能,我创建了一个新的分支uiTests并在那里进行了更改并发出了拉取请求。

The PR was there for a while and in the meantime there were some other PRs got merged and came conflicts. PR已经存在了一段时间,与此同时还有一些其他PR合并并且出现了冲突。 I resolved them in my fork and there were two merge commits at the end. 我在我的fork中解决了它们,最后有两个合并提交。

When I check the git log, the top of the list looks like this; 当我检查git日志时,列表的顶部看起来像这样;

commit 70db4de884d5e4b64ef3a2c903f310c901dd68e2
Merge: ef5dfc2 5b7a827
Author: Padmal
Date:   Sat May 26 18:53:33 2018 +0530

    Merge branch 'CloudyPadmal-uiTest' into uiTests

commit 5b7a827763c35a3605daed6c717004700582eede
Merge: e815867 ef5dfc2
Author: Padmal
Date:   Sat May 26 18:52:52 2018 +0530

    Merge branch 'uiTests' of git://github.com/CloudyPadmal/pslab-android into CloudyPadmal-uiTest

commit ef5dfc2f4af7431b2bc5efa356540bd616669706
Author: Padmal
Date:   Thu May 24 20:06:27 2018 +0530

    test: removed Travis build time consuming UI tests

    removed dummy test files

So I wanted to squash 70db4de884d5e4b64ef3a2c903f310c901dd68e2 and 5b7a827763c35a3605daed6c717004700582eede onto ef5dfc2f4af7431b2bc5efa356540bd616669706 and update the PR with one commit. 所以我想将70db4de884d5e4b64ef3a2c903f310c901dd68e25b7a827763c35a3605daed6c717004700582eedeef5dfc2f4af7431b2bc5efa356540bd616669706并用一次提交更新PR。

For more information, git log --oneline results is as follows; 有关更多信息, git log --oneline结果如下;

70db4de Merge branch 'CloudyPadmal-uiTest' into uiTests
5b7a827 Merge branch 'uiTests' of git://github.com/CloudyPadmal/pslab-android into CloudyPadmal-uiTest
ef5dfc2 test: removed Travis build time consuming UI tests

But when I tried git rebase -i HEAD~3 , the console looks like this; 但是当我尝试git rebase -i HEAD~3 ,控制台看起来像这样;

pick 9b97740 Changed Help and Feedback Activity to Fragment (#882)
pick 7e93db6 chore: Update app version (#922)
pick ca155b6 Added card view for instruments section (#884)
pick e815867 Removed AboutUs activity (#914)
pick ef5dfc2 test: removed Travis build time consuming UI tests

It doesn't show me the merge commits. 它没有向我显示合并提交。 For more information, following is the version log from Android Studio; 有关更多信息,以下是Android Studio的版本日志;

git日志视图

Am I following the correct method to squash these merge commits? 我是否按照正确的方法来压缩这些合并提交? Or is it possible to squash these commits at all? 或者是否有可能压制这些提交? These kind of squashing merge commits been a little problem for me for a while. 这种压缩合并提交对我来说有点问题了。 Thanks a lot for any help and tips! 非常感谢任何帮助和提示! :) :)

Let me assume that you have an upstream repo branch called "development" and your own branch called "uiTests". 让我假设您有一个名为“development”的上游repo分支和您自己的名为“uiTests”的分支。 And you have created a PR from fork/uiTests to upstream/development (so you have 2 remotes: fork and upstream ). 你已经从fork/uiTestsupstream/development创建了一个PR(所以你有2个遥控器: forkupstream )。

After conflict resolution you have some mess in the log, but you have tested that your working copy state is good, the app works, your changes work, all good, and all you want is to have your work all in 1 commit and update the PR. 解决冲突之后你会在日志中弄得一团糟,但是你已经测试过你的工作副本状态是好的,应用程序是否正常,你的更改是否正常,一切都很好,你想要的就是让你的工作全部在1中提交并更新PR。

One simple solution is to run: 一个简单的解决方案是运行:

git reset --soft upstream/development

And then re-commit all your changes again: 然后再次重新提交所有更改:

git add .
git commit

After this you are going to have just 1 commit with your changes on top of the new upstream branch. 在此之后,您将在新的上游分支之上只进行1次更改。

Assuming that you do all this in the "uiTests" branch, all you need to update the PR is to: 假设您在“uiTests”分支中执行了所有这些操作,您需要更新PR的所有内容是:

git push -f fork

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

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