Git Merge Conflicts

ENV 872 - EDA   |   Spring 2024   |   Instructor: John Fay  |   

Here I demonstrate the repercussions of not following proper protocol when using Git, specifically a git merge conflict, a result of having divergent branches. You need to repeat these steps yourself, but rather following along to better understand the Git workflow, how things can go wrong, and how to fix them.

Step 1. Getting started

In this step, we execute the proper workflow for creating a simple document in RStudio and adding it to our local and remote Git repositories.

  1. Open up your RStudio project that is connected to your GitHub repo.
  2. Pull any new changes that might be in your remote repository to your local repository.
  3. Pull any changes in the upstream repository that might be new to your local repository.
  4. Create a new text document in RStudio and add some text to that document.
  5. Add (i.e. “stage”) this file to your local Git repo and the commit that change
  6. Push that change up to your remote GitHub repo.

You should now see the file you just created on your GitHub web page.

Step 2. Edit the file in the remote repository

Here we’ll do something that I implore you not do: make changes to files directly in your remote repository. Doing so just adds one more layer of complexity that you don’t need, not while learning Git for the first time.

  1. Open your remote repository on GitHub.com
  2. Navigate to the place where the file you created in Step 4 above lives.
  3. Edit that file, e.g., by clicking on the pencil icon.
  4. Commit those changes

This step mimics what might happen if you typically work on two or more local machines. We have a change on the remote repository that our local repository is not yet aware of. Typically, we’d just pull those changes to the local repo, and all would be good, but what if we forgot?

Step 3. Edit the local file without pulling first

Now, we’ll start down the path of doom: making edits to files without properly synchronizing edits across repositories. It’ll start out innocuously enough by making changes to our local file and the committing those changes to our local repository, but then we’ll also make changes to the same file in the remote repository and see what happens.

  1. In RStudio, make a few edits to your text file created in step 4 above and save the file.
  2. As expected, the Git tab in RStudio will indicate the file has been modified; stage and commit the changes to your local repository.
  3. Push those changes to the remote.

Here, you’ll get an error: your push has been rejected. Why? Because, Git doesn’t know how to reconcile the changes made on your remote repository and the changes you made to the same file in the local one.

Step 4. Resolving the issue

Git can’t reconcile the changes, so we must do it manually. Here’s how:

  1. Pull remote changes to the local repo. You’ll get a warning about a “Merge conflict”. You may also notice the icons in the Git tab of RStudio are an unfamiliar “U” for unmerged. We need to fix that…
  2. Open the text file in RStudio. You’ll see markings like <<<<<<< HEAD and >>>>>>> be7133be624a240c86d8fdc12e398c8de5311996. It’d between these markings that conflicting changes occur. Notice that changes to your remote and to your local file are shown here.
  3. Accept one set of change and then remove the <<<<… and >>>>… lines, and save the file.
  4. Stage the changes (the U’s should change to an ` M`) and then commit them.
  5. Push the changes. Now the two repositories are synchronized again