]> Tony Duckles's Git Repositories (git.nynim.org) - dotfiles.git/blob - bin/git-amend
.vimrc: Use vim-pandoc for syntax=markdown
[dotfiles.git] / bin / git-amend
1 #!/bin/sh -e
2 # Usage: git-amend <commit>
3 # Amend changes staged in the index to <commit>, or edit commit message if
4 # no changes are currently staged. Modifications not staged are stashed and
5 # then reapplied once the amend and rebase operations are complete.
6 #
7 # This command rewrites history. Do not run it after <commit> or its
8 # decendants have been published to the world.
9 #
10 # This version in POSIX sh by Ryan Tomayko <tomayko.com/about>
11 #
12 # Based on Mislav's bash version here:
13 # http://gist.github.com/278825
14
15 # NOTE removed check for staged files, since sometimes I just
16 # want to change the commit message.
17
18 TARGET="$1"
19 BRANCH=$(git name-rev HEAD | cut -d' ' -f2)
20
21 test -z "$TARGET" && {
22 echo "$(basename $0): you must specify the target commit" 1>&2
23 exit 1
24 }
25
26 # if there are local work tree modifications, stash those off leaving
27 # the index for amending to the target commit
28 DIRTY=0
29 if ! git diff-files --quiet --ignore-submodules --; then
30 DIRTY=1
31 fi
32 test $DIRTY -eq 1 && git stash save -q --keep-index git-amend
33 # always restore from stash before exiting
34 test $DIRTY -eq 1 && trap 'git stash pop -q stash@{0} 2>/dev/null' EXIT
35
36 # go back in history
37 git checkout -q "$TARGET" || {
38 echo "$(basename $0): changes didn't apply cleanly" 1>&2
39 exit 1
40 }
41
42 # amend the commit. this opens your editor
43 git commit -v --amend
44
45 # apply the remaining commits on this branch
46 git rebase --onto HEAD "$TARGET" "$BRANCH"