简体   繁体   English

Git - 如何压缩选定的提交?

[英]Git - How to squash selected commits?

I have the following git history:我有以下 git 历史:

        X --- X --- X --- X --- X feature_branch 
       /           /                        
X --- X --- X --- X develop

As you can see at some point the develop branch has been merged to the feature_branch.正如您在某些时候所看到的,develop 分支已合并到 feature_branch。 Now I want to merge feature_branch to develop and I want to have only two commits in the feature branch history:现在我想合并 feature_branch 来开发,我想在功能分支历史中只有两个提交:

  • commit one (squashed all feature branch commits)提交一(压扁所有功能分支提交)
  • commit two (merge develop to feature branch commit)提交二(将开发合并到功能分支提交)

I have tried to use git rebase -i develop but it doesn't show merge develop to feature branch commit.我尝试使用git rebase -i develop但它没有显示合并开发到功能分支提交。

How to do it ?怎么做 ?

Edit1 After git rebase -i HEAD~6 (6 because merge is a separate commit after feature branch commit 3, it is a little bit simplifier on the above diagram) I have something like this: Edit1git rebase -i HEAD~6 之后(6 因为在功能分支提交 3 之后合并是一个单独的提交,它在上图中稍微简化了一点)我有这样的事情:

pick 1efad76 feature commit1
pick bae14d6 feature commit2
pick 3de7c48 feature commit3
pick 01dbb6a develop commit3
pick 674b2aa develop commit4
pick db5d9be feature commit4
pick 9cbe436 feature commit5

Merge commit is visible as two develop commits - develop commit3 and develop commit4.合并提交可见为两个开发提交 - 开发 commit3 和开发 commit4。 How put there "s" to achieve my goal ?如何放“s”来实现我的目标? Should I somehow reorder those commits ?我应该以某种方式重新排序这些提交吗?

You should be able to achieve the intended result by doing the following:您应该能够通过执行以下操作来达到预期的结果:

git rebase -i HEAD~5 swap out 5 for as many commits as required git rebase -i HEAD~5根据需要换出 5 个提交

From there you'll be able to things like fixup which will squash the commit, and discard the commit's log message.从那里,您将能够进行fixup ,这将压缩提交,并丢弃提交的日志消息。

Take your time with it, make sure you read what git is telling you, and if you make a mistake - git reflog has got you covered - you can reset to any point in your history and undo many things with reflog - it's got me out of many pickles before!慢慢来,确保你阅读了 git 告诉你的内容,如果你犯了一个错误 - git reflog已经涵盖了你 - 你可以reset到历史中的任何一点并使用reflog撤消许多事情 - 它让我出局之前的许多泡菜! More info here更多信息在这里

Once you're done with your interactive rebase, and you've got your commit history looking how you want it, you can go ahead and do a fast-forward merge on develop if you're finished with the feature (see below) - or continue with your feature until ready.一旦您完成了交互式 rebase,并且您已经获得了您想要的提交历史记录,如果您完成了该功能,您可以继续进行快速合并开发(见下文) -或继续您的功能,直到准备就绪。

git checkout develop
git fetch
git pull
git merge --ff-only feature_branch

A pull will do a fetch automatically, but a it's a personal preference of mine to see what's being updated before I pull . pull将自动执行fetch ,但我个人偏好在pull之前查看正在更新的内容。

Note: if you're submitting a pull-request, then it will do some of this for you or warn if it can't and tell you what to do ;)注意:如果你提交了一个 pull-request,那么它会为你做一些这样的事情,或者如果它不能,就会警告你并告诉你该怎么做 ;)

If you are continuing your feature, and develop is being updated often, then it's a good idea to keep your feature up to date with develop.如果您正在继续您的功能,并且develop经常更新,那么最好让您的功能与开发保持同步。

You can do a git rebase origin/develop or just develop - but again, it's a preference of mine to be up to date with the origin - which I will fetch first.你可以做一个git rebase origin/develop或只是develop - 但同样,我的偏好是与origin - 我将首先fetch

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

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