I'm using git on a new project that has two parallel -- but currently experimental -- development branches:
master
: import of existing codebase plus a few mods that I'm generally sure ofexp1
: experimental branch #1exp2
: experimental branch #2
exp1
and exp2
represent two very different architectural approaches. Until I get further along I have no way of knowing which one (if either) will work. As I make progress in one branch I sometimes have edits that would be useful in the other branch and would like to merge just those.
What is the best way to merge selective changes from one development branch to another while leaving behind everything else?
Approaches I've considered:
git merge --no-commit
followed by manual unstaging of a large number of edits that I don't want to make common between the branches.Manual copying of common files into a temp directory followed by
git checkout
to move to the other branch and then more manual copying out of the temp directory into the working tree.A variation on the above. Abandon the
exp
branches for now and use two additional local repositories for experimentation. This makes the manual copying of files much more straightforward.
All three of these approaches seem tedious and error-prone. I'm hoping there is a better approach; something akin to a filter path parameter that would make git-merge
more selective.
Here is how you can replace
Myclass.java
file inmaster
branch withMyclass.java
infeature1
branch. It will work even ifMyclass.java
doesn't exist onmaster
.Note this will overwrite - not merge - and ignore local changes in the master branch rather.
I know this question is old and there are many other answers, but I wrote my own script called 'pmerge' to partially merge directories. It's a work in progress and I'm still learning both git and bash scripting.
This command uses
git merge --no-commit
and then unapplies changes that don't match the path provided.Usage:
git pmerge branch path
Example:
git merge develop src/
I haven't tested it extensively. The working directory should be free of any uncommitted changes and untracked files.
1800 INFORMATION's answer is completely correct. As a git noob, though, "use git cherry-pick" wasn't enough for me to figure this out without a bit more digging on the internet so I thought I'd post a more detailed guide in case anyone else is in a similar boat.
My use case was wanting to selectively pull changes from someone else's github branch into my own. If you already have a local branch with the changes you only need to do steps 2 and 5-7.
Create (if not created) a local branch with the changes you want to bring in.
$ git branch mybranch <base branch>
Switch into it.
$ git checkout mybranch
Pull down the changes you want from the other person's account. If you haven't already you'll want to add them as a remote.
$ git remote add repos-w-changes <git url>
Pull down everything from their branch.
$ git pull repos-w-changes branch-i-want
View the commit logs to see which changes you want:
$ git log
Switch back to the branch you want to pull the changes into.
$ git checkout originalbranch
Cherry pick your commits, one by one, with the hashes.
$ git cherry-pick -x hash-of-commit
Hat tip: http://www.sourcemage.org/Git_Guide
I like the 'git-interactive-merge' answer, above, but there's one easier. Let git do this for you using a rebase combination of interactive and onto:
So the case is you want C1 and C2 from 'feature' branch (branch point 'A'), but none of the rest for now.
Which, as above, drops you in to the interactive editor where you select the 'pick' lines for C1 and C2 (as above). Save and quit, and then it will proceed with the rebase and give you branch 'temp' and also HEAD at master + C1 + C2:
Then you can just update master to HEAD and delete the temp branch and you're good to go:
To selectively merge files from one branch into another branch, run
where
branchX
is the branch you want to merge from into the current branch.The
--no-commit
option will stage the files that have been merged by Git without actually committing them. This will give you the opportunity to modify the merged files however you want to and then commit them yourself.Depending on how you want to merge files, there are four cases:
1) You want a true merge.
In this case, you accept the merged files the way Git merged them automatically and then commit them.
2) There are some files you don't want to merge.
For example, you want to retain the version in the current branch and ignore the version in the branch you are merging from.
To select the version in the current branch, run:
This will retrieve the version of
file1
in the current branch and overwrite thefile1
automerged by Git.3) If you want the version in branchX (and not a true merge).
Run:
This will retrieve the version of
file1
inbranchX
and overwritefile1
auto-merged by Git.4) The last case is if you want to select only specific merges in
file1
.In this case, you can edit the modified
file1
directly, update it to whatever you'd want the version offile1
to become, and then commit.If Git cannot merge a file automatically, it will report the file as "unmerged" and produce a copy where you will need to resolve the conflicts manually.
To explain further with an example, let's say you want to merge
branchX
into the current branch:You then run the
git status
command to view the status of modified files.For example:
Where
file1
,file2
, andfile3
are the files git have successfully auto-merged.What this means is that changes in the
master
andbranchX
for all those three files have been combined together without any conflicts.You can inspect how the merge was done by running the
git diff --cached
;If you find some merge undesirable then you can
git commit
If you don't want to merge
file1
and want to retain the version in the current branchRun
If you don't want to merge
file2
and only want the version inbranchX
Run
If you want
file3
to be merged automatically, don't do anything.Git has already merged it at this point.
file4
above is a failed merge by Git. This means there are changes in both branches that occur on the same line. This is where you will need to resolve the conflicts manually. You can discard the merged done by editing the file directly or running the checkout command for the version in the branch you wantfile4
to become.Finally, don't forget to
git commit
.Easiest way is to set your repo to the branch you want to merge with then run,
If you run
you will see the file already staged...
Then run
Simple.