DEV Community

Cover image for Git - Squash
kulsoomzahra for eduAlgo

Posted on

Git - Squash

Using git/github for your personal projects is pretty easy but when you're a part of an organisation or a large scale project with thousands of users you might get a tough time. One wrong move can cause mess or make the commit tree dirty or anything you don't want which in turn may fuel your Impostor Syndrome!
So hey, HANG ON! You can do it.

A Clean Commit Tree

Squashing your commits can help so here we go

  1. Switch to the branch where the commit tree is
    git checkout <branch name>

  2. Make sure everything on your local is pushed to the branch
    git add .
    git commit -m "Commit message"
    git push origin <branch name>

  3. Squash n previous commits
    (n is the no.of commits you want to squash)
    git rebase -i HEAD~3

Alt Text

Now, change pick to s or squash in the last 2 commits
a) Ctrl + O to write out
b) Enter to save changes
c) Ctrl + X to exit

Now you can edit the final commit message .
Repeat a,b,c

Don't forget to PUSH your changes on to the branch
git push origin <branch name>

If this doesn't work force push your changes
git push origin <branch name> -f

You're Done! Now you have a clean commit tree.

Top comments (2)

abhijit2505 profile image
Abhijit Tripathy

Amazing 🥳

kulsoomzahra profile image

Thanks :)