Git Autosquash – Enabling Local Organizing And Amending Commits Before Permanently Sharing To A Remote Repo

One of Git’s strengths is enabling local organizing and amending commits before permanently sharing to a remote repo. This allows us to no longer suffer with “Forgot to include this file in that commit” commits! We can manually update – reorganize, combine (squash), split – them before pushing, resulting in well-organized and clear commit history for future commit archeologists!

Git has a useful feature named “autosquash” to help automate commit squashing. It operates on commits with messages beginning with “fixup! “, “squash! “, or “amend! ” (note the space after the !).

To Use…

To use, start a commit message with one of those keywords followed by either the commit hash or the starting text of an existing Git commit message to combine/squash with. Then, use Git’s interactive rebase with the “autosquash” argument:

git rebase --i --autosquash
Git will automatically reorder the fixup! commits to after the matching commit and mark it to squash.

Let’s step through an example.

1. Create a first commit:

git commit -m "Fix issue 1234"

2. Additional commits occur.

3. Realizing you missed committing a file with the first commit, commit it now and prefix the message with fixup! and the first commit’s message:

git commit -m "fixup! Fix issue 1234"

4. Issue the Git interactive rebase command with the “autosquash” argument:

git rebase -i --autosquash

5. Git displays the rebase todo list with the “fixup!” commit placed next to the first commit and marked for squashing, saving the manual “move and mark to squash” steps. Without “fixup!” as the commit’s first message word, Git lists the commits in commit order.

6. Proceed as normal to complete the commit.

This feature is particularly helpful with multiple fixup! commits as Git will place them all in the correct location.

Set “rebase.autoSquash” to true to always enable this behavior and omit specifying “–autosquash”:

git config --global rebase.autoSquash true

About Intertech

Founded in 1991, Intertech delivers software development consulting to Fortune 500, Government, and Leading Technology institutions, along with real-world based corporate education services. Whether you are a company looking to partner with a team of technology leaders who provide solutions, mentor staff and add true business value, or a developer interested in working for a company that invests in its employees, we’d like to meet you. Learn more about us.