Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send a patch to another developer and avoid merge conflicts?

How do I get a patch from a commit in order to send it to another developer? And how do I best avoid a merge conflict with this patch when merging our trees at a later date?

If you know how please explain how to do this in your VCS of choice such as subversion, git, Mercurial, bzr or etc.

like image 800
Spoike Avatar asked Nov 16 '08 10:11

Spoike


People also ask

How do I resolve a patch conflict?

If you are frequently running into the same conflict set when applying patches, rebasing or merging then you can use git rerere (reuse recorded resolution) function. This allows you to pre-define how conflicts should be resolved based on how you resolved them in the past.

How do you avoid a merge conflict in git?

Currently, there are visible conflicts that need to be resolved manually. If you want to skip this commit, you can type git rebase --skip, or if you want to abort this rebase, you can type git rebase --abort. After managing this conflict manually, we will open the merge tool.


1 Answers

In git you can pipe the output of git-diff between two commits like this:

git diff fa1afe1 deadbeef > patch.diff

Send the patch.diff to the developer and let him git-apply it to his workspace like this:

git apply patch.diff

If the other developer already has the commits available in his repository he could always pipe it in himself without merging like this:

git apply < git diff fa1afe1 deadbeef

You can then add and commit the changes in the diff the usual way.


Now here comes the interesting part when you have to merge the patch back to the master branch (that is public). Consider the following revision tree where C* is the applied patch from C in the master branch:

A---B---C---D          master, public/master
     \
      E---C*---F       feature_foo

You can use git-rebase to update the topic branch (in this example named feature_foo) with it's upstream head. What that means is when you type in the following:

git rebase master feature_foo

Git will rearrange the revision tree like this and will also apply the patch itself:

A---B---C---D          master, public/master
             \
              E*---F*  feature_foo

Merging to the upstream branch will now be an easy fast-forward merge. Also check that the new commits E* and F* work as the previous E and F respectively.

You can do the same thing against another developer's branch using the same steps but instead of doing it on a public repo, you'll be fetching revisions from the developer's repository. This way you won't have to ask the other developer for a patch if it is already available from what he published at his repo.

Please note to never rebase a public branch because the command will rewrite git history which is something you don't want to do on branches that people depend on and will create a mess when merging to remote repositories. Also never forget to integrate often so others in your team can take part of your changes.

like image 147
Spoike Avatar answered Sep 21 '22 08:09

Spoike