Question

I created a file, file.txt, and then added one line with my commit to the master branch with the default text

"-----Text--------"

Another developer on the repository, MrA, cloned the source and edited his work like this:

"-----Text--------"
Line added by MrA;

MrA commits to the remote repository in the server.

At the same time MrB also modified the same file like this

"-----Text--------"
Line added by MrB;

When MrA commits there's no problem since there's no other changes before his.

But when MrB wants to commit it has an message error saying there is a conflict. When MrB tries to pull he gets an error message that you move file.txt or stash it to merge the content.

What should I do to resolve this conflict?

Was it helpful?

Solution

Both MrA and MrB edited the same part of the file, hence the merge conflict.

I'm guessing you are getting this message when pulling:

error: Your local changes to the following files would be overwritten by merge:
        file.txt
Please, commit your changes or stash them before you can merge.
Aborting

Solution (Steps MrB should do)

  1. Do git commit. This step should not fail. Although he could stash, I don't recommend it.
  2. Do a git pull. This will result in the merge conflict, as expected.
  3. Use git mergetool to bring up a GUI to aid in merging. Alternatively, just open the file up and edit it by hand to what you want. Git uses a special syntax to denote where the conflict is. See my link on branching and merging.
  4. Do git add on the file.
  5. Do git commit and now you've finished resolving the merge.

Reference material


As for git stash, This will save all of your modified tracked files and staged changes. It essentially makes your copy of the text file unmodified. It stores the changes you made internally, but without committing. So when you pull, there is no conflict. However as @nneonneo points out, when you try to recover your changes with git stash pop, you will get a conflict. I don't recommend this approach.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top