-
-
Save mislav/278825 to your computer and use it in GitHub Desktop.
git amend command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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