Skip to content

Instantly share code, notes, and snippets.

@mjambon
Last active August 26, 2020 17:58
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 mjambon/119b04bf2df4d37609c80aeb93a1100f to your computer and use it in GitHub Desktop.
Save mjambon/119b04bf2df4d37609c80aeb93a1100f to your computer and use it in GitHub Desktop.
Delete a commit from a git branch
#! /usr/bin/env bash
#
# Remove a single git commit from history.
#
set -eu
usage() {
cat <<EOF
Usage: $0 <commit ID>
Remove the specified git commit, rewriting the history of the branch from
that point.
It is recommended you try this on a new branch first e.g.
git checkout -b practice
git-delete 6b2484a
# then check the code you wanted gone
EOF
}
if [[ $# != 1 ]]; then
usage
exit 1
fi
commit=$1
set -x
git rebase --rebase-merges --onto "$commit"^ "$commit"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment