]> Tony Duckles's Git Repositories (git.nynim.org) - dotfiles.git/blob - bin/git-amend
.screenrc: Enable auto-logging
[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 # stash off work tree modifications leaving the
27 # index for amending to the target commit
28 git stash save -q --keep-index git-amend
29
30 # always restore from stash before exiting
31 trap 'git stash pop -q stash@{git-amend} 2>/dev/null' EXIT
32
33 # go back in history
34 git checkout -q "$TARGET" || {
35 echo "$(basename $0): changes didn't apply cleanly" 1>&2
36 exit 1
37 }
38
39 # amend the commit. this opens your editor
40 git commit -v --amend
41
42 # apply the remaining commits on this branch
43 git rebase --onto HEAD "$TARGET" "$BRANCH"