Last Updated: February 25, 2016
·
1.921K
· rane

Git alias for fixing/amending a commit in the history

  1. Stage the changes you want to have amended to the earlier commit
  2. $ git fix <revision> (e.g. git fix HEAD~4 or git fix bbfba98)

Sometimes you want to amend a commit that isn't the immediate parent of the current HEAD. This alias makes it easier.

[alias]
    fix = "!_() { c=$(git rev-parse $1) && git commit --fixup $c && if grep -qv \"No local changes\" <<<$(git stash); then s=1; fi; git -c core.editor=cat rebase -i --autosquash $c~; if [[ -n "$s" ]]; then git stash pop; fi; }; _"

Gist: https://gist.github.com/raine/5636366

2 Responses
Add your response

I didn't knew till date that I can go back and amend commits I thought I'm allowed to amend only last commit using git commit --amend! thanks for sharing.

over 1 year ago ·

Great use of --fixup and --autosquash options!

However, it will fail on Shells which don't have the <<< redirection operator (such as dash).
It is possible to detect if stash is required using diff-index instead of grep:

fix = "!_() { c=$(git rev-parse $1) && git commit --fixup $c && git diff-index --quiet HEAD; s=$?; [ $s != 0 ] && git stash; git -c core.editor=cat rebase -i --autosquash $c~; [ $s != 0 ] && git stash pop; }; _"

Thanks, your alias is really useful.

over 1 year ago ·