Zeno's Notes


Posts tagged git

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

1 note

Today I learned …

… some new things about git: How to push things to remote branches, and how to push things to several remote repositories at once (thanks to Carsten for the hint).

Remote Branches

Currently, I am re-working some underlying data structures of MyMediaLite, the recommender system library that I (well, mostly me) develop. This breaks things BIG TIME, and for quite a while. In other words, a really good case for branching.

I knew how local branching and merging works, but I had no idea how to push the contents of branches to a remote repository without pushing it to the remote master branch.

It turns out it is quite easy:

git push git@gitorious.org:mymedialite/mymedialite.git new_ratings:new_ratings

Well, everything looks easy with git once you find out how to do it. It was not obvious to me, and reading the man pages also did not really help.

Pushing to Several Remote Repositories

My other problem was that I wanted to keep both the gitorious as well as the github repository up to date. StackOverflow had the answer. This is how it looks in my .gitconfig file:

[remote "MML"]
    url = git@gitorious.org:mymedialite/mymedialite.git
    url = git@github.com:zenogantner/MyMediaLite.git

I can then use the MML alias to push to both repositories:

git push MML

Combining Both Features

And guess what, of course the two features can be combined without a problem:

git push MML new_ratings:new_ratings

Filed under git version control mymedialite github gitorious