Zeno's Notes

...

Posts tagged software development

1 note

TIL: Convenient cherry-picking from another git repository

At work, I sometimes (well, actually quite often) need to merge commits from different repositories or branches. Usually, I do not want all of them, only some.

This is where git’s cherry-pick command comes in. Basically, it applies a specific commit to your current repository, for example

git cherry-pick 727af6f5c7a7c65df6e300be58c9aa369da976e0

My old workflow was like this:

  1. git fetch some-remote-repository — fetch the updates (new commits) to the remote repository
  2. git log -p master..some-remote-repository/master — list the changes (commits) to the remote repository
  3. From the log, select and copy-paste the commits I want to cherry pick, using the command above.

This works nicely, but it does not scale if you have more than, say, half a dozen commits. Then the copy-paste business becomes very annoying.

So I was looking for a better workflow. I had a look at various GUI tools, but none of them (after an admittedly superficial look …) allowed me to cherry-pick from a remote repository. I am quite sure that some of them can do this somehow, but I did not find a way to either (a) specify the remote repository and fetch the data or (b) cherry-pick.

Neither asking for input on Twitter, nor Google provided me with a satisfactory answer.

After playing with tig, a curses-based git client, for a while, I found out that it supports cherry picking - by entering “C” when the relevant commit is selected. So the missing piece was to find out how to get the information about the remote repository. It turns out to be quite easy as well: Just start tig with the remote branch name.

So now it is:

  1. git fetch some-remote-repository
  2. tig some-remote-repository/master
  3. select relevant commits and enter ‘C’ for each of them

Here is a screenshot of tig:

Navigation in tig is very similar to less, you can use “/” to search, and “h” for help. Press “q” for quitting the help again.

If you are using a GUI git client (free as in liberty, on Linux) regularly, and know how to perform cherry-picking in an elegant/efficient way, I would like to hear from you.

July 1, 2011: If you want to add a new remote repository you want refer to as some-remote-repository as above, you need to use this command: git remote add some-remote-repository URL, e.g. git remote add MML-github git@github.com:zenogantner/wikipedia-tools.git

Filed under git software development verison control programming tricks