Question
The commit history is:
$ git log --pretty=oneline
52756ce25b26442a21ebfe52655dcf9c6ba9aa98 ch6 changed
41d49f2fbd12c00af99a90b6fbd1e4af18db0103 ch5 changed
c24b32555b5fe98d556acc7d32a2bd68346d40ba ch4 changed
4088dde16b47f39d1b5f9553bc902e2afc34bf9a ch3 changed
ea37405f9b59bef389ef6ca6a0c6ae8ad6365fb9 Fix some syntax problem
...
You want to combine changes in chapter 3, 4 and 5.
Solution
git rebase -i ea37405f
Here "ea37405f" is the commit older than the commits you want to combine.
Now in an editor like:
pick 4088dde ch3 changed
pick c24b325 ch4 changed
pick 41d49f2 ch5 changed
pick 52756ce ch6 changed
Notice the timeline order is reversed compared with that in git log
:
the newest commit ("ch6 changed") is now in the last line, instead of the first.
Modify the content like this:
pick 4088dde ch3 changed
s c24b325 ch4 changed
s 41d49f2 ch5 changed
pick 52756ce ch6 changed
Here "s" means squashing the commit into previous (older) commit.
Save and quit, you now in an editor for new commit message, input:
chapter 3, 4 and 5 changed
Save and you get a message "Successfully rebased and updated refs/heads/master".
Discussion
Check the history after rebase:
$ git log --pretty=oneline
b4463e99ad221f270fd9f51d5bc7e8b348805804 ch6 changed
276932a252b9041085fb30d0d5e4082db8c03584 chapter 3, 4 and 5 changed
ea37405f9b59bef389ef6ca6a0c6ae8ad6365fb9 Fix some syntax problem
You can see the changes in files with git diff <older> <newer>
, here it is:
git diff HEAD~2 HEAD~
, or git diff ea37405 276932a
.
If something wrong happens and your rebase process stopped,
come back to the place where you started with git rebase --abort
.