Tuesday, August 20, 2013

git cherry-pick : move a selected commit to another branch

This git post is to demonstrate :
1 - use git cherry-pick to move a selected commit (among many commit of a brnach ) to amother from
2 - git review : just one commit out of many commit to gerrit code review

Working Bed:

Clone you central repo to your local system. create a branch alphabranch create a file first_file.txt and commit it.

$ git clone ssh://gerrit:29418/centralrepo.git mygitrepo
Initialized empty Git repository in /home/nasimuddin.ansari/git/mygitrepo/.git/
remote: Counting objects: 8272, done
remote: Finding sources: 100% (8272/8272)
remote: Total 8272 (delta 3517), reused 7971 (delta 3517)
Receiving objects: 100% (8272/8272), 1.31 MiB | 932 KiB/s, done.
Resolving deltas: 100% (3517/3517), done.

$ cd mygitrepo/

$  git checkout -b alphabranch
Switched to a new branch 'alphabranch'

$  git branch
* alphabranch
  master

$  date >first_file.txt

$  git status
# On branch alphabranch
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       first_file.txt
nothing added to commit but untracked files present (use "git add" to track)

$  git add first_file.txt

$  git status
# On branch alphabranch
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   first_file.txt
#

$  git commit -m"first commit for first_file.txt"
[alphabranch 1e9f56d] first commit for first_file.txt
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 first_file.txt

$  git log -1
commit 1e9f56d5e1acdf90360ddf553dd47cdc15071173
Author: Nasimuddin Ansari <nasimuddin.ansari@company.com>
Date:   Tue Aug 20 07:26:50 2013 +0000

    first commit for first_file.txt


Create a second file, do second commit and then create third file and do third commit in same branch alphabranch.

$  cal >second_file.txt
$  git add second_file.txt &&  git commit -m"second commit for second_file.txt"
$  cal 2013 >third_file.txt
$  git add third_file.txt && git commit -m"third commit on third_file.txt"

$  git log --graph --decorate --oneline -n3
d60352(HEAD, alphabranch) third commit on third_file.txt
e30f0b second commit for second_file.txt
1e9f56 first commit for first_file.txt


$  git status
# On branch alphabranch
nothing to commit (working directory clean)

 Now it is time to submit it for gerrit review. I just want to submit only second commit for review for the moment. On first and 3rd I need to do some more changes before submitting for review. Do a dry run and you know the problem - all three commits are listing in git review list !



$  git review -n
You have more than one commit that you are about to submit.
The outstanding commits are:

d60352(HEAD, alphabranch) third commit on third_file.txt
e30f0b second commit for second_file.txt
1e9f56 first commit for first_file.txt

Is this really what you meant to do?
Type 'yes' to confirm:no

I  just want to submit second commit for review.

git cherry-pick : moving a selected commit to new branch

Create a  commitbranch branch from master

$  git checkout -b commitbranch master
Switched to a new branch 'commitbranch'

$  git log --graph --decorate --oneline -n5
* 3d1bb52 (HEAD, master, commitbranch) boss has submitted fix
* bb7ebab change for system foo config file

Switch to alphabranch and find out commit ID you want to move to commit branch

$  git checkout alphabranch
Switched to branch 'alphabranch'

$  git log --graph --decorate --oneline -n3
d60352(HEAD, alphabranch) third commit on third_file.txt
e30f0b second commit for second_file.txt
1e9f56 first commit for first_file.txt


Now switch to commitbranch and do cherry-pick on second commit's ID.

$  git checkout commitbranch
Switched to branch 'commitbranch'

$  git cherry-pick e30f0b
Finished one cherry-pick.
[commitbranch c957988] second commit for second_file.txt
 1 files changed, 8 insertions(+), 0 deletions(-)
 create mode 100644 second_file.txt
                            ( Commit ID will change !)

$  git log --graph --decorate --oneline -n3
* c957988 (HEAD, commitbranch) second commit for second_file.txt
* 3d1bb52 (master) boss has submitted fix
* bb7ebab change for system foo config file


Now you are good to submit only one change for review. Do a dry run and submit it !

$  git review -n
Please use the following command to send your commits to review:

        git push gerrit HEAD:refs/publish/master/commitbranch

$ git review

                              Did this post help anyone ?

No comments:

Post a Comment