2

I'm aware that an exceedingly similar-sounding question has already been posted here:

Updating a local repository with changes from a github repository

However, my question was not answered by that page. In essence, I updated my GitHub repo with some new files and I edited some old ones on a machine. Now, I have an older version on another machine. How do I update the older version?

This was my error message. I ran sudo git pull origin master.

error: Your local changes to the following files would be overwritten by merge:
    test.py
Please commit your changes or stash them before you merge.
error: The following untracked working tree files would be overwritten by merge:
    README.txt
Please move or remove them before you merge.
Aborting

Then I ran

git commit -m "idk this is old but i have to commit n e way even though i just want to update from master?"

Since I was supposed to commit my changes or "stash" them which I have not learned about yet. In addition, I did not know what to do about README.txt so I just left it for now.

[master 16b204e] idk this is old but i have to commit n e way even though i just want to update from master?
 3 files changed, 11 insertions(+), 3 deletions(-)
 create mode 100644 README
 create mode 100644 README.txt

I ran

sudo git pull origin master

again but I got this error:

From https://github.com/bobhob314/fright-before-christmas-clone
 * branch            master     -> FETCH_HEAD
Auto-merging test.py
CONFLICT (content): Merge conflict in test.py
Automatic merge failed; fix conflicts and then commit the result.

I want to know why this happened. Is this a possibility? I don't recall if this is exactly what took place.

  1. I had version 1 on machine A. I pushed version 1 to GitHub.
  2. I cloned version 1 from GitHub to machine B. I edited the local repo and pushed it to GitHub, rewriting version 1. We'll call this version 2. Now I have version 2 on GitHub and the local repo on machine B.
  3. I edited version 1 on machine A by accident to "version 3". (The step that I assume caused the error)
  4. I tried to pull version 2 from GitHub to machine A's version 3 and it didn't work.

I don't exactly know the under-the-hood mechanics of git, so please help me figure out what went wrong.

Thanks!

Jeeter
  • 5,887
  • 6
  • 44
  • 67
Jack Pan
  • 1,261
  • 3
  • 11
  • 12

3 Answers3

4

You have what's called a "Merge Conflict"

Essentially, you have a file which has been modified by 2 different sources in 2 different ways, and Github doesn't know which file is the version you want, since they have changes that conflict with each other.

To put this in context of what you described, your "version 2" and "version 3" both changed the same lines of test.py, and now Github is unsure of which version to use.

The way to fix this is to go into the affected files (in this case, test.py) and fix the conflicts. You'll see something similar to this:

>>>>>>>>>>> HEAD
<code that's on the server (version 2)>
======
<code that you modified locally (version 3)>
<<<<<<<<<<< [commit hash]

Once you fix that, you can create the merge commit and push it back up


@TimeSheep posted a link to a great SO question here on how to resolve merge conflicts, which says you can install/use tools like mergetool to help you resolve these conflicts

Jeeter
  • 5,887
  • 6
  • 44
  • 67
4

Merge issues, I hate them....

Fast way: you are going to override your local changes in your oldest code machine.

git fetch --all
git reset --hard origin/master

that is all dude!

1

You are correct in that step 3 caused the error. This is a merge conflict, which means that git does not know how to produce a file that contains the changes in both version 2 and version 3.

What you need to do is resolve this conflict. There are several great tools which can help you do this. I recommend looking into GitKraken (conflict handling is a paid feature, free for students), KDiff or P4Merge (Which is for Perforce Helix, but supports git as well). You may already have a good tool installed, in which case you can start solving the conflicts by simply running git mergetool.

You can also resolve the conflicts by using a simple text editor, as recommended in another answer, but if you made a lot of changes, it can be a big help to use a GUI for the job.

If you want more information on resolving merge conflicts, I recommend looking at this question: How to resolve merge conflicts in Git?

Steen Schütt
  • 1,355
  • 1
  • 17
  • 31