DEV Community

Bhanu Reddy
Bhanu Reddy

Posted on

Git Squash! The terminal way

It is the Friday evening when I was about to close my work I received a message from my boss to revert a commit done by other engineer since it is not satisfying the end to end test cases ran over the day. I have reverted the commit but hell broke loose when it went for preRelease many tests are failing and Release manager has called me on Saturday since I did the last commit. I have spent an hour only to find that there are multiple commits which I had to revert not a single commit, only if squash has be used I would have had a pleasant Saturday.

What is squashing?

Squashing is the process of combining multiple commits into new single commit. This is in general done to clean up commit history before merging the PR into the main branch. If the commits to be squashed are present in both remote and local after the squash a force push is required since history is altered. Pay little attention while performing force push on branches you are working with others since you might override their commits.

Ways to git squash

  1. Auto Squash
    git commit --squash=<commit> # to mark the commit as to be squashed
    git commit --fixup= # to do the same squashing

    Follow below steps:

    • git commit -m "first commit"
    • git commit -m "second commit" --squash=6ca34bfbd3cd2c6f06ef2f2d156e481f6ab7c9f1
    • git rebase --autosquash

    BEST FOR:

    • Automates squashing work flow by marking the commits as squash
    • Saves time by efficiently picking the commits to be squashed
  2. Branch Merge
    git merge --squash {branch}
    A common strategy followed while merging a feature branch on to main branch.

    BEST FOR:
    • Squashing an entire branch into a single commit when merging.
    • When you want to merge a feature branch but don’t want to keep the detailed commit history.

  3. Fine Grained Control
    git rebase -i HEAD~{N} # N is number of commits from HEAD you need to squash.

    Shows a list of commits in terminal change the commits wording from pick to squash after saving and closing the editor adjust the commit message for a new single commit.

    git rebase -i {commit-hash}
    When there are long series of commits and you were not aware from how many commits before head you need to squash.

    BEST FOR:
    • Cleaning up a series of commits before merging a feature branch.
    • When you need fine-grained control over the commit history.

  4. Quick Fix
    git reset --soft HEAD~n

    When several commits are made to local without pushing to remote this way quickly squashes into single commit.

    BEST FOR:
    • Quick and simple squashing of the most recent commits which are not yet pushed to remote
    • When you’re not concerned about losing the original commit messages.

Each dev has their own preferred way of squashing commits based on the needs and they all contribute to maintaining a clean and readable commit history. Some IDE's like IDEA also offers to squash commits from UI but learning how to do it from terminal always gives you an edge. You can create you own work flow for example i wrote functions to add all to staging area and commit, similarly to rebase from first commit of the current branch.

Top comments (0)