Skip to content

Instantly share code, notes, and snippets.

@zach-klippenstein
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zach-klippenstein/a9644cd07b184e42a904 to your computer and use it in GitHub Desktop.
Save zach-klippenstein/a9644cd07b184e42a904 to your computer and use it in GitHub Desktop.
Shell script for amending commits on a feature branch.
#!/bin/zsh
if [ "$1" = '-h' ]; then
cat <<-EOF
Usage: git-edit <branch> [args-to-commit]
e.g. If your tree looks like this:
* cc3cd23 (HEAD -> feat1) Feature 1.2
* 53f56c1 Feature 1
* 08355e5 (master) Initial commit.
And you want to change something from the commit "Feature 1",
$ git checkout 53f56c1
# Make your edits.
$ git-edit feat1 --no-edit
You'll end up on the newly-amended version of 53f56c1, with the rest
of branch feat1 on top of you.
EOF
exit 0
fi
# Exit when any command returns a non-zero exit code.
set -e
[ -z "$1" ] && (echo "No branch specified. Run with -h."; false)
original_commit=$(git rev-parse HEAD)
current_branch="$1"
shift
git commit --amend "$@"
new_commit=$(git rev-parse HEAD)
git rebase --onto $new_commit $original_commit "$current_branch"
git checkout --quiet $new_commit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment