git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Picking up multiple cherries at one go.

From
JBJozef Babjak <jozef.babjak@gmail.com>
Date
Mar 24, 2010, 08:47 UTC
Message-ID
<ffef8f9a1003240147o777d058jfacd79aa86908925@mail.gmail.com>
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
Next: Alex Riesen
Message 1 of 3 in “Picking up multiple cherries at one go.”
  1. Jozef BabjakMar 24, 2010
  2. Alex RiesenMar 24, 2010
  3. Alex RiesenMar 24, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.