1

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.

teddy
  • 197
  • 2
  • 15
  • How can you be sure that the changes made in the `develop` issue branch will work in the current `news` branch? How often are `master` and `news` updated? – Jason Mar 31 '14 at 02:04
  • How is the `news` branch related to the other branches? –  Mar 31 '14 at 02:05
  • Keep all changes local, do the changes locally. You'll probably want to either merge the "official" branches into news, or rebase news on one of the others. – vonbrand Mar 31 '14 at 02:21
  • 1
    You can use `git merge --squash` for this. See http://stackoverflow.com/questions/5308816/how-to-use-git-merge-squash – Michael Anderson Mar 31 '14 at 02:28
  • @Jason Work or doesn't work isn't the scope of the question. I will have a method to verify. I must give more information. news branch same as develop but it has new features we want to deploy to production in the future. I want to keep all new features add to develop available on news too.master is same as the source code I deployed in production server. It is updated about every 2 weeks and whenever there is a hotfix. – teddy Mar 31 '14 at 02:34
  • @MichaelAnderson I will try your suggestion. Rebase is nearly feature but it works base on local commit. – teddy Mar 31 '14 at 02:37
  • Merge squash isn't solution for my problem – teddy Mar 31 '14 at 04:59

3 Answers3

1

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 ;)

danelips
  • 774
  • 6
  • 5
0

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.

Pavel Šimerda
  • 5,783
  • 1
  • 31
  • 31
  • 1
    Thanks @Pavel Simerda. When I try your help. I stuck at the second step: git rebase news. It isn't apply only changes from E,F against C. It apply old changes than C. – teddy Mar 31 '14 at 07:16
  • Good point. You may have to tweak the rebase a bit. I'll try to amend the answer accordingly. – Pavel Šimerda Mar 31 '14 at 08:08
0

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
eckes
  • 64,417
  • 29
  • 168
  • 201
  • 1
    Thank you for your comment. However, news is parallel developing with develop branch. It has some features which aren't included in develop branch. – teddy Mar 31 '14 at 08:22
  • @teddy: in that case, just `git checkout news && git merge develop` after you merged the fix into `develop`. – eckes Mar 31 '14 at 08:32
  • @eckes that may or may not be what he wants. But you're right that when merging you have to merge the *upstream* to the *downstream* if I can call them so, or the *stable* branch to the *testing* branch, provided that updates intended for both are first applied to stable. But the rebase workflow is often used instead. – Pavel Šimerda Mar 31 '14 at 14:19