DEV Community

Cover image for Bash Shortcut for Git Squash
Ayan Banerjee
Ayan Banerjee

Posted on • Edited on

2 2

Bash Shortcut for Git Squash

Cover photo by Moritz Kindler on Unsplash

I often find myself squashing a lot of unnecessary commits. The usual way to do this is by using rebase or soft reset. I personally prefer to use the soft reset method.

# soft reset some commits
git reset --soft HEAD~2
git commit -m "Commit message"
Enter fullscreen mode Exit fullscreen mode

As you can see the process is rather long. So how we can save some precious seconds ... enter bash functions!

Add this function to your .bashrc file:

function sq {
    if [ -z "$1" ]
    then
        echo "First argument required"
        return 1
    fi
    if [ -z "$2" ]
    then
        message="$(git log --pretty=format:"%B" -1)"
    else
        message=$2
    fi
    git reset --soft HEAD~$1
    git commit -m "$message"
}
Enter fullscreen mode Exit fullscreen mode

And then run source ~/.bashrc.

Usage

If you want to squash 2 commits with the commit message Add tests, run this: sq 2 "Add tests".

More often than not we want the commit message of the squashed commit to be the same as the recent most commit message. In those cases, simply run sq 2 if you want to squash 2 commits.

Sometimes we need to squash all the commits that we made after branching off from a branch. For that, add the following function to your .bashrc file:

function cnt {
    compare_branch=${1-master}
    git rev-list --count HEAD ^$compare_branch
}
Enter fullscreen mode Exit fullscreen mode

This gives the number of commits made after branching off. Try running cnt for the number of commits made after branching off from master and cnt my_branch for the number of commits made after branching off from my_branch. Don't forget to run source ~/.bashrc after modifying .bashrc!

Now simply run sq "$(cnt my_branch)" to squash the commits made after branching off from my_branch with the recent-most commit message.

I hope that you have learned something useful from this post and save some seconds of your development time! Let me know if something is not clear.

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

Image of PulumiUP 2025

Let's talk about the current state of cloud and IaC, platform engineering, and security.

Dive into the stories and experiences of innovators and experts, from Startup Founders to Industry Leaders at PulumiUP 2025.

Register Now

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay