#!/usr/bin/env sh # A script to keep a list of the hashes by which a commit was previously known # Adapted from https://stackoverflow.com/a/54352290/3324977 # Installation: # - copy this script to a directory in your $PATH # - make it executable: # chmod +x ./update_commit_previous_hashes_list # - it should now be accessible from any directory, which can be checked like this: # cd some/other/directory # which update_commit_previous_hashes_list # Usage: # - rebase on a branch named 'some-branch' # git rebase some-branch --exec update_commit_previous_hashes_list # - same but in interactive mode: you should see an exec command between every pick # git rebase some-branch --interactive --exec update_commit_previous_hashes_list # Get the message of the commit we just rebased git log --pretty="format:%B" -n 1 > _last_rebased_commit_message metadata_header="This commit was previously known as:" # If the header is already there, there is already a list of previous commit hashes, # no need to re-add that header grep "$metadata_header" _last_rebased_commit_message > /dev/null || { echo "\n$metadata_header" >> _last_rebased_commit_message } # Get the hash of the commit we just rebased grep --only-matching "\w\{40\}" .git/rebase-merge/done | tail -n 1 >> _last_rebased_commit_message # Update the commit message git commit --amend --file _last_rebased_commit_message # Cleanup rm _last_rebased_commit_message