如何 git rebase -i 进行一系列提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7758128/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:04:13  来源:igfitidea点击:

How to git rebase -i for a range of commits?

gitcommitgit-branchrebasegit-rebase

提问by Dylan Valade

Can I squash a range of commits for a local feature/topic branch using rebase that does not includethe most recent commit? This is for commits that I want to prepare before they get merged and pushed to a public repo.

我可以使用不包含最近提交的rebase 压缩本地功能/主题分支的一系列提交吗?这是针对我想在合并并推送到公共存储库之前准备的提交。

I was working quickly and made a bunch of minor changes with poor titles and descriptions that I want to squash into two or three separate logical commits with a great comments. Can I select a range of commits between 329aed9 and af39283 that could be at any point in this feature branch's short history?

我工作很快,做了一堆小改动,标题和描述都很糟糕,我想把它们分成两三个单独的逻辑提交,并附上很好的评论。我可以选择 329aed9 和 af39283 之间的一系列提交,这些提交可能在此功能分支的短暂历史中的任何一点吗?

git rebase -i RANGE_START_COMMIT_ID RANGE_LAST_COMMIT_ID

Thanks!

谢谢!

采纳答案by Karl Bielefeldt

You could always create a new branch with git checkout -b new_branch af39283, and then rebase that. However, if you want to include the later commits at some future point, there's no getting around rebasing them as well. The SHA1 for a commit depends on all its ancestor commits.

你总是可以用 来创建一个新的分支git checkout -b new_branch af39283,然后重新设置它的基础。但是,如果您想在将来的某个时间点包含较晚的提交,则也无法绕过它们。提交的 SHA1 取决于其所有祖先提交。

回答by Matt Enright

So, it's not entirely clear what you mean by "not including" the most recent commit, but when you do a rebase -iyou're able to squash/re-order/reword/fixup/remove prior commits without having to do anything to the last commit. You're rewriting the history underneath it of course, so its diff will be re-applied and it will be a different commit object following the rebase, but since you haven't pushed this publicly (and you're rewriting the rest of it) that shouldn't matter much.

因此,“不包括”最近的提交是什么意思并不完全清楚,但是当您执行 a 时,rebase -i您可以压缩/重新排序/改写/修复/删除先前的提交,而无需对最后一次执行任何操作犯罪。当然,您正在重写它下面的历史记录,因此将重新应用它的差异,并且在变基后它将是一个不同的提交对象,但是由于您没有公开推送它(并且您正在重写它的其余部分) ) 那应该没什么关系。