Git: Interactively rebase a range of commits

谁说胖子不能爱 提交于 2019-12-10 12:53:52

问题


I'm trying to rebase -i a few commits that occurred a while back in my history. Say I have a log like this:

* 5e32fb0 (HEAD -> master) Add latest feature
* 106c31a Add new feature
* 2bdac33 Add great feature
...100 other commits...
* 64bd9e7 Add test 3
* 3e1066e Add test 2
* 26c612d Add test 1
* 694bdda Initialize repo

and I want to squash the 3 test commits. In these circumstances, git rebase -i HEAD~106 isn't very practical. What I'm looking for instead is something like git rebase -i 64bd9e7:26c612d.

Does git have this sort of behaviour, and if so, how can I use it?

I had a look at this post, but it didn't answer my question.


回答1:


In order to squash those three commits, you'll need to re-write history for the entire repository. That is, all the commit hashes for commits after those test commits will be modified. The most straight-forward way to do it, in my opinion, is to:

  1. Create a temporary branch and checkout pointing at 64bd9e7.
  2. Squash the three commits together.
  3. Checkout your main branch, and reset it to the commit immediately after 64bd9e7.
  4. Rebase it onto your temporary branch



回答2:


Just use rebase -i and squash only those commits.

git rebase -i <main_branch>
# Now just add `s` in front of all the commits you want to squash

If you have many commits, I'd suggest you to take a look at rebase --onto -

git rebase --onto <final_base_commit> <initial_base_commit> <head>

git rebase --onto 694bdda 64bd9e7 5e32fb0

Note: Rewriting history is an advanced operation, so tread with care.

Git rebase tutorial



来源:https://stackoverflow.com/questions/45336573/git-interactively-rebase-a-range-of-commits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!