Skip to content

Instantly share code, notes, and snippets.

@skylarbpayne
Created July 24, 2021 20:16
Show Gist options
  • Save skylarbpayne/cb809d7b4ccef88b1dc86f6e757445d3 to your computer and use it in GitHub Desktop.
Save skylarbpayne/cb809d7b4ccef88b1dc86f6e757445d3 to your computer and use it in GitHub Desktop.
A git alias for smart reversions
# 'Smart Revert'
# Uses git reset to perform a revert such that:
# - Git history stays intact
# - Works across merge commits
# See this stack overflow answer for information on the solution:
# https://stackoverflow.com/a/1470452
# This takes a single argument: a git commit. Note: this can be either
# an exact commit, or some expression such as 'HEAD^'
[alias]
sr = "!f() { \
COMMIT=$(git log -1 --format='format:%H' $1); \
ORIG_COMMIT=$(git log -1 --format='format:%H'); \
git reset --hard ${COMMIT}; \
git reset --soft ${ORIG_COMMIT}; \
git add .; \
git commit -am 'Reverting changes between '${ORIG_COMMIT}' and '${COMMIT}''; \
}; f"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment