Git 压缩分支中的所有提交而不会发生冲突
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17354353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git squash all commits in branch without conflicting
提问by Andy Ray
A common development workflow for us is to checkout branch b
, commit a bunch to it, then squash all those commits into one (still on b
).
对我们来说,一个常见的开发工作流程是检出分支b
,向它提交一堆,然后将所有这些提交压缩成一个(仍在b
)。
However, during the rebase -i
process to squash all the commits, there are frequently conflicts at multiple steps.
但是,在rebase -i
压缩所有提交的过程中,多个步骤经常会发生冲突。
I essentially want to alter the branch into one commit that represents the state of the repository at the time of the final commit on b
我基本上想将分支更改为一个提交,该提交代表在最终提交时存储库的状态 b
I've done some searching but I haven't found exactly what I'm looking for. I don't want to merge --squash
because we would like to test the squashed feature branch before merging.
我已经做了一些搜索,但我还没有找到我正在寻找的东西。我不想这样做,merge --squash
因为我们想在合并之前测试压缩的功能分支。
回答by Rasmus ?stergaard Kj?r Voss
If you don't need the commit information, then you could just do a soft reset. Then files remain as they were and when you commit, this commit will be on top of the commit you did reset to.
如果您不需要提交信息,那么您可以进行软重置。然后文件保持原样,当您提交时,此提交将位于您重置的提交之上。
To find the commit to reset to:
找到要重置的提交:
git merge-base HEAD BRANCH_YOU_BRANCHED_FROM
Then
然后
git reset --soft COMMIT_HASH
Then re-craft the commit, perhaps:
然后重新制作提交,也许:
git commit -am 'This is the new re-created one commit'
回答by ColinM
This is simlar to the answer from Rasmus but broken down into three steps that should always work:
这与 Rasmus 的答案相似,但分为三个应该始终有效的步骤:
$ git merge feature1
$ git reset --soft HEAD@{1}
$ git commit -c feature1
Explanation:
解释:
- merge and resolve conflicts
- keep changes staged but reset to old head
- commit all changes using commit message and author from feature branch latest commit
- 合并和解决冲突
- 保持更改上演但重置为旧头
- 使用来自功能分支最新提交的提交消息和作者提交所有更改