views:

47

answers:

2

I've just started using Git and find that whilst I am implementing a feature in a branch, I will encounter some bug that really needs to be pushed to the trunk as soon as possible. In order to do so, I use checkout to switch back to the trunk, make my changes and commit them. I now have a trunk that is bug free.

Unfortunately, my branch needs to have this bug fixed as well. As the feature is not complete, I can't just merge the branch back into the trunk. How can I alter my branch so that it receives the changes I made to the trunk?

If it matters, I am developing on my own and so only have a single repository to worry about.

I am using TortoiseGit so instructions specific to that would be helpful but not necessary.

+4  A: 

Make sure you have your branch checked out (git checkout branch-name) and run

git rebase master

And resolve any conflicts that arrive.

If you aren't sure what these commands do, try not using TortoiseGit and use the terminal. It will help you really understand the commands.

WARNING: This assumes a local branch. If you have shared the branch, do not run the rebase (because it modifies history). Run

git merge master

while you are on your other branch. This has less clean history, but can be used.

The difference is:

  • Rebase - rewrites the branch ontop of master, replaying all the changes
  • Merge - a normal merge, creating a commit with two parents
mathepic
This will copy all the changes that are in master and not your branch to your branch. It will also re-apply your branch changes so that they come after the changes in master in the repository history.
Sean Carpenter
Yeah, isn't that the desired behavior? I guess you could do a cherry-pick, but that should be avoided if possible. It depends on if you have any commits on master that you don't want in the other branch. However, why would you have those commits in master, anyway? Master should generally be stable.
mathepic
In the general case, you should indeed merge, but you should merge a bugfix branch rather than master directly. You can create the bugfix branch from the common ancestor of all branches which need it, then merge it into all of them (in this case, master and feature). This avoids unnecessarily merging *other* content from master into your feature branch.
Jefromi
Usually it shouldn't matter if you merge _other_ content. If it does, then yes, if the bugfix is on its own branch you either rebase on top of bugfix or merge bugfix.
mathepic
@mathepic: It's just a bad idea in principle to merge downstream, getting unnecessary changes into a branch whose purpose is purely to develop a given feature. See for example http://gitster.livejournal.com/42247.html
Jefromi
@Jefromi and that is why rebase is usually a better idea, you do it just to make sure that all your changes apply. All it does is make it appear that you forked the branch at the latest possible point.
mathepic
@mathepic: I understand what a rebase does, and that's precisely why in general the best idea is to start branches from where they should be started, which means rebasing is not necessarily a good general principle. Branches need to start from a common ancestor of everything they'll eventually be merged into. Sometimes this is compatible with a rebase, sometimes not. Suppose you're working on a branch which ultimately will be merged into both master and a maintenance branch. Your branch cannot be rebased forward; it must remain based on a common ancestor of master and the maintenance branch.
Jefromi
@mathepic: To phrase that in terms of what Junio said in the post I linked to, when you merge foo into bar, OR when you rebase bar onto foo, you are stating that it is desirable to have *all* contents of foo in bar. This is why the right answer is to create the bugfix branch in the right place in the first place, so that when you merge or rebase, all you are gaining is the bugfix, nothing else.
Jefromi
@Jefromi sigh. You have a good method, I have a good method. I just see no reason to not rebase topic branches that you have branched off master. I do that in git.git all the time.
mathepic
@Jefromi To explain my reasoning - When I create a create a topic branch, I typically base off the head of master. Before I send off the patch to the git.git mailing list, I typically rebase master to make sure that my patch applies cleanly and so that I can test to make sure that the patch works with the latest master.
mathepic
+1  A: 

If your repository is not available to anyone else, then "git rebase master" as suggested above will work.

If your repository is available publicly, you really don't want to rebase since it may screw up others who have cloned your repository. In that case, you want to use "git merge master" to merge the changes from trunk into your feature branch.

Wade Williams