Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.4k 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 (27.5k points)

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

#!/bin/sh

# 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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...