How can I squash a range of git commits together given starting and ending SHA's

Michael Durrant picture Michael Durrant · Nov 24, 2015 · Viewed 10.9k times · Source

I have a branch with about 20 commits.

The first SHA on the branch is bc3c488...
The last SHA on the branch is 2c2be6...

How can I merge all the commits together?

I want to do this without using interactive rebase as there are so many commits.

I need this for a github Pull Request where I am being asked to merge my commits.

Need to do this without doing a git merge --squash as I need to squash locally and another developer does the merge and wants me to do the squash first before merging.

Answer

IgnazioC picture IgnazioC · Aug 24, 2018

If the first SHA is HEAD you can also use this approach:

git reset --soft $OLD_SHA; git add -A; git commit --amend --no-edit

be careful, this command will change the history of the repo.

If you want to squash commits that are in the middle of your history:

|---* --- 0 --- 1 ---- 2 --- 3 --- * --- * --- * --- HEAD

like in this case the commits 1, 2 and 3

I would really recommend to use rebase -i