3. Squashing several commits into one

Create a branch as example with multiple commits

git checkout -b squash
for c in H e l l o , ' ' w o r l d; do
    echo "$c" >>squash.txt
    git add squash.txt
    git commit -m"Add '$c' to squash.txt"
done

How to squash commits

To squash multiple commits into one in the branch you’re on, do the following:

  1. git log --oneline to check how much coomits you need to squash

  2. Run git rebase -i HEAD~4 (with 4 being the number of commits) or Run git rebase -i [SHA] (where [SHA] is the commit after the last one you want to squash. *)

  3. You should see a list of commits, each commit starting with the word “pick”.

  4. Make sure the topmost, first commit says “pick” and change the rest below from “pick” to “squash”. This will squash each commit into the previous commit, which will continue until every commit is squashed into the first commit.

  5. Save and close the editor.

EXAMPLE

git log --oneline
Choose the next commit after the one you want to be squashed
git rebase -i f9781b5

https://www.scraggo.com/how-to-squash-commits/

Last updated

Was this helpful?