如何一次压扁/变基 [英] How to squash/rebase in a single shot
问题描述
如何才能将我的所有提交(即使具有合并和冲突解决方案)压榨到一个功能分支上的单个提交,然后在我开始开发的分支之上重新建立基础从? 不想重做已经解决的冲突.尽量避免麻烦.
How can I, with minimum effort, squash all my commits (even with merges and conflict resolutions) to a single one on a feature branch and then rebase on top of the branch where I started developing from? Don't want to redo conflict resolution that's already done. Keep hassle to a minimum.
假设我们正在谈论的分支是master和featureX.
Suppose the branches we are talking about are master and featureX.
推荐答案
首先使用git branch
检查,您当前的工作分支是什么.接下来,如果还没有featureX
,请使用git checkout featureX
切换到它.
First check with git branch
, what's your current working branch. Next, if it isn't featureX
already, switch to it, using git checkout featureX
.
我们现在将计算出您想压缩成一个位置的提交数量.为此,我们将使用git log
.要获得更好的视图,请使用git log --graph --decorate --pretty=oneline --abbrev-commit
.
We'll now figure out how many commits there where that you want to squash into one. For this we'll use git log
. To get a bit nicer view of it, use git log --graph --decorate --pretty=oneline --abbrev-commit
.
接下来要进行实际压扁:git rebase -i HEAD~<NUMBER OF COMMITS TO SQUASH>
或使用SHA哈希git rebase -i <SHA>
.如果您需要有关此步骤的帮助,请阅读以下两篇不错的文章: https://thoughtbot.com/blog/git-interactive-rebase-squash-amend-rewriting-history 和
Next to do the actual squashing: git rebase -i HEAD~<NUMBER OF COMMITS TO SQUASH>
or alternatively use the SHA hash git rebase -i <SHA>
. If you need help with this step, here are two pretty nice articles: https://thoughtbot.com/blog/git-interactive-rebase-squash-amend-rewriting-history and https://medium.com/@dirk.avery/the-definitive-git-rebase-guide-dbd7717f9437
现在,我们要确保已经拥有master
分支的最新版本. git checkout master
,然后是git pull origin master
(假设您将原点用作上游)和git checkout featureX
.
Now we want to make sure, we've got the latest version of the master
branch. git checkout master
followed by git pull origin master
(assuming you use origin as upstream) and git checkout featureX
.
最后,我们可以使用git rebase master
将featureX
改编为master
,并可以将featureX
推送到GitHub,GitLab或我们喜欢的任何地方. git push origin featureX
.
注意:如果您以前曾按过featureX
,则需要强制按git push origin featureX --force
.确保仅在您正在工作的分支上执行此操作,这些分支不会共享,否则人们会遇到麻烦.
Finally we can rebase featureX
onto master
using git rebase master
and can push featureX
to GitHub, GitLab or where ever we please. git push origin featureX
.
Note: if you've previously pushed featureX
, you'll need to force push git push origin featureX --force
. Make sure to only do this on branches you're working on, that aren't shared, otherwise people will get into troubles.
可以在以下位置找到一篇文章,该文章解释了整个基础和壁球的内容,以使提交历史记录保持良好的清洁度: https://blog.carbonfive.com/2017/08/28/always-squash-and-rebase-your-git-commits/
An article explaining the whole rebase and squash stuff to keep the commit history clean pretty good can be found here: https://blog.carbonfive.com/2017/08/28/always-squash-and-rebase-your-git-commits/
这篇关于如何一次压扁/变基的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!