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

Re: cherry picking several patches at once

From
MWMarc Weber <marco-oweber@gmx.de>
Date
Jan 24, 2010, 15:31 UTC
Message-ID
<1264345006-sup-8582@nixos>
In-Reply-To
<94a0d4531001240510g25ea3398qdd0c7b98f9b81209@mail.gmail.com>
> Ok, in that case we would just need a cherry-pick that can commit to a
> separate branch, however I don't think git internals allow that kind
> of thing.
Which is the best way to implement this git-cherry-push command?
# naive implementation (BASH function):
gitCherryPush(){
  # start a subshell so that failures don't quit your shell because of
  # set -e
  (
  set -e
  local branch=$(cat .git/HEAD |  sed 's@ref: refs/heads/\(.*\)@\1@')
  local otherBranch=$1; shift
  commits="$(
    for range in "$@"; do
      local HAS_DOTS=${range%%*..*}
      # add -1 if commit is not a range
      git rev-list --reverse ${HAS_DOTS:+-1} "$range"
    done
  )"
  echo "checking out other branch"
  git checkout "$otherBranch"
  echo "$commits" | while read c; do
    git cherry-pick "$c" || {
      echo "cherry-picking $c failed. fix it, exit 1 to abort. Starting subshell now"
      $SHELL || [ "$?" != 1] || return
    }
  done
  echo "checking out original branch $branch"
  git checkout $branch
  )
}
zsh completion:
compdef _gitCherryPush gitCherryPush
_gitCherryPush(){
  typeset -A opt_args
  _arguments -S \
   ':branch to which to move commit range:__git_branch_names' \
   ':branch to which to move commit range:__git_commit_ranges2'
}

Example usage (push last 5 commits) gitCherryPush branch-to-push-to HEAD~4 HEAD~4..HEAD

Can I checkout the other branch without touching the working copy? I don't think so because cherry-pick will fail operating if working directory is not clean, correct?

Is something like this worth comitting to the main repo?
Marc Weber
Previous: Felipe Contreras
Message 7 of 7 in “cherry picking several patches at once”
  1. Felipe BalbiJan 21, 2010
  2. Felipe ContrerasJan 22, 2010
  3. Marc WeberJan 23, 2010
  4. Sverre RabbelierJan 23, 2010
  5. Felipe BalbiJan 24, 2010
  6. Felipe ContrerasJan 24, 2010
  7. Marc WeberJan 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.