Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

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

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

question from:https://stackoverflow.com/questions/65932545/how-to-combine-multiple-historic-commits-into-one-while-not-touching-the-recent

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

@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"**

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...