Frage

My remote Git repository has 3 branches:

  1. master
  2. develop
  3. news

When a dev change he will branch off develop such as issue123. When he finished changing, he will push to remote issue123. I will merge it into develop after reviewing the code. My problem is how i patch all changes which made from issue123 into news branch. I don't want to merge as develop, I just want to apply the changes.

Thanks

I have prepared the below image (remote repository): enter image description here

I want to merge issue123 to develop. It is easy task. However, I also want to patch the changes from E,F against C to news branch. Hope receive more help from Git experts.

War es hilfreich?

Lösung

A short term solution to your problem would be to use 'cherry-pick'

git checkout news
git cherry-pick E
git cherry-pick F

I would not recommend this in the long term though, as you cannot see where the code was merged from in the git tree.

Your underlying issue that is preventing you from using 'merge' is that there are commits on issue123 that have come from the develop branch which you don't want in the news branch. The solution is that issue123's starting branch point should have been common to new and develop branches (point A). If you branch issue123 from there then you can merge it into develop and news.

Maybe you need to work out a better branching strategy for your team though to make this work...try not to start a war ;)

Andere Tipps

Your branching model is similar to this one: http://nvie.com/posts/a-successful-git-branching-model/

So, everything shall finally make it's way into the develop branch.

IMO, you don't need the new branch at all: your devs branch off develop with

git checkout -b issue123 develop

and implement the fix. Once they're done, they push issue123 somewhere and ask you to review the patch:

git push origin issue123
git request-pull develop origin/issue123

Now, there's an issue123 branch on origin and your dev has a ready-made E-Mail that she can send you (the output of git request-pull).

You do

git fetch origin
git checkout -b review/issue123 origin/issue123

You review the patch and decide it's good. Next would be (while being on review/issue123) to

git rebase develop

Now, review/issue123 could be simply merged as fast-forward merge into develop:

git checkout develop
git merge review/issue123
git push origin devlop

The develop branch on origin is now updated, go ahead and clean up what's no longer needed:

git push origin :issue123 # delete the original issue123 branch on origin
git branch -d review/issue123 # delete the local branch since it's now in develop

This answer is here to show you how to handle the news branch as requested. You didn't specify the purpose of the news branch until recently in a comment to the other answer. Basically your new branch seems to get new features that aren't present in the development branch.

You may want to rethink your workflow carefully as it's typically much easier to have just one development branch, usually called master, which gets all new development when it's ready. Then you have short lived feature branches whose purpose is just to track a handful of patches to be applied to the master branch. For stabilization purposes you can create your stable branches e.g. named after the stable version number excluding the last part. Then purpose of each brach is clear and you avoid confusion.

Get the issue branch:

git checkout $ISSUE_BRANCH

Rebase the changes on the state of the news branch:

git rebase news

But you may have to tweak the rebase a little bit to only apply changes you want to. The easiest way to do that is an interactive rebase which allows you to delete the commits you're not interested in.

git rebase -i news

You could also read the git-rebase manual page carefully and learn to rebase exact ranges of commits. But if you don't which commits to use to specify the range, the above command is much easier to work with.

If confict arises, fix it and use:

git add -u
git rebase --continue

When finished, fast forward the news branch to include the commits in the issue branch:

git checkout news
git merge --ff-only $ISSUE_BRANCH

Delete the issue branch in case you would want to work with it again:

git branch -d $ISSUE_BRANCH

Note that there are probably other ways, sometimes quicker to write, but this should be a rather clean way to do what you requested.

This way you get the same changeset applied to news (whose purpose is doubtful to me), but the commit objects will be of course different.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top