Back
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?
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 $2git commit --amend -m "squashed history"# Then remember the new commit sha1TARGET=`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 commitgit rebase --onto $TARGET $2
#!/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