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 server 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 countATCG.py currently looks like this in both partners’ copies of our gitcollab_sls repository:

$ cat countATCG.py
# Load the fasta file into script
# Function to remove headers from file
# Function to count the number of ATCGs in sequence

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

$ nano countATCG.py
$ cat countATCG.py
# Load the fasta file into script
# Function to remove headers from file
# Function to count the number of ATCGs in sequence
# Write output to file

and then push the change to GitHub:

$ git add countATCG.py
$ git commit -m "Adding a line in our home copy"
[master 5ae9631] Adding a line in our home copy
 1 file changed, 1 insertion(+)
$ git push origin master
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 352 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://github.com/vlad/gitcollab_sls
   29aba7c..dabb4c8  master -> master

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

$ nano countATCG.py
$ cat countATCG.py
# Load the fasta file into script
# Function to remove headers from file
# Function to count the number of ATCGs in sequence
# Saving output to file

We can commit the change locally:

$ git add countATCG.py
$ git commit -m "Adding a line in my copy"
[master 07ebc69] Adding a line in my copy
 1 file changed, 1 insertion(+)

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

$ git push origin master
To https://github.com/vlad/gitcollab_sls.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/vlad/gitcollab_sls.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

The Conflicting Changes

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. 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:

$ git pull origin master
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 3 (delta 1)
Unpacking objects: 100% (3/3), done.
From https://github.com/vlad/gitcollab_sls
 * branch            master     -> FETCH_HEAD
Auto-merging countATCG.py
CONFLICT (content): Merge conflict in countATCG.py
Automatic merge failed; fix conflicts and then commit the result.

git pull tells us there’s a conflict, and marks that conflict in the affected file:

$ cat countATCG.py
# Load the fasta file into script
# Function to remove headers from file
# Function to count the number of ATCGs in sequence
<<<<<<< HEAD
# Saving output to file
=======
# Write output to file
>>>>>>> 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. Let’s replace both so that the file looks like this:

$ cat countATCG.py
# Load the fasta file into script
# Function to remove headers from file
# Function to count the number of ATCGs in sequence
# Saving/Writing output to file

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

$ git add countATCG.py
$ git status
On branch master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:

	modified:   countATCG.py

$ git commit -m "Merging changes from GitHub"
[master 2abf2b1] Merging changes from GitHub

Now we can push our changes to GitHub:

$ git push origin master
Counting objects: 10, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 697 bytes, done.
Total 6 (delta 2), reused 0 (delta 0)
To https://github.com/vlad/gitcollab_sls.git
   dabb4c8..2abf2b1  master -> master

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 pull origin master
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 2), reused 6 (delta 2)
Unpacking objects: 100% (6/6), done.
From https://github.com/vlad/gitcollab_sls
 * branch            master     -> FETCH_HEAD
Updating dabb4c8..2abf2b1
Fast-forward
 countATCG.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

We get the merged file:

$ cat countATCG.py
# Load the fasta file into script
# Function to remove headers from file
# Function to count the number of ATCGs in sequence
# Saving/Writing output to file

We don’t need to merge again because Git knows someone has already done that.

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 one of these approaches to reducing them:

Solving Conflicts that You Create

Clone the repository created by your instructor. Add a new file to it, and modify an existing file (your instructor will tell you which one). When asked by your instructor, pull her changes from the repository to create a conflict, then resolve it.

Conflicts on Non-textual files

What does Git do when there is a conflict in an image or some other non-textual file that is stored in version control?

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:

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   echo 100 >> numbers.txt
3    
4    
5    
6 Celebrate! AFK

Key Points