Resolving merge conflicts in Git

From genomewiki
Revision as of 00:51, 2 March 2011 by Marygoldman (talk | contribs) (adding a common questions section)
Jump to navigationJump to search

Two ways git merge/git pull can fail

There are 2 ways in which git merge (or a git pull, which is a git fetch and then a git merge) can fail:

  • Git can fail to even start the merge. This occurs because git knows there are changes in either your working directory or staging area that could be written over by the files that you are merging in. The error message is as follows:
error: Entry '<fileName>' not uptodate. Cannot merge. (Changes in working directory)

or

error: Entry '<fileName>' would be overwritten by merge. Cannot merge. (Changes in staging area) 


  • Git can fail during the merge. This occurs because you have committed changes that are in conflict with someone else's committed changes. The error message is as follows:
CONFLICT (content): Merge conflict in <fileName>
Automatic merge failed; fix conflicts and then commit the result.

Common questions

How do I find conflicts in a file?

Conflicts are marked in a file with clear line breaks

 <<<<<<< HEAD:mergetest
 This is my third line
 =======
 This is a fourth line I am adding
 >>>>>>> 4e2b407f501b68f8588aa645acafffa0224b9b78:mergetest

<<<<<<<: Indicates the start of the lines that the user currently has committed and start of lines involved in merge conflict

>>>>>>>: Indicates the end of the lines that are trying to be merged into the user's tree and the end of lines involved in merge conflict

=======: Indicates the break point used for comparison. Breaks up changes that user has committed (above) to changes coming from merge (below) to visually see the differences.

Tools to help you resolve a merge conflict

Tools and example output when in a merge conflict situation.

git status

The git status command provides an overview of all files that have been modified and are in conflict at the time of the merge.


Example:

   # Changes to be committed:
   #   (use "git reset HEAD <file>..." to unstage)
   #
   #	modified:   <Some file>
   #
   # Changed but not updated:
   #   (use "git add <file>..." to update what will be committed)
   #   (use "git checkout -- <file>..." to discard changes in working directory)
   #
   #	unmerged:   <file>
   #
  • "Changes to be committed": All changes to files that are not affected by the conflict are staged.
  • "Changes but not updated": All files that have conflicts that must be resolved before repository will be back to working order.

git stash

Stashes away any changes in your staging area and working directory. This command is useful in saving all changes not ready to be committed and the user wants to have an updated repository.

git stash save "<Save Message>": Save changes to files in working directory and staging area that git is aware of

 git stash save "Saved changes for stash example"
 Saved working directory and index state "On master: Saved changes for stash example"
 HEAD is now at 4e2b407 Added second file for example.

git stash list: List the stashes that you currently saved

git stash list
stash@{0}: On master: Saved changes for stash example

git stash pop <stash@{#}>: Removes the most recent stash or any stash specified and applies changes as a merge. If merge fails the stash is not removed from the list and must be removed manually.

git stash apply <stash@{#}>: Applies changes of recent stash or any stash is list specified, but doesn't remove it from the list.

git stash drop <stash@{#}>: Removes the most recent stash or any stash specified from the list.

git stash clear: Removes all stash's stored in the list



Scenarios

Git refuses to start a merge/pull

Error Messages:

  1. error: Entry '<fileName>' not uptodate. Cannot merge. (Changes in working directory)
  2. error: Entry '<fileName>' would be overwritten by merge. Cannot merge. (Changes staged, but not commited)

Steps toward Resolution:

  1. git stash save "<Message that describes what is being Saved>" (Stashes away any changes in your staging area and working directory in a separate index.)
  2. git status ( Verify all changes are staged)
  3. git stash list (Lists all stash's user has)
  4. git pull or git merge (Bring in changes from central repository or another branch)
  5. git stash pop (Will repopulate your changes into your working directory, maybe have to resolve merge conflicts)

Git is unable to resolve a merge/pull

Error Message:

CONFLICT (content): Merge conflict in <fileName>
Automatic merge failed; fix conflicts and then commit the result.

Steps toward Resolution:

  1. git status (Shows all files that are in conflict as unmerged changed in working directory.)
  2. Resolve Merge confilicts
  3. git add <files>
  4. git commit -m "<Informative commit message>"