-
Fixing merge conflicts
Branching is great. Being able to work on code that you don’t have to check in, that doesn’t risk polluting the main branch until it’s finished is great; but you have to be able to bring the changes back in at some stage. One way is to use
git merge. Here’s a nice contrived example of a simple repository containing two files. Greetings.txt and Partings.txt. There are three branches; master, es and fr. The currently checked out branch is es so the HEAD reference points to that. Can you spot the deliberate mistakes in the contents of the files?
I want to merge the changes from the fr branch into the es branch and since I am on the es branch already, I use the command
git merge fr, and that’s when the problems start. It looks like the merge didn’t work and has left me with conflicts.
Conflicts are an unavoidable fact of working with merges in any version control system. If there has been a change to the same line in both branches then git does not know which one to apply. Although it’s very good at interleaving changes to different lines in files, there is nothing magical about git: it isn’t going to risk corrupting my files in cases where there is a conflict. So, that means that they need to be fixed by hand. Before we look at ways of fixing these commits, let’s take a look at the state of our repository now.
Examining the conflicts
Run
git statusto see the state of your working directory.
Here you can see that the two files are in the working directory, but not in the index. These files have been modified by git with familiar conflict markers.

Notice that it has marked the changes to the HEAD and fr branches in each case. What this doesn’t show you are the hidden references to the state of the files at each branch. This is what your repository looks like now:

Quick and dirty resolution
For small files like this the fixes are quite simple:
- Search for the conflict markers in each file.
- Edit the file to the state that you want, and remember to remove the conflict markers.
- Add the changed files to the index with
git add. - Once all the conflicted files are fixed, commit them with
git commit. - Edit the commit message to show that I’ve merged the files and fixed the conflicts.
Not so dirty resolution
In this case, I can see that files in one or the other branch should take priority. That is, changes in those files are the ones to be applied in conflicts. So here; I am on the es branch and I can see that I want my version of the Greetings.txt file to be in the merge, and the fr branch version of Partings.txt is the correct one. What I can do is to use the references to check out the version of the file that I want. So what I do here is:
- Get the es version of Greetings.txt with
git checkout --ours Greetings.txt. Note: I am using--oursbecause it is the one from the branch I am currently on. - Get the fr version of Partings.txt with
git checkout --theirs Partings.txt. Note: I am using--theirsbecause it is the one from the branch I am merging with. - Do a quick sanity check of the files; and I can see that the correct versions exist, and without the conflict markers.

- Add the files to the index with
git add . - Commit the changes with
git commit - Edit the commit message to show that I’ve merged the files and fixed the conflicts.
Resolved Merge
After resolving the conflicts and checking in the changes this is what the repository looks like.

You can see that there is a new commit with the correct versions of the files. Also, since this was a merge commit, this new commit has two parents which are the commits that it merged.
Summary
This is a contrived example but merge conflicts are a fact of life. Git doesn’t replace communication between team members and cannot read minds when deciding which changes to apply if there are conflicts. It’s also generally safe to merge branches because you will be asked to resolve conflicts rather than have changes applied under you that might mess up the project.
There are other ways of joining branches and dealing with the conflicts. If you can’t remember them all, the quick and dirty method is the one that you can always use.
Of course, if things become too much of mess, and there are too many conflicts to resolve all at once, you can always about a merge (before resolving and committing, of course) with
git reset --mergewhich will restore the repository to the state it was before you started the merge. You can then use other methods to get your project into shape before trying to merge changes.Posted on April 1, 2010 with 40 notes ()
-
lingeriedive reblogged this from 365git
-
brooklyndeckerswimsuit reblogged this from 365git
-
evamendes-hq reblogged this from 365git
-
kate-upton-hq reblogged this from 365git
-
bodyslanguage reblogged this from 365git
-
iosova likes this
-
mangalcun likes this
-
rosiode likes this
-
pixelsa likes this
-
chabad-chabad likes this
-
lanka-news-today likes this
-
galoshesandrainboots likes this
-
benign-mesothelioma likes this
-
fightthepower1z3 likes this
-
365git posted this