tags:

views:

104

answers:

2

Sometimes I'm working with several branches at once. Say I have branches called master, release-1.1, and experimental. I create a new file or make a change in experimental and I want that one single change to apply to the other branches.

Can I do this in git? If I simply merge the commit into another branch, git automatically "fast-forwards" and includes any commits in between. But there must be some way to handle this use case.

+7  A: 

What you want to do is called cherry picking. You can cherry pick a single commit using the following command:

$ git cherry-pick <commit hash or name>

This will incorporate the change from that commit only into your current branch. Note, however, that this creates a new commit; this new commit has the exact same changes as the cherry-picked commit (and even the same commit date and author), but it is technically a new commit, so you'll see it as a new commit in, e.g., gitk. You'll also have to perform the cherry pick for each branch in which you want to see the change.

mipadi
+3  A: 

The solution, but unfortunately the one you rather need to know before making the change, is to create separate branch only for this change (so called "topic branch"), off the earliest branch or earliest commit that makes sense, and merge this branch into any branch that needs this commit.

In some cases other solution could be to make change in maintenance branch, merge maintenance branch into stable branch, and merge stable branch into development branch.

Junio C Hamano (Git maintainer) wrote about this in his blog: Resolving conflicts/dependencies between topic branches early

Jakub Narębski
Do you have a link to that blog entry? I think that would be helpful, coming from a Git authority.
Greg Hewgill
Maybe that blog entry? http://gitster.livejournal.com/27297.html
VonC
Cherry picking does seem more appropriate than this, sorry to say.
Ryan Bigg
@VonC: That is the one. Thanks (answer updated). @Radar: Cherry-picking creates many duplicates of commits, which can hurt at reintegration time. If it is one branch, it doesn't matter much, though.
Jakub Narębski