Skip to content

Instantly share code, notes, and snippets.

@chamatt
Created December 29, 2022 06:07
Show Gist options
  • Save chamatt/295a0adb711592093f3741c77928c3e0 to your computer and use it in GitHub Desktop.
Save chamatt/295a0adb711592093f3741c77928c3e0 to your computer and use it in GitHub Desktop.
Ammend Old Commits with file changes
git-amend-old() (
# Stash, apply to past commit, and rebase the current branch on to
of the result.
# For Gerrit.
https://stackoverflow.com/questions/1186535/how-to-modify-a-specified-commit/53$
current_branch="$(git rev-parse --abbrev-ref HEAD)"
apply_to="$1"
git stash
git checkout "$apply_to"
git stash apply
git add -u
git commit --amend --no-edit
new_sha="$(git log --format="%H" -n 1)"
git checkout "$current_branch"
git rebase --onto "$new_sha" "$apply_to"
)
export -f git-amend-old

Usage:

  1. Modify the file with the change you want
  2. Call git-amend-old <sha>, replacing <sha> with the commit sha you want the change to apply to
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment