54

I'm running into conflicts while trying to merge upstream changes back into my branch and I'm not sure how to resolve them.

I created my own fork. I cloned it. I made changes to the branch on my fork, committed, and pushed. But then the main fork updated, and I tried to update my own fork by merging upstream in like so:

$ cd repo-name
$ git remote add upstream git://github.com/username/repo-name.git
$ git fetch upstream
$ git merge upstream/master

The merge says that there's some problem with a file and auto-merging doesn't work. It tells me to fix it myself and re-merge. So I actually went to the (upstream) repository on GitHub of the main fork and copied all the code of the new file into the file on my fork, and tried to merge again. Then, git gives me this error:

fatal: 'merge' is not possible because you have unmerged files. Please, fix them up in the work tree, and then use 'git add/rm ' as appropriate to mark resolution and make a commit, or use 'git commit -a'.

Is there some argument I'm leaving out? Am I doing something stupid? What does it mean by "unmerged files?" Isn't the whole point of merging to merge files? Do I have to commit my changes before I merge?

Community
  • 1
  • 1
anonymous
  • 551
  • 1
  • 4
  • 5
  • 1
    Honest advice: I suggest you read a tutorial on VCS and merging in general. The questions and responses you gave make me worry quite a bit whether you'll get it right – sehe Apr 24 '11 at 20:20
  • 1
    How to resolve conflicts: http://www.kernel.org/pub/software/scm/git/docs/git-merge.html#_how_to_resolve_conflicts – Josh Lee Apr 24 '11 at 20:25
  • 1
    ProGit seems to be a pretty popular online book on Git; I would also recommend Git from the Bottom Up. – alternative Apr 24 '11 at 20:34

5 Answers5

44

What you are seeing means that automatic merge could not resolve the conflicts in the files. You need to resolve these conflicts manually. Run git mergetool or git gui.

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
  • 1
    OK just ran Git GUI. I've never used it before and when I opened up my local repository. After I run git GUI what do I do? – anonymous Apr 24 '11 at 20:06
  • In the `git gui` you will see the conflicting files. You can inspect and resolve them directly from the `git gui`. Or if you don't like the `git gui`, simply run the `mergetool` to resolve the collisions. – Šimon Tóth Apr 24 '11 at 20:10
  • 3
    OK i prefer mergetool. So it shows me each conflict, does it automatically fix the conflict? Can you change how it fixes it? Or do I still have to manually fix it? – anonymous Apr 24 '11 at 20:12
  • @anonymous Mergetool just runs the program you selected, the behaviour depends on the program. – Šimon Tóth Apr 24 '11 at 20:14
  • when I run mergetool it tells me to hit to open up "merge resolution tool" (tortoisemerge) And then I open tortoisemerge and look at all the differences. Does it automatically merge them for me? – anonymous Apr 24 '11 at 20:16
  • @anonymous Well, yes tortoisemerge is your resolution tool. You can change that. And when you hit enter, it will run tortoisemerge inside which you will be able to resolve the conflict. – Šimon Tóth Apr 24 '11 at 20:17
  • OH I think I see- then I can right-click on "theirs" and choose to use the whole file then save the file. Right? – anonymous Apr 24 '11 at 20:19
  • @anonymous I have no idea how tortoisemerge works. But what you are describing sounds reasonable (if you want to erase your local changes). – Šimon Tóth Apr 24 '11 at 20:20
  • Yes, it worked. In tortoisemerge, there are two main sections: "Theirs" and "Mine". It shows the difference between the two. At the bottom, there is a file called "Merged." It shows what the current file is. You can right-click the "theirs" side and choose something like "Use whole text" and it will use all of "theirs" and resolve the problem. Then I can commit and merge! – anonymous Apr 24 '11 at 20:26
  • run git mergetool... then what? Its freaky. – Morkrom May 31 '13 at 20:40
  • I had a similar problem, I would recommend you having a look at how to merge files very nicely described here (helped me a lot): https://stackoverflow.com/questions/161813/how-to-resolve-merge-conflicts-in-git?page=1&tab=votes#tab-top – My Work Apr 08 '20 at 11:57
