Last active
November 18, 2021 07:31
-
-
Save shotasenga/ac0907b39fcdeb0e99781b1ef939fff4 to your computer and use it in GitHub Desktop.
git-sort (1)
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
#!/bin/bash | |
# | |
# Sort commit hash in topological order (oldest -> newest) | |
# *Unknown commit hash will be silently ignored | |
# | |
# This can be used to cherry-pick commits in appropriate order | |
# $ git cherry-pick `git sort hash-1 hash-2 ... hash-n` | |
# $ git cherry-pick `cat /path/to/commit-hash/file | git sort` | |
# create temporary file descriptor (see: https://unix.stackexchange.com/a/181938/501792) | |
tmpfile=$(mktemp /tmp/git-sort.XXXXXX) | |
exec 3>"$tmpfile" | |
exec 4<"$tmpfile" | |
rm $tmpfile | |
cat /dev/stdin >&3 | |
for ((i=1; i<=$#; i++)) | |
do | |
echo "${!i}" >&3 | |
done | |
# rev-list | |
git rev-list --all --topo-order --reverse | grep -f <(cat <&4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The order of commit hash matters when you
cherry-pick
multiple commits.Install
git-sort
and move it somewhere in your $PATHchmod +x /path/to/git-sort
Example
Sort commit hashes
Sort commit hashes in a file
with cherry-pick