views:

125

answers:

1

I understand how to merge branches together in git, and I love how easy it makes it.

I have a branch that looks like this:

project/
|--subproj1/
|  |---(files)
|
|--subproj2/
   |---(files)

A colleague has a branch that's made significant changes to both subprojects. More specifically, he's altered them both to depend on a common bit of code in a new subproj3. However, for various reasons, we're not pushing his branch anytime in the forseeable future.

My branch diverges from master only in subproj1, where I'm making subproject-specific enhancements. My subproject is less important than some of the others, and thus has less constraints on what types of changes we can push on it. I'd like to merge in my colleague's subproj1 changes (and his new subproj3 which those changes depend on) but leave my existing subproj2 alone. Sometime in the future, when he merges his project in entirety to master, I'd like his subproj2 changes to merge in normally.

How do I do this with git?

+1  A: 

Each commit is a logical unit of work. Unfortunately, that means you cannot separate the edits you colleague has made to subproj1 and subproj2 if he made them on the same branch.

However, to do what you are asking, you would use git filter-branch. You can use this to create a new branch which is defined by replaying all of your colleague's commits but only adding changes to subproj1 to each commit.

The commands are something along the lines of:

git fetch colleague-repo ...
git filter-branch --subdirectory-filter subproj1 -- A..B

(where A..B is the range of commits you fetch from his repository)

By doing this you are creating a unique branch with work that is totally separate from your colleague's work. If you merge this new branch and your colleague's branch into master, the history of changes to subproj1 will permanently be split between the two branches.

Stephen Jennings
Sweet, I'll give this a try. Thanks!
Cory Petosky