0 votes
1 view
in DevOps and Agile by (20k points)

With git rebase --interactive <commit> you can squash any number of commits together into a single one.

That's all great unless you want to squash commits into the initial commit. That seems impossible to do.

Are there any ways to achieve it?

1 Answer

0 votes
by (28.4k points)

You give it two commit SHA1s and it will squash everything between them into one commit named "squashed history":


# Go back to the last commit that we want to form the initial commit (detach HEAD)

git checkout $2

# Reset the branch pointer to the initial commit (= $1), but leaving the index and working tree intact.

git reset --soft $1

# Amend the initial tree using the tree from $2

git commit --amend -m "squashed history"

# Then remember the new commit sha1

TARGET=`git rev-list HEAD --max-count=1`

# Go back to the original branch (assume master for this example)

git checkout master

# Replay all the commits after $2 onto the new initial commit

git rebase --onto $TARGET $2

Welcome to Intellipaat Community. Get your technical queries answered by top developers !