简体   繁体   中英

Git squash commits with interactive rebase with merge commits between

I have a test repository with the following history:

commit eb4a4d52a8fe6abdebb93c7747beac2d511003af (HEAD, master) Merge: 22f849c 94e27d1 Author: Your Name Date: Mon Jan 27 00:51:42 2014 +0100

 Merge branch 'cheryr' Conflicts: FILE 

commit 22f849c403b6cdf43280e66e937931bf9d0ab25a Author: Your Name Date: Sun Jan 26 21:01:35 2014 +0100

 4 toto 

commit 94e27d1c78833784619e25eeb8e0186f154f2282 (cheryr) Author: Your Name Date: Mon Jan 27 00:48:12 2014 +0100

 toto 

commit 2368d78ba95811e9eb9897487cccb7b7f6927910 Author: Your Name Date: Sun Jan 26 22:31:51 2014 +0100

 10 

commit b1f0f8a1a1951e661a7e833314fc483085516b0c (tmp) Author: Your Name Date: Sun Jan 26 22:19:56 2014 +0100

 9 

commit 3a8f2e17e721821ae8ebd1e272437c8632224b9a Author: Your Name Date: Sun Jan 26 22:18:23 2014 +0100

 8 

commit 28d4a62d4d21c3e8155553e1216bfa981afe7212 Author: Your Name Date: Sun Jan 26 22:15:42 2014 +0100

 7 

Is this possibe to squash a few first commits into one, together with merge commit? It's not possible to simply pass HEAD~4 to git rebase -i because it will take 4 commits back from the first parent of the merge commit.

git rebase -i HEAD~4 would work just fine, also if it's a far commit or you don't know how far it is, take the hash of the parent of the first commit you want to edit, or for me I take the hash and append a ^ for saying parent

git rebase -i hash^

I think ~ would work too

git rebase -i hash~

Then you can set the first commit as pick p or reword if you want to change it's message r and the remaining 3 as fixup f so it doesn't keep stopping asking you for a new commit message, it will stop in the middle for any conflicts, also expect that you will need to re-resolve any conflicts if you pass by a commit that created one.

try: git rebase -i HEAD^2~3

'^' sign is used to select 1 of the parents (in this case ^2 means select second parent, and then ~3 means 'go 3 commits back')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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