Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git rebase - add original commit hash to commit messages

Tags:

git

rebase

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
like image 826
Alex028502 Avatar asked Jan 14 '19 16:01

Alex028502


People also ask

Does rebase change commit ID?

Note that the commits modified with a rebase command have a different ID than either of the original commits. Commits marked with pick will have a new ID if the previous commits have been rewritten.

How do I rebase a commit message?

On the command line, navigate to the repository that contains the commit you want to amend. Use the git rebase -i HEAD~n command to display a list of the last n commits in your default text editor. Replace pick with reword before each commit message you want to change.

Does changing commit message change hash?

If you amend the commit message, or the files in a commit, this will change the git hash.


Video Answer


1 Answers

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
like image 129
Adam Avatar answered Oct 22 '22 17:10

Adam