threads / discuss / 23161

Picking up multiple cherries at one go.

Subject: Picking up multiple cherries at one go.

## tl;dr

3 messages between Mar 24, 2010 and Mar 24, 2010.

replies: 2people: 2as markdown or json

Jozef Babjak· Mar 24, 2010, 08:47 UTC · lore
Hello all!

I'm a quite new git fellow, but I must to say that the git already affected my development work significantly. I found out that almost all imaginable tasks related to VCS are directly supported by git out-of-the-box and all tools are very pragmatic. Great!

However, last days I needed to split a long multi-purpose branch into several topic branches. The branch was created as a result of 'general cleanup and refactoring', but at the end of day I realized that some well-defined parts can be identified there and so I wanted to extract them to dedicated branches. Thus, cherry-pick and rebase were my friend for couple of days. Everything goes well, but there was a repetitive task - cherry picking multiple commits. Sometimes the process failed, because cherry picking led to conflicts. To simplify this task, I wrote the shell script attached at the end of this email.The script named git_cherries.sh reads commit IDs from stdin and cherry-picks those commits to current branch. Everything is done on an auxilliary branch, so the process is transactional - it succeed completelly or it does not change anything. You can find more detailed information at the beginning of te script.

Please try the script and feel free to use it, if you consider it to be useful. It would be nice to have something similar in git suite. Any feedback is appreciated.

Jozef Babjak
---- git_cherries.sh ---
#! /bin/sh

## ## This script cherry-picks more commits at one go. Commit IDs are read ## from standard input and applied to the current branch. Everything is ## done on auxilliary branch under the hood, so the change is done ## completely or it is not done at all. This is useful when refactoring ## a long multi-purpose branch into several separated branches. ## ## Usage [just an example, but you got the idea]: ## ## git checkout master ## git checkout -b new_topic ## git log --pretty=oneline master..multi_topic_branch | \ ## tail -n 5 | cut -f 1 -d " " | tac | git_cherries.sh ## ## The commands above gets the last 5 commits from 'multi_topic_branch' ## and applies them to newly created 'new_topic' branch. Please note ## that tac(1) is used to reverse order of commits to applying them in ## proper order. ## ## Any additional command line arguments are without change passed to ## git-cherry-pick(1) command. This makes sense for -x, -r, -s and -n ## options. ## ## Program needs to be invoked only from root of a GIT repository. ##

if [ ! -d "./.git" ] ; then
        echo "This is not a root directory of any GIT repository: '`pwd`'"
        exit 1
fi
current_branch=`git branch | grep '*' | cut -f 2 -d " "`
tmp_branch="${current_branch}_cherrypicking_${$}"

## ## Auxilliary branch creation. ## git checkout --quiet -b "${tmp_branch}"

##
## Applying cherry picks.
##
while read commit ; do
        git show --pretty=oneline "${commit}" | head -n 1
        git cherry-pick "${@}" "${commit}" > /dev/null 2>&1 ||
cherry_pick_failed="true"
        if [ ! -z "${cherry_pick_failed}" ] ; then
                break
        fi
done
##
## We are done with cherry picking now. If 'cherry_pick_failed' is
## set something went wrong and we cannot merge auxilliary branch.
##
if [ ! -z "${cherry_pick_failed}" ] ; then
        git reset --hard -q
fi
git checkout --quiet "${current_branch}"
if [ -z "${cherry_pick_failed}" ] ; then
        git merge "${tmp_branch}"
fi

## ## Success or not, delete auxilliary branch. ## git branch -D "${tmp_branch}" > /dev/null

if [ -z "${cherry_pick_failed}" ] ; then
        echo "Done." 1>&2
        exit 0
else
        echo "Failed: ${commit}" 1>&2
        exit 1
fi
Alex Riesen· Mar 24, 2010, 09:21 UTC · re: Jozef Babjak · lore

Re: Picking up multiple cherries at one go.

On Wed, Mar 24, 2010 at 09:47, Jozef Babjak <jozef.babjak@gmail.com> wrote:
Show 8 quoted lines
> However, last days I needed to split a long multi-purpose branch into
> several topic branches. The branch was created as a result of 'general
> cleanup and refactoring', but at the end of day I realized that some
> well-defined parts can be identified there and so I wanted to extract
> them to dedicated branches. Thus, cherry-pick and rebase were my
> friend for couple of days. Everything goes well, but there was a
> repetitive task - cherry picking multiple commits. Sometimes the
> process failed, because cherry picking led to conflicts. ...

Try git rebase -i (interactive) next time. It does not exactly what your script does, but ... You may like it.

> To simplify this task, I wrote the shell script attached at the end of this
> email.The script named git_cherries.sh reads commit IDs from stdin and
> cherry-picks those commits to current branch. Everything is done on an
> auxilliary branch, ...

You can commit without a branch (called "detached HEAD"). Try "git checkout HEAD^0", for example. No need to invent new branch names (and risk collisions).

Alex Riesen· Mar 24, 2010, 11:52 UTC · lore

Re: Picking up multiple cherries at one go.

Please, don't strip gitml from cc. Your questions maybe interesting to someone else.

On Wed, Mar 24, 2010 at 10:36, Jozef Babjak <jozef.babjak@gmail.com> wrote:
Show 12 quoted lines
> Thanks for hint. I did not used interactive git commands ever; maybe I
> should try.
>
>> Try git rebase -i (interactive) next time. It does not exactly what your
>> script does, but ... You may like it.
>
>  ^-- Do you mean to make a copy of the multi_purpose_branch first and
> then to rebase interactively that new base onto master or any other
> ancestor branch and removing unwanted comits? The issue is that I
> usually want to keep only small portion of commits in newly created
> topic branch. It seems to be easier to mark somehow 5 commits I want
> instead of 95 commits I do not want.

You'll be amazed how easy it is (and how nothing it costs) to make a copy of a branch: "git branch tmp". And you can rebase-interactive a portion of the branch you're on: "git rebase -i HEAD~10". This will consider last 10 commits for cherry-picking. And even if you want to review all 100 commits, it maybe not that hard if you have a plain text file with subject lines of the commits (and their sha1). You always can start a history browser of your choice (gitk?) to help you with that. At least that's how I do that since Johannes invented rebase-interactive.

← back to recent threads