12

The "git merge" command tries to incorporate changes from another branch onto the present branch. If the merge is clean, meaning no conflicts, it will commit. Since your merge did have conflicts, it didn't commit. You need to resolve the conflict.

Pulling the copy from the upstream repo is one way to do that - by accepting the upstream repo's version. You can do that within git using "git checkout --theirs conflicting_file.txt"

Editing the file to get it into the shape you want is another way.

Once it's fixed, you need to add using "git add conflicting_file.txt" then commit. Then your working copy is clean and ready for more hacking. Good luck.

cbare
  • 12,060
  • 8
  • 56
  • 63
  • OK, (the project I'm working on is in Java) so if the conflicting file was called "blah.java" I would run "git checkout --theirs blah.java"? And I don't understand the whole "add" thing. Why would I need to "add" if I've fixed a conflict? – anonymous Apr 24 '11 at 20:15
  • 1
    That's just like any edit within git. You first edit the file in your working copy. You then add (git add blah.java), which tells git that the file (blah.java) will be part of the next commit. Then actually do the commit. The difference with a merge is that some files, those with no conflicts, will already have been added. You add the remaining files to show that you've resolved the conflicts. – cbare Apr 25 '11 at 00:35
9

In Git there are cases merge refuses to even start in order to protect your local changes. This may happen in two cases:

  • You have uncommitted changes in you repository that conflict with merge. The git will refuse to do a merge with the following message:

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

    Then you have to either commit the changes first (git commit -a or git add + git commit), or stash them away with git stash save.

  • You are in the middle of some unfinished merge-y operation. There was some conflict, for example

    Auto-merging foo
    CONFLICT (content): Merge conflict in foo
    Automatic merge failed; fix conflicts and then commit the result.
    

    and you have not finished resolving conflicts (by editing files and marking them as resolved with git add, or using some graphical merge tool via git mergetool) and didn't create a final merge commit with git commit -a, or aborted the merge with git reset --hard (NOTE: this will discard all you changes, and you will loose work done on resolving conflicts!!!).

    Or you have just run second git merge too fast, or used git merge instead of git commit to create a merge commit.

    error: 'merge' is not possible because you have unmerged files.
    hint: Fix them up in the work tree,
    hint: and then use 'git add/rm ' as
    hint: appropriate to mark resolution and make a commit,
    hint: or use 'git commit -a'.
    fatal: Exiting because of an unresolved conflict.
    

    Resolve conflicts as described e.g. in old Fun with completing a merge article by Junio C Hamano and finalize a merge with git commit, or discard a merge, or stash it away. Then if you meant to create this second merge, you can do it.

Sidenote: by default git-aware shell prompt shows if you are in the middle of merge, rebase or applying patches (git am operation). You can also configure it to show if the working directory is dirty (different from latest version, i.e. HEAD).

Jakub Narębski
  • 309,089
  • 65
  • 217
  • 230
3

Run git commit (after adding the files) the second time, not git merge.

Also the conflict resolution will create files to help you merge. See also git mergetool.

alternative
  • 12,703
  • 5
  • 41
  • 41
2

After you've resolved a merge, you need to use git add to add the files you've changed to the index, and then commit (like the message says). This says to git "Yes, I really do want to make these changes".

Remember, always use git add before committing (either normally or committing a merge), if you're using the command line interface. Frontends like magit can streamline this for you so you don't have to worry about typing "git add" every time.

Robin Green
  • 32,079
  • 16
  • 104
  • 187
  • OK I guess that makes sense. Simply "git add?" Or would "git add -a" work? :P nope didn't work. I have to add each changed file manually? – anonymous Apr 24 '11 at 20:08