KansaiRobot
KansaiRobot

Reputation: 9912

How to apply the same fix to two separate branches with git

This is a question about a particular situation I have found when working with git.

Say I have an application with a master branch (several commits of course). The application is working.

Then I decide to (for example- the actual thing is not that important) translate the comments in the code so I create a branch called translation and I start to translate everything. I commit several times.

Then I realize there is a bug (unrelated with translation). I can of course fix the bug but how is the correct way to proceed with git?

(take into account that this fix has to go to both the master version and the translated version, but the master does not have to have anything translated (so no merging I guess??)

I can think of two(2) possibilities:

1) go to master, create a fix branch, fix the bug and commit. Then go to translation and.... ooops... I don't know how to "copy" that fix there too

2) Correct in translation and merge?? (but then again, I can't merge the translation part??)

Upvotes: 2

Views: 580

Answers (4)

Nicolas Voron
Nicolas Voron

Reputation: 2996

The 1) is an hotfix strategy. You commit your patch in master and then cherry-pick that commit wherever you want/need.

The 2) is clearly not git oriented and error-prone

There is an existing 3) alternative. You can inject your fix into translation's history. How ? Your translation is based on master, right ? so you can commit on master and then rebase your translation branch on top of master. It will be like the fix was already included when you branched translation. Drawback : this will rewrite translation's history, so it's better if tranlation is a local branch. If it's not, you will have to force push your branch, which is far from ideal (if you don't know exactly what you are doing). EDIT Drawback 2 : In addition to that, 3) implies to include maybe other commits that you have inserted in the master branch history before your fix (which could be unwanted in the translation branch).

Upvotes: 1

jthill
jthill

Reputation: 60255

Try this method:

  1. Check out the commit that introduced the bug, however far back that is

    git checkout -b fixthatbug $thatcommit
    
  2. fix the bug and commit it
  3. merge the bugfix wherever it needs merging

Upvotes: 0

shirakia
shirakia

Reputation: 2409

The best way is like 1)

go to master, create a fix branch, fix the bug and commit. Then merte it into master, and merge master into translation branch

If you need only that fix for translation branch, you can cherry-pick the commit(s) into your branch.

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520998

I recommend your first suggestion, namely just going back to master and fixing the bug there. This assumes that the bug has nothing to do with translation of comments, which it probably does not.

Then, you may rebase your translation branch on master, to bring in the bug fix, before you get ready to merge this feature branch back to master. You could get lots of merge conflicts because all the translations changed. But, in that case, you might also get the same conflicts even if you were to completely ignore the bug fix.

Upvotes: 0

Related Questions