Cherry-Picking for Fun and Profit

In Git, it is often useful to merge one specific commit from one branch into another. Frequently this happens when Using Git Interactive Rebase With Feature Branches, as you develop in the branch, you realize that one or more of your commits should be added to master right away, but not all the CLs. Enter git cherry-pick <changelist hash> for the win! The cherry pick command allows you to merge one CL from a branch, or even straight from the reflog into another branch.

Getting ready

For this article, lets assume you have a commit history that looks like:

fc64d55 -- d0e6ac9 -- 1edbe95 -- 24c685b -- 2f0514a  [master]
                         \
                          a70d41c -- b45af4a -- 38967f5 [branch feature1]

And we want to merge some important change in #b45af4a into master.

How do it…

The code to cherry pick the change into master is:

git checkout master
git cherry-pick b45af4a

Now rebase master on to the feature1 branch:

git checkout feature1
git rebase master

You may have to resolve conflicts, but notice that git is smart enough not to reapply the cherry picked changelist:

Applying: a70d41c
Applying: 38967f5

How it works…

It is important to note that cherry-pick behaves just like a merge. If there are merge conflicts that git cannot resolve, then it will ask you to manually resolve them. Consequently, the new changelist in master will not necessarily be identical to the cherry-picked CL, and it will definitely have a new hashcode. Fortunately, git tracks cherry-picked CLs, so when you rebase back onto the cherry-picked branch, it won’t duplicate the CLs and will restructure your history according to master.

Also, while cherry-pick is great for merging one or two commits, it is not ideal from many commits.

Connecting to Github and EC2 Through a Proxy on Port 80 or 443

Today we’ll cover how to connect to github and EC2 through a draconian proxy allowing only port 80 and 443. Github uses SSH, so like EC2 it can be connected to using SSH tunnelling. This article is based on a blog post by tachang[1], which needed some additional explanation and changes to work behind my proxy. I will be explaining how to connect on a unix-based machine, but these settings should also work on ...

Develop Locally Using Git, Instead of Your Company's Crappy Version Control

At my new company (Ariba), I am forced to use a beastly version control system (Perforce) and it was really cramping my style. I couldn’t easily branch or check in small incremental changes (even locally), so I was going days to weeks without checking in, waiting until my code was production ready. This meant if I made a mistake and had to roll back, or wanted to maintain multiple version of a file, I ...

Encrypting Settings Files

When I was developing for Votizen, I always felt uneasy that we had our settings files with many important passwords and auth keys stored unencrypted on a third-party service. Sure, SSL was used for fetching and committing code, and only a few SSH public keys were allowed access to our repository, but it is one more opportunity for your site to be hacked. I recently read John Resig’s post Keeping Passwords in Source ...

Using Git Interactive Rebase With Feature Branches

The next GIT topic to cover is branching and using interactive rebase before merging the branch back into master. This article will cover a workflow strategy where you create a local branch for a new feature where you can make many small commits. Then before merging and committing to master, use interactive rebase to squash some (or all) of the less meaningful commits, keeping the commits to master more concise and feature focused.

Getting ready

...

Git Rebase Versus Git Pull

Before continuing the GIT series, it is worthwhile to tangent a bit and write about using git rebase versus git pull. Using git pull will fetch any changes from the remote branch and merge them on to your local branch, creating a new merge commit. Using git rebase will remove each of your commits temporarily (stored in .git/rebase), update your local branch to match the remote branch, then apply each of your local changes again ...

Amending and Squashing Commits in Git

Have you ever committed code to your Git repository, and then realized that you forgot something or there was an error in your code? Wish there was an easy way to amend your commit with the fix? What about if you've already pushed your code or made several commits? If you run a strong test suite, and commit before running the whole test, this probably happens regularly (at least it does for me). Usually each ...