Search code examples
gitrebase

git rebase - add original commit hash to commit messages


Is there any way to do the same thing as cherry-pick -x (add the hash of the original commit to the message of the copied commit) in a rebase?

I can work around it currently by replacing the following

git checkout other-branch
git rebase master
git checkout master
git merge other-branch

with

git checkout master
....
git cherry-pick -x other-branch^^^^
git cherry-pick -x other-branch^^^
git cherry-pick -x other-branch^^
git cherry-pick -x other-branch^
git cherry-pick -x other-branch

Solution

  • It ain't pretty but it get's the job done;

    git rebase --exec='git log --pretty="format:%B" -n 1 > tmp;
        grep -o "\w\{40\}" .git/rebase-merge/done | tail -n 1 >> tmp; 
        git commit --amend -F tmp; 
        rm tmp;' master
    

    To explain each part of the --exec script;

    • Put the commit message we just completed into tmp;
    • Grab the hash of the commit we just rebased from .git/rebase-merge/done and append it to tmp.
    • Ammend the commit we just made using tmp as the commit message.
    • Remove the tmp file.

    I'm sure you can hack that into a format you'll find pleasing.

    Log of original work;

    commit 1ebdfc2fd26b0eed9f131197dc3274f6d5048e97
    Author: Adam
    Date:   Thu Jan 24 16:33:09 2019 +0000
    
        Content C
    
    commit 632f1a4e1ab5d47c9e4c3ca3abd02a207a5dda09
    Author: Adam
    Date:   Thu Jan 24 16:33:06 2019 +0000
    
        Content B
    
    commit a7e0d1eb2e412ec51865ccd405ea513c7677c150
    Author: Adam
    Date:   Thu Jan 24 16:33:04 2019 +0000
    
        Content A
    

    Log of rebased work;

    commit 79d7ece06185b21631248a13416e5ca5c23e55b2
    Author: Adam
    Date:   Thu Jan 24 16:33:09 2019 +0000
    
        Content C
        1ebdfc2fd26b0eed9f131197dc3274f6d5048e97
    
    commit d2fe6267165fa05f5fe489a6321b0b1742d1a74c
    Author: Adam
    Date:   Thu Jan 24 16:33:06 2019 +0000
    
        Content B
        632f1a4e1ab5d47c9e4c3ca3abd02a207a5dda09
    
    commit da72fab2008e74f6a8e247f93619943805ebf86e
    Author: Adam
    Date:   Thu Jan 24 16:33:04 2019 +0000
    
        Content A
        a7e0d1eb2e412ec51865ccd405ea513c7677c150