14

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
5
  • Doesn’t git rebase master -x work? Jan 14, 2019 at 19:15
  • no, that's to execute a script after each commit, like to run the tests after each commit. would be great if it did.
    – Alex028502
    Jan 14, 2019 at 19:18
  • On a separate note, why do you want this? Jan 14, 2019 at 19:25
  • because if you make a mistake when you are resolving conflicts, it's good to be able to look at the original version sometimes, at least for the next few hours or days.
    – Alex028502
    Jan 17, 2019 at 15:43
  • 2
    You can do that without this. Just make a branch before you rebase. Or check the reflog. Jan 17, 2019 at 15:56

4 Answers 4

9
+50

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
6
  • 1
    nice! I turned it into an executable to be able to write it like this: git rebase some-branch --exec update_commit_previous_hashes_list gist.github.com/maxlath/03cf6e6dcb525ff84f6c07b6ff58ed8e
    – maxlath
    Oct 28, 2020 at 11:04
  • I've been using this, and, unfortunately, this might fubar the last commit. When it is run twice. .git/rebase-merge/done won't contain the right informations because the last commit is already rebased and there is nothing to do, but the last commit will still be amended.
    – deadalnix
    Jul 5, 2021 at 13:37
  • @deadalnix so if you run this twice, targeting branchA then branchB, the final commit will be fubar?
    – Adam
    Jul 6, 2021 at 10:22
  • So I investigated a bit more, and I misdescribed the problem, even though there is one. The problem happen when there is a commit that is skipped over during the rebase, for instance because the content of that commit already is in, then the amend does amend the previous commit with the info for the dropped commit.
    – deadalnix
    Jul 7, 2021 at 11:48
  • This happens when you rebase the same code twice, which ends up fubaring one commit. i ended up using filter-branch to remove all the commit that would be dropped, and then rebase. idk if that solution would be suitable on you end.
    – deadalnix
    Jul 7, 2021 at 11:49
6

So, here is my solution:

git rebase <branch> \
-ix "git rev-parse --short HEAD > tmp &&  \
echo 'from: $<branch_shortid>' > tmp && \
git commit --amend -F tmp"

Make sure that branch_shortid is correct and was generated on the branch you wish to rebase content from.

Disclaimer: I am not sure if this will work in all cases, esspecially if you have some strange or complicated referencing systems going on. I ran this on a very simple git repo generated by:

$ git init 
$ echo "a" > a.txt && git add . && git commit -m "first commit"
$ git checkout -b "feature1" 
$ echo "b" > b.txt && git add . && git commit -m "second commit"
$ echo "c" > c.txt && git add . && git commit -m "third commit"
$ feature1id=$(git rev-parse --short HEAD)
$ git checkout master
$ git rebase feature1 \
  -ix "git rev-parse --short HEAD > tmp &&  \
  echo 'from: $feature1_id' > tmp && \
  git commit --amend -F tmp"

Here is corresponding output:

enter image description here


Discussion:

As pointed out earlier, I think that you using git reflog is a better solution for investigating from which commit on which branch was merged onto the desired one.

The point of rebasing is to apply commits onto the top of another branch, as if that were commit structure in the first place:

Rebasing produces a linear history.

7
  • i.stack.imgur.com/8efe9.png This just gives the original commit id of the old head right? It won't put each commit's original id from where it is being applied.
    – Alex028502
    Jan 22, 2019 at 8:34
  • oh sorry - gimme an hour
    – Alex028502
    Jan 22, 2019 at 8:39
  • @Alex028502 You just did echo 'from: $feature1_id' > tmp, without the git rev-parse --short HEAD > tmp, as well.
    – 9301293
    Jan 22, 2019 at 8:43
  • @Alex028502 The idea is that one of those is the "root" commit, and the other is the more dynamic "every" commit.
    – 9301293
    Jan 22, 2019 at 8:43
  • when I try to do exactly what you have there I get only exec git rev-parse --short HEAD > tmp && echo 'from: ' > tmp && git commit --amend -F tmp in git-rebase-todo I don't think that variable gets exported. But either way I don't see how it can work because it will put the same hash into every commit message no matter what
    – Alex028502
    Jan 22, 2019 at 10:02
2

building on https://stackoverflow.com/a/54352290/5203563, I ended up with a script that looks like this

#! /usr/bin/env bash

set -e

if [[ "$@" != "" ]];
then
    git rebase $@ --exec="$0"
    exit 0
fi

HASH="$(grep -o "\w\{40\}" .git/rebase-merge/done | tail -n1)"

git log --pretty="format:%B%n%nwas $HASH" -n1 | git commit --amend -F -

I wanted to avoid temporary files. I originally did it in two scripts, but then combined them into one with this recursive thing so that it can be easily shared. I have called mine git-record so if you go git record HEAD^^^^ it'll put the current hashes in all the commits (and change all the hashes in the process).

1

If you're just using rebase to do a batch cherrypick, you can easily construct its pick list yourself and do the cherrypicks with any options you want:

batchxpickto() {
        local U=${1-@{u\}}      # given branch or default to configured upstream 
        local B=`git symbolic-ref -q --short HEAD`  # branch name to move if any
        local L=`git cherry $U.. | awk /^+/{print\$2}`  # commits to pick if any
        git checkout $U && ${L:+git cherry-pick -x $L}
        ${B:+git checkout -B $B}
}

use batchxpickto master the way you'd use git rebase master.

Really, I like Adam's answer better, but this is an alternative and its tactics might be more generally useful.

2
  • I consider myself fairly competent in bash and git don't understand any of that wizardry! What's the {L:+git bit doing? Can you link me to any bash docs please?
    – Adam
    Jan 24, 2019 at 22:32
  • 1
    @Adam man bash and hunt up parameter expansion, braces around the variable name allow modifiers to the expansion, :+ says "expand to the following if the variable's set and not empty"‚ :- is expand to the following if the variable's unset or empty, leave off the : to leave off the empty test. So ${1-@{u\}} is the first parameter if that's set, otherwise @{u} with the backslash being the usual syntax escape; the ${L:+git cherry-pick -x $L}` expands to nothing if there's nothing to cherrypick; and so on. This isn't bash-specific, this is underappreciated standard stuff.
    – jthill
    Jan 25, 2019 at 4:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.