Conflicts

Overview

Teaching: 15 min
Exercises: 0 min
Questions
  • What do I do when my changes conflict with someone else’s?

Objectives
  • Explain what conflicts are and when they can occur.

  • Resolve conflicts resulting from a merge.

As soon as people can work in parallel, it’s likely someone’s going to step on someone else’s toes. This will even happen with a single person: if we are working on a piece of software on both our laptop and a computer in the lab, we could make different changes to each copy. Version control helps us manage these conflicts by giving us tools to resolve overlapping changes.

To see how we can resolve conflicts, we must first create one. The file mars.txt currently looks like this in both partners’ copies of our planets repository:

Cold and dry, but everything is my favorite color
The two moons may be a problem for Wolfman
But the Mummy will appreciate the lack of humidity

Let’s add a line to one partner’s copy only

Cold and dry, but everything is my favorite color
The two moons may be a problem for Wolfman
But the Mummy will appreciate the lack of humidity
This line added to Wolfman's copy

and then push the change to GitHub.

Now let’s have the other partner make a different change to their copy without updating from GitHub:

Cold and dry, but everything is my favorite color
The two moons may be a problem for Wolfman
But the Mummy will appreciate the lack of humidity
We added a different line in the other copy

We can commit the change locally:

Local commit

 

but Git won’t let us push it to GitHub:

Conflict

 

Git detects that the changes made in one copy overlap with those made in the other and stops us from trampling on our previous work.

The Conflicting Changes

What we have to do is pull the changes from GitHub, merge them into the copy we’re currently working in, and then push that. Let’s start by pulling:

Pulll

 

Git will tell us that there’s a merge conflict we need to resolve, and will show us where they are in the file(s).

Conflict alert

 

Conflict diff

 

Cold and dry, but everything is my favorite color
The two moons may be a problem for Wolfman
But the Mummy will appreciate the lack of humidity
<<<<<<< HEAD
We added a different line in the other copy
=======
This line added to Wolfman's copy
>>>>>>> dabb4c8c450e8475aee9b14b4383acc99f42af1d

Our change—the one in HEAD—is preceded by <<<<<<<. Git has then inserted ======= as a separator between the conflicting changes and marked the end of the content downloaded from GitHub with >>>>>>>. (The string of letters and digits after that marker identifies the commit we’ve just downloaded.)

It is now up to us to edit this file to remove these markers and reconcile the changes. We can do anything we want: keep the change made in the local repository, keep the change made in the remote repository, write something new to replace both, or get rid of the change entirely. Open the file in your text editor to make your changes

If The File Is Already Open

You might find that you already had the file open in a text editor before you pulled. In that case, you need to be very careful. Depending on your text editor, what you see in your window will likely not be updated even though the underlying file has changed on disk. If you save and close this file as is, you will overwrite (and lose) the changes, and lose your opportunity to resolve the merge conflict properly. To avoid this, you need to close the file without saving it and reopen it. If your open file contains changes that were not saved, you need to copy the contents of that file to a new, temporary file before you close and reopen this one.

In our example, we are keeping both changes:

File in Editor

 

Fixed in Editor

 

To finish merging, we add mars.txt to the changes being made by the merge and then commit:

Commit Merge

 

Now we can push our changes to GitHub.

Git keeps track of what we’ve merged with what, so we don’t have to fix things by hand again when the collaborator who made the first change pulls again.

Git’s ability to resolve conflicts is very useful, but conflict resolution costs time and effort, and can introduce errors if conflicts are not resolved correctly. If you find yourself resolving a lot of conflicts in a project, consider these technical approaches to reducing them:

Conflicts can also be minimized with project management strategies:

A Typical Work Session

You sit down at your computer to work on a shared project that is tracked in a remote Git repository. During your work session, you take the following actions, but not in this order:

  • Make changes by appending the number 100 to a text file numbers.txt
  • Update remote repository to match the local repository
  • Celebrate your success with beer(s)
  • Update local repository to match the remote repository
  • Review changes to be committed
  • Commit changes to the local repository

In what order should you perform these actions to minimize the chances of conflicts? Put the commands above in order in the action column of the table below. When you have the order right, see if you can write the corresponding commands in the command column. A few steps are populated to get you started.

order action . . . . . . . . . . command . . . . . . . . . .
1    
2    
3    
4    
5    
6 Celebrate!  

Solution

order action . . . . . . command . . . . . . . . . . . . . . . . . . .
1 Update local File >> Pull
2 Make changes Edit the file in your text editor
3 Review changes Look at the staged changes
4 Commit changes Write commit message and click “commit”
5 Update remote File >> Push
6 Celebrate! beer.emoji

Key Points

  • Conflicts occur when two or more people change the same file(s) at the same time.

  • The version control system does not allow people to overwrite each other’s changes blindly, but highlights conflicts so that they can be resolved.