GIT: How to take a single commit from one branch and apply it to another.

Hello folks.

One Git command that I've found to be really useful lately is "git cherry-pick". It lets you take a single commit from one branch and apply it to another.
 
For example, say you're working on your "new_module" feature branch, and you find a bug in the "hook_menu" method. You fix it and commit to the branch between a couple of other feature commits.
 
Now let me show the workflow to apply your change quickly and easily.
 
1. Clone your branch of git in your local machine
Imagine you have your server: git@gitserver.com:/yourproject.git with two branches: Release and Develop.
#git clone git@gitserver.com:/yourproject.git  -b Develop develop_folder

Now we fix the files and push changes to the HEAD of our branch

#git pull
#git commit -m "Fixing issue #7"
#git push origin master

Now we have to get the SHA1 code for the specific commit.

#git log -1 --pretty=oneline
b5d39bcb8658f287ea6d6c5b239f4ba514239831 Fixing issue #7

2. Put the commit to our Release Branch

Now we need to checkout our release branch in  a new folder

#git clone git@gitserver.com:/yourproject.git  -b Release release_folder

Now we need to execute the command to bring the commit from our Develop branch to our Release branch using the SHA1 code

#git cherry-pick b17f5b936b34ef9f827cb48ed348797f967ba258

Here's what that command does:

  • Look at that commit on the feature branch
  • See what changes it introduced
  • Create a new commit on the master branch that introduces the same changes.

At the end we only need to push our changes to the HEAD of our Release branch.

#git push

Enjoy IT

enzo.