Last active
October 30, 2020 12:47
-
-
Save maxlath/03cf6e6dcb525ff84f6c07b6ff58ed8e to your computer and use it in GitHub Desktop.
Keep a list of the hashes by which a commit was previously known
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is especially useful when some commits refers to some other commits by hash, and that hash would have otherwise been lost during the rebase: that commit can now by found using
git log --grep