gitgithooksgit-rebasegit-amend

Why is git autosquash not working as expected?


Update

My company adds a prefix to the commit history which i had gotten so used to, I didn't see it but of course git would, and that was causing the issue. Removing that prefix fixed my issue.

Example:

kth-JAN-1614-ui-nits fixup! kth-JAN-1614-ui-nits Refactor and small improvements [Karthik Thirugnanam]

The hook adds that kth-JAN-1614-ui-nits prefix to the commit summary.

It works again if I disable hooks:

git commit --no-verify --fixup

Original

I am trying to get the fixup/autosquash flow to work but it doesnt seem to work for me, though it seems to have been introduced ages ago..

My repo log

* 4f9429d8 Tue Jun 24 16:13:44 2025 (12 minutes ago )  (HEAD -> kth-JAN-1614-ui-nits) kth-JAN-1614-ui-nits fixup! kth-JAN-1614-ui-nits Refactor and small improvements [Karthik Thirugnanam]
* b9f50337 Tue Jun 24 15:57:02 2025 (29 minutes ago )  (origin/kth-JAN-1614-ui-nits) kth-JAN-1614-ui-nits Log SQL statement in Marten logger [Karthik Thirugnanam]
* 6f376eca Tue Jun 24 15:52:55 2025 (33 minutes ago )  kth-JAN-1614-ui-nits UI Nit fixes for release pipelines [Karthik Thirugnanam]
* c071b3c2 Tue Jun 24 15:50:56 2025 (35 minutes ago )  kth-JAN-1614-ui-nits Refactor and small improvements [Karthik Thirugnanam]
*
> git -v
git version 2.39.1.windows.1

Rebase invocation

> git rebase -i --autosquash origin/master

Rebase

pick c071b3c2 kth-JAN-1614-ui-nits Refactor and small improvements
pick 6f376eca kth-JAN-1614-ui-nits UI Nit fixes for release pipelines
pick b9f50337 kth-JAN-1614-ui-nits Log SQL statement in Marten logger
pick 4f9429d8 kth-JAN-1614-ui-nits fixup! kth-JAN-1614-ui-nits Refactor and small improvements

From what I see, the last commit should be reordered and marked as fixup?


Solution

  • The issue seems to be that the pre-commit hook adds the branch name as the subject prefix for some reason. Something like this:

    # .git/hooks/commit-msg
    set -u
    msg="$1"
    (
        git branch --show-current | tr '\n' ' '
        cat "$msg"
    ) | sponge "$msg"
    

    Here I use the commit-msg hook which is the hook designed for that purpose.

    You encounter the problem when you run e.g. git commit --fixup=@. Because you get this:

    JIRA-OFFICIAL-65498798745-fix-issue fixup! Perfect solution
    

    Now you can’t autosquash because the fixup! won’t be detected.

    You can detect this case in the script and stop the prefixing for these automation commits.

    #!/usr/bin/env bash
    set -u
    msg="$1"
    subject=$(head -1 "$msg")
    subject_start="${subject:0:7}"
    # Ignore if this is an amend commit
    if test "$subject_start" = 'fixup! ' \
            || test "$subject_start" = 'amend! ' \
            || test "$subject_start" = 'squash!' \
            || test "$subject_start" = 'reword!'
    then
        exit 0
    fi
    (
        git branch --show-current | tr '\n' ' '
        cat "$msg"
    ) | sponge "$msg"
    
    

    Or with regular shell:

    #!/bin/sh
    set -u
    msg="$1"
    subject=$(mktemp)
    head -1 "$msg" >"$subject"
    # Ignore if this is an amend commit
    if git grep --quiet --no-index \
           -e '^fixup!' -e '^squash!' \
           -e '^amend!' -e '^reword!' -- "$subject"
    then
        exit 0
    fi
    (
        git branch --show-current | tr '\n' ' '
        cat "$msg"
    ) | sponge "$msg"