How to combine multiple historic commits into one while not touching the recent ones?

Viewed 58

I want to combine multiple historic commits together, while not touching the recent ones, as shown in the picture, I want to only combine the 2-4 commits together.

After reading relevant answers and posts, I have tried git rebase - i HEAD~5 and fix and rename commits in vim, but I found this would combine them into one commit. Is there a way that I can combine the middle commits, while leaving the more recent commits untouched?

git

1 Answers

@LWang, No. You cannot combine the middle commits, while leaving the more recent commits untouched.

However, If you have to do it, No other choice, then , you can reset to the commit you need the changes from and start committing afresh. I would recommend this only if there are few(one or two) commits that are to be preserved and you very well know what changes go into what commits.

In your example, you can follow the below steps;

**git reset -- mixed 66b8e0b**  (Your changes will be preserved)
**git add <filename>**  (add only those files which are required for the middle commit)
**git commit <commit message>** (Enter the commit message for the middle commit)
**git add <filename>** (add all the pending files)
**git commit -m "Wireup survey flag"**
Related