For code review purpose, I want to
- cherry pick specific commits
- create a new branch with them and
- push that branch to remote
so that I can give branch url to peers for review.
I want to create a shell script and issue simple command like
git-review <trackingID>
It gives output as below
Branch <currentgitusername>_<trackingID> created and pushed to remote.
I put together a script that does above mentioned steps.
#!/bin/bash
if [ -z $1 ]; then
echo "Rationale: Cherry pick all commits in master, matching 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
#Best of luck if you did not add add tracking ID in commits.
user="$(id -u -n)" > /dev/null
echo "You are - $user"
branchname=$user"_"$1"_review"
echo "Creating branch - $branchname"
git checkout -b $branchname > /dev/null
git log master --pretty=%H --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
But got stuck in couple of places.
- I want to validate the trackingID format. It should be
AGW-<somenumber>
Looks like the cherry-picks have merge issues.
myname@mymachine ~/myproj $ ../git-review.sh AGW-1234 You are - myname Creating branch - myname_AGW-1234_review Switched to a new branch 'myname_AGW-1234_review' 2dfafa89e10062e7cfccb1d7a947ebb848c40cc6 The previous cherry-pick is now empty, possibly due to conflict resolution. If you wish to commit it anyway, use: git commit --allow-empty Otherwise, please use 'git reset' 1e295000bc3d80880518c9cac5e34ef3b28fc29e error: Your local changes to the following files would be overwritten by merge: rest-service/src/main/java/package/HealthCheckDAOImpl.java Please, commit your changes or stash them before you can merge.
Am I cherry picking in wrong way ? Also, please suggest any changes to make this script robust.
As already pointed in the comments by @novelocrat, You are cherry picking in the wrong order. By default, git log will output from the latest commit to the first commit. To do this, either feed in the output of
git log --oneline
totac
, or use the--reverse
flag:The second option is obviously prefered, because that ensures line by line output, while the first (using
tac
) will need to be fed all the input in one go.Also, since you are creating a new branch out of master, when you do
git checkout -b $branchname
.When you do that, all the history of the current branch is picked from that.
So you need to create an orphaned git branch, like mentioned here.