Git alias for fixing/amending a commit in the history
- Stage the changes you want to have amended to the earlier commit
-
$ git fix <revision>
(e.g.git fix HEAD~4
orgit 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; }; _"
Written by Raine Virta
Related protips
2 Responses
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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Git
Authors
khasinski
591.1K
dmichaelavila
500.4K
Related Tags
#git
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#