Skip to content

Instantly share code, notes, and snippets.

@raine
Last active December 17, 2015 16:09
Show Gist options
  • Save raine/5636366 to your computer and use it in GitHub Desktop.
Save raine/5636366 to your computer and use it in GitHub Desktop.
A git alias for fixing (or "amending") an earlier 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`)
[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; }; _"
@jcarsique
Copy link

You should avoid use of <<< redirection which is not available on dash and dash is called by default by Git ! on many systems.

Here's an alternative:

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; }; _"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment