2

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

LWang
  • 95
  • 1
  • 10
  • 1
    You must have done something wrong. The usual solution to this problem is to use `git rebase -i`. Mark commits d37d and e0f4 with "fixup" (or "squash" if you want to salvage their commit messages) instead of "pick". – j6t Jan 28 '21 at 07:48
  • @j6t You must be right. I redo it with my coworker with ```git rebase - i master``` rather than ```HEAD~5```, and that did not squash all commits into one. I was not working on master. The result will be merged into master after cleanup. – LWang Jan 28 '21 at 16:00
  • j6t is right. 'fix' is what I should have used. After that, just push force to the branch. – LWang Apr 15 '21 at 04:03

1 Answers1

0

@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"**
Sohail
  • 303
  • 1
  • 10