How do I squash two non-consecutive commits?

Viewed 45971

I'm a bit new to the whole rebasing feature within git. Let's say that I made the following commits:

A -> B -> C -> D

Afterwards, I realize that D contains a fix which depends on some new code added in A, and that these commits belong together. How do I squash A & D together and leave B & C alone?

5 Answers

Interactive rebase works well until you have big feature branch with 20-30 commits and/or couple of merges from master or/and fixing conflicts while you was commiting in your branch. Even with finding my commits through history and replacing pick with squash doesn't worked here. So i was looking for another way and found this article. I did my changes to work this on separate branch:

git checkout master
git fetch
git pull
git merge branch-name
git reset origin/master
git branch -D branch-name
git checkout -b branch-name
git add --all
#Do some commit
git push -f --set-upstream origin branch-name

Before this I got my pull request with about ~30 commits with 2-3 merges from master + fixing conflicts. And after this I got clear PR with one commit.

P.S. here is bash script to do this steps in automode.

Related