Git: Squash all commits in feature branch


Sometimes there's an old and super messy feature branch that needs to be merged back into the main branch. If the branch has been appropriately tended to, regularly rebased, commits squashed and tidied up, then that is usually a very simple process.

If however that is not the case, and the branch is a total mess of nonsensical commits, merge commits, and lots of conflicts, then it's less simple...

Found a simple, although slightly sketchy, workaround: Do a soft reset to throw away all the commits, while keeping the changes, and create a new commit.

# Checkout the feature branch
git checkout feature/foobar

# Do a soft reset to main, throwing away commits,
# while keeping changes in working dir
git reset --soft main

# Create a new commit
git add .
git commit -m "Foobar changes in a single commit"

Before actually merging this new commit back into main, it's probably a good idea to create a PR and look through the diff properly...