0

This is a follow up question for Git cherry pick those commits that contain a keyword (tracking id)

I want to create an orphan branch for code reviews. This orphan branch will be made from the SHA_of_first_commit~1 with commit messages that contain my trackingID. After the orphan branch is created, I cherry-pick those commits with messages that contain my trackingID. The problem is all the cherry-picked commits are shown individually in the orphan branch. I want to all of them to be seen as a single commit so that I can review the code with this new SHA. Following is the script I use now.

#!/bin/bash

if [ -z $1 ]; then
    echo "Rationale: Cherry pick all commits in master, that match the tracking ID and create a new branch.";
    echo "";
    echo "Usage:  $0 traackingID";
    echo "";
    exit 1;
fi

#If $1 doesn't match a AGW-<number> pattern, thrown an error

user="$(id -u -n)" > /dev/null

echo "You are - $user"

branchname=$user"_"$1"_review"

echo "Creating branch - $branchname"

basecommit="$(git log master --pretty=oneline --reverse | grep "$1" | head -1 | cut -d ' ' -f 1)""~1"

echo "Checking out from $basecommit"

git checkout --orphan $branchname $basecommit > /dev/null
git commit -m "$1"

git rev-list master --reverse --grep="$1" | while read rev
do
  echo $rev
  git cherry-pick $rev > /dev/null
done

#git push &> /dev/null

echo "Created branch, cherry picked, pushed to remote. Now switching back to master. Bye."

git checkout master
Community
  • 1
  • 1
TechCrunch
  • 2,924
  • 5
  • 45
  • 79

1 Answers1

5

To be honest, I don't see the point of checking out an orphan branch and reapplying a range of commits for code review. But that's not the question.

git cherry-pick accepts a -n (--no-commit) option. From the documentation, emphasis mine.

Usually the command automatically creates a sequence of commits. This flag applies the changes necessary to cherry-pick each named commit to your working tree and the index, without making any commit.

After cherry-pick applied the changes in question you can finish the process by making a simple git commit.

Sascha Wolf
  • 18,810
  • 4
  • 51
  • 73
  • I started with orphan branch as I din't want to have an references form master to it. I realized I won't mess anything if I don't use orphan branch. – TechCrunch Mar 02 '15 at 16:47