0 votes
1 view
in Devops and Agile by (29.9k points)

I'm using git on a new project that has two parallel -- but currently experimental -- development branches:

1. master: import of existing codebase plus a few mods that I'm generally sure of

2. exp1: experimental branch #1

3. exp2: 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.

2. 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.

3. 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 straight forward.

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.

1 Answer

0 votes
by (51.2k points)

To get individual commits from one branch use git-cherry-pick commands

Refer: https://git-scm.com/docs/git-cherry-pick

If suppose your changes are not in individual commits then use the following method by splitting the commits to an individual commits and then you can use cherry-pick

git rebase -i

To get the original commit to editing, then

git reset HEAD^ 

to selectively revert changes, 

Then 

git commit

To commit that bit like a new commit in history.

In Red-Hat-Magazine there is one more way, where we use 

git add --patch

 Or

 possibly git add --interactive

So this will help you to change the changes to individual files then you can use cherry-pick commands to selectively merge or pick changes from another branch in Git.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...