Skip to content

Instantly share code, notes, and snippets.

@mislav
Forked from nachof/git-amend.sh
Created January 16, 2010 13:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mislav/278825 to your computer and use it in GitHub Desktop.
Save mislav/278825 to your computer and use it in GitHub Desktop.
git amend command
#!/usr/bin/env bash
git status >/dev/null
if [ $? -ne 0 ]; then
echo "Nothing to commit. Use git add first"
exit 1
fi
TARGET=$1
BRANCH=$(git name-rev HEAD | cut -d' ' -f2)
if [ -z $TARGET ]; then
echo "Aborted: you must specify the target commit"
exit 1
fi
# go back in history
git checkout -q $TARGET
if [ $? -ne 0 ]; then
echo "Aborted: your changes didn't apply cleanly"
exit 1
fi
# amend the commit. this opens your editor
git commit -v --amend
# check if working copy is still dirty
git status -a >/dev/null
dirty=$?
# stash changes if so
[ $dirty -eq 0 ] && git stash save -q
# apply the remaining commits on this branch
git rebase --onto HEAD $TARGET $BRANCH && [ $dirty -eq 0 ] && git stash pop -q
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment