1
0

git-amend-last 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env bash
  2. # This essentially makes the current commit empty so you can see the full diff
  3. # of the commit to be amended.
  4. #
  5. # This allows something similar to "Amend Last Commit" of git gui.
  6. set -euo pipefail
  7. old_commit=$(git rev-parse HEAD)
  8. git-fmt() {
  9. git show -s --format="$1"
  10. }
  11. title=$(git-fmt %s)
  12. message=$(git-fmt %B)
  13. GIT_AUTHOR_NAME=$(git-fmt %an)
  14. GIT_AUTHOR_EMAIL=$(git-fmt %ae)
  15. GIT_AUTHOR_DATE=$(git-fmt %aD)
  16. GIT_COMMITTER_NAME=$(git-fmt %cn)
  17. GIT_COMMITTER_EMAIL=$(git-fmt %ce)
  18. GIT_COMMITTER_DATE=$(git-fmt %cD)
  19. export \
  20. GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE \
  21. GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE
  22. git reset --soft HEAD~1
  23. new_commit=$(git commit-tree -p HEAD 'HEAD^{tree}' -m "$message")
  24. git update-ref -m "amend-last: $title" HEAD "$new_commit"
  25. # If we're rebasing, git stores the current commit in rebase-merge/amend to
  26. # know whether --continue should result in an amend. If we don't update this,
  27. # --continue will complain that we have unstaged changes instead.
  28. if git rev-parse -q --verify rebase-merge/amend; then
  29. git update-ref rebase-merge/amend "$new_commit" "$old_commit"
  30. fi