How to merge branches in Git by "hunk"

Go To StackoverFlow.com

6

Here's the scenario. I made a "dev" branch off the "master" branch and made a few new commits. Some of those changes are going to only be relevant to my local development machine. For example I changed a URL variable to point to a local apache server instead of the real URL that's posted online (I did this for speed during the testing phase). Now I'd like to incorporate my changes from the dev branch into the master branch but NOT those changes which only make sense in my local environment.

I'd envisioned something like a merge --patch which would allow me to choose the changes I want to merge line by line.

Alternatively maybe I could checkout the "master" branch, but keep the files in my working directory as they were in the "dev" branch, then do a git add --patch. Would that work?

2012-04-05 23:19
by asolberg


6

One approach would be to merge the changes from the other branch, but remove the development-specific content before committing the merge.

First, get the changes, without committing them with git merge --no-ff --no-commit dev.

Remove the development-specific changes either by editing the affected files and git adding them, or git reset HEAD the affected files and then git add --patch the parts you want.

Then commit the merge. An advantage of doing a merge commit is that future merges will be painless. Because the development-specific commits are considered merged, future merges not involving development-specific pieces can be done with a simple git merge dev. In this way you can indefinitely maintain a separate branch with its own configuration while still painlessly merging changes into the master branch.

2012-04-06 00:21
by blahdiblah
That didn't seem to work. I tryped git merge --no-commit dev and got back: "Updating 91a6696..056b567 Fast-forward ". So then I typed git reset HEAD and git add --patch and it says "no changes. - asolberg 2012-04-07 00:13
Ah, sorry about that, I didn't anticipate a fast-forward merge. Try with the updated command that includes '--no-ff'. That should force the merge commit to be created, though not actually committed - blahdiblah 2012-04-07 00:17


2

Maybe you could try something like this?

git merge --squash --no-commit $YOUR_OTHER_BRANCH
git reset HEAD
git add -p
git commit
2012-04-05 23:25
by pioto
note that --squash implies --no-commit, as of the latest git anywa - CharlesB 2012-05-31 12:40
Ads