Resolving merge conflicts in Git: Difference between revisions

From genomewiki
Jump to navigationJump to search
(Adding link info to a repository to test having a merge conflict.)
 
(40 intermediate revisions by 4 users not shown)
Line 3: Line 3:
There are 2 ways in which git merge (or a git pull, which is a git fetch and then a git merge) 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:
===Git can fail to 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. If this happens, there are no merge conflicts in individual files. You need to modify or stash the files it lists and then try to do a git pull again. The error messages are as follows:


  error: Entry '<fileName>' not uptodate. Cannot merge. (Changes in working directory)
  error: Entry '<fileName>' not uptodate. Cannot merge. (Changes in working directory)
Line 9: Line 10:
or
or


  error: Entry '<fileName>' would be overwritten by merge. Cannot merge. (Changes in staging area)  
  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. Git will do its best to merge the files and will leave things for you to resolve manually in the files it lists. The error message is as follows:
* 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>
  CONFLICT (content): Merge conflict in <fileName>
  Automatic merge failed; fix conflicts and then commit the result.
  Automatic merge failed; fix conflicts and then commit the result.


==Common questions==
==Common questions for when git fails during the merge==


===How do I know which files have conflicts in them?===
===How do I know which files have conflicts in them?===
If your merge failed to even start, there will be no conflicts in files. If git finds conflicts during the merge, it will list all files that have conflicts. You can also check on which files have merge conflicts by doing a 'git status'.  
If your merge failed to even start, there will be no conflicts in files. If git finds conflicts during the merge, it will list all files that have conflicts after the error message. You can also check on which files have merge conflicts by doing a 'git status'.  


Example:
Example:
Line 39: Line 39:
"Changes to be committed": All committed changes to files that are not affected by the conflict are staged.  
"Changes to be committed": All committed 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.  
"Changed but not updated ... unmerged": All files that have conflicts that must be resolved before repository will be back to working order.


===How do I find conflicts within the file itself?===
===How do I find conflicts within the file itself?===


Conflicts are marked in a file with clear line breaks
Conflicts are marked in a file with clear line breaks:


   <<<<<<< HEAD:mergetest
   <<<<<<< HEAD:mergetest
Line 51: Line 51:
   >>>>>>> 4e2b407f501b68f8588aa645acafffa0224b9b78:mergetest
   >>>>>>> 4e2b407f501b68f8588aa645acafffa0224b9b78:mergetest


'''<<<<<<<''': Indicates the start of the lines that had a merge conflict. The first set of lines are the lines from the file that you were trying to merge the changes '''into'''
'''<<<<<<<''': Indicates the start of the lines that had a merge conflict. The first set of lines are the lines from the file that you were trying to merge the changes '''into'''.


'''=======''': 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.
'''=======''': 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.
Line 61: Line 61:


===What do I do after I've resolved conflicts in all affected files?===
===What do I do after I've resolved conflicts in all affected files?===
git add the file(s), git commit and git push
git add the file(s), git commit and git push (Push only for branches tracked.)
 
('''Note''' added by Chin - need to commit everything, not just the resolved conflict file.)
 
==Tools to help you resolve both types of merge conflicts==
The following git tools below can help you resolve both simple and more complicated git merges.
 
===General tools===
 
====git diff====
 
[[Working_with_Git#.27git_diff.27|'''git diff''']]: a command that helps find differences between states of a repository/files. Useful in predicting and preventing merge conflicts.
 
'''git diff origin/master <fileName>''': Find the differences between the current index (HEAD) of fileName and what is in the central repository (origin/msater)
 
diff --git a/mergetest b/mergetest
index 9be56b9..0aeffac 100644
--- a/mergetest
+++ b/mergetest
@@ -1,3 +1,4 @@
  hello
+I am also editing this line
  This is a test
-This is my third line
+This is a fourth line I am adding


==Tools to help you resolve a merge conflict==
Changes coming from origin/master are marked with '''+''', while changes that are in your local repository (HEAD) are marked with '''-'''. This syntax does not notify which lines are added are deleted but just which lines originate in which state of the file.
Tools and example output when in a merge conflict situation.


===git status===
'''git diff FETCH_HEAD <fileName>''': Will provide the same output as above except is limited to the index of the last fetch that the user did. This may not be latest revision in the central repository.
The git status command provides an overview of all files that have been modified and are in conflict at the time of the merge.


====git status====
[[Working_with_Git#.27git_status.27|'''git status''']]: a command provides an overview of all files that have been modified and are in conflict at the time of the merge.


Example:
Example:
Line 86: Line 110:
* "Changes to be committed": All changes to files that are not affected by the conflict are staged.
* "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.  
* "Changed but not updated ... unmerged": All files that have conflicts that must be resolved before repository will be back to working order.
 
===Tools specifically for when git refuses to start merge===


===git stash===
====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.
'''IMPORTANT: Do not use git stash if git went through with the merge and there were merge conflicts! Only use git stash if git refused to merge because it foresees there being conflicts.'''
 
[[Working_with_Git#.27git_stash.27|'''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 "<Save Message>"''': Save changes to files in working directory and staging area that git is aware of
Line 96: Line 124:
   HEAD is now at 4e2b407 Added second file for example.
   HEAD is now at 4e2b407 Added second file for example.


'''git stash list''': List the stashes that you currently saved
'''git stash pop''': 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 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 checkout====


'''git stash apply <stash@{#}>''': Applies changes of recent stash or any stash is list specified, but doesn't remove it from the list.
[[Working_with_Git#.27git_checkout_HEAD_fileName.27|'''git checkout <fileName>''']]: Can be used to trash changes in the working directory so as to allow a '''git pull'''.


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


'''git stash clear''': Removes all stash's stored in the list
[[Working_with_Git#.27git_reset_--.5Boption.5D_HEAD.27|'''git reset --mixed''']]: Can be used to unstage files so as to allow a '''git pull'''.


===Tools specifically for when git conflicts arise during a merge===


====git reset====


[[Working_with_Git#.27git_reset_--.5Boption.5D_HEAD.27|'''git reset --hard''']]: reset repository in order to back out of merge conflict situation. git reset, particularly with the --hard option can be used to back out of merge conflict (click [[Working_with_Git#Commit_IDs_and_Referring_to_ancestors|here]] for more information).


'''IMPORTANT: Do not use any other options other than --hard for reset when resolving a situation where git failed during the merge, as they will leave conflict line markers in file and you can end up committing files with conflict markers still present.'''


==Scenarios==
==Scenarios==
Line 116: Line 146:


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


Steps toward Resolution:
Steps toward Resolution:
# 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.)
# 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.) '''OR''' git checkout <file> (throws out your changes so that you can do a merge)
# git status ( Verify all changes are staged)
# git status (Verify all changes are staged)
# git stash list (Lists all stash's user has)
# git pull or git merge (Bring in changes from central repository or another branch)
# git pull or git merge (Bring in changes from central repository or another branch)
# git stash pop (Will repopulate your changes into your working directory, maybe have to resolve merge conflicts)
# Only if did a 'git stash' in step 1: git stash pop (Will repopulate your changes into your working directory, may have to resolve merge conflicts)


===Git is unable to resolve a merge/pull===
===Git is unable to resolve a merge/pull===


Error Message:
Error Message:
#
 
  CONFLICT (content): Merge conflict in <fileName>
  CONFLICT (content): Merge conflict in <fileName>
  Automatic merge failed; fix conflicts and then commit the result.
  Automatic merge failed; fix conflicts and then commit the result.
Line 135: Line 165:
Steps toward Resolution:
Steps toward Resolution:
# git status (Shows all files that are in conflict as unmerged changed in working directory.)
# git status (Shows all files that are in conflict as unmerged changed in working directory.)
# Resolve Merge confilicts
# [[Resolving_merge_conflicts_in_Git#Common_questions_for_when_git_fails_during_the_merge|Resolve merge conflicts]]
# git add <files>
# git add <files>
# git commit -m "<Informative commit message>"
# git commit -m "<Informative commit message>"
==A GitHub test repository to experiment with conflicts==
You can experiment with resolving a git conflict with this repository: [https://github.com/brianleetest/testGit/blob/master/README.md https://github.com/brianleetest/testGit/blob/master/README.md]
#You will need a GitHub account and be added as a collaborator to push your changes.
#Create two separate directories, gitClone and gitCloneLeader in two different terminals and locations.  The leader can be the first to push changes (requires being a collaborator).
#Do the first "Group Member" steps up until WAIT in the gitClone directory.
#Then do all of the "Group Leader" steps in the gitCloneLeader directory (push required to cause conflict).
#Continue the "Group Member" steps (first git pull since cloning the repository and editing the file).
[[Category:Git]]

Latest revision as of 22:54, 24 August 2016

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 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. If this happens, there are no merge conflicts in individual files. You need to modify or stash the files it lists and then try to do a git pull again. The error messages are 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. Git will do its best to merge the files and will leave things for you to resolve manually in the files it lists. The error message is as follows:

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

Common questions for when git fails during the merge

How do I know which files have conflicts in them?

If your merge failed to even start, there will be no conflicts in files. If git finds conflicts during the merge, it will list all files that have conflicts after the error message. You can also check on which files have merge conflicts by doing a 'git status'.

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 committed changes to files that are not affected by the conflict are staged.

"Changed but not updated ... unmerged": All files that have conflicts that must be resolved before repository will be back to working order.

How do I find conflicts within the file itself?

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 had a merge conflict. The first set of lines are the lines from the file that you were trying to merge the changes into.

=======: 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.

>>>>>>>: Indicates the end of the lines that had a merge conflict.

How do I resolve a merge conflict in a file?

You resolve a conflict by editing the file to manually merge the parts of the file that git had trouble merging. This may mean discarding either your changes or someone else's or doing a mix of the two. You will also need to delete the '<<<<<<<', '=======', and '>>>>>>>' in the file.

What do I do after I've resolved conflicts in all affected files?

git add the file(s), git commit and git push (Push only for branches tracked.)

(Note added by Chin - need to commit everything, not just the resolved conflict file.)

Tools to help you resolve both types of merge conflicts

The following git tools below can help you resolve both simple and more complicated git merges.

General tools

git diff

git diff: a command that helps find differences between states of a repository/files. Useful in predicting and preventing merge conflicts.

git diff origin/master <fileName>: Find the differences between the current index (HEAD) of fileName and what is in the central repository (origin/msater)

diff --git a/mergetest b/mergetest
index 9be56b9..0aeffac 100644
--- a/mergetest
+++ b/mergetest
@@ -1,3 +1,4 @@
 hello
+I am also editing this line
 This is a test
-This is my third line
+This is a fourth line I am adding

Changes coming from origin/master are marked with +, while changes that are in your local repository (HEAD) are marked with -. This syntax does not notify which lines are added are deleted but just which lines originate in which state of the file.

git diff FETCH_HEAD <fileName>: Will provide the same output as above except is limited to the index of the last fetch that the user did. This may not be latest revision in the central repository.

git status

git status: a 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.
  • "Changed but not updated ... unmerged": All files that have conflicts that must be resolved before repository will be back to working order.

Tools specifically for when git refuses to start merge

git stash

IMPORTANT: Do not use git stash if git went through with the merge and there were merge conflicts! Only use git stash if git refused to merge because it foresees there being conflicts.

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 pop: 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 checkout

git checkout <fileName>: Can be used to trash changes in the working directory so as to allow a git pull.

git reset --mixed

git reset --mixed: Can be used to unstage files so as to allow a git pull.

Tools specifically for when git conflicts arise during a merge

git reset

git reset --hard: reset repository in order to back out of merge conflict situation. git reset, particularly with the --hard option can be used to back out of merge conflict (click here for more information).

IMPORTANT: Do not use any other options other than --hard for reset when resolving a situation where git failed during the merge, as they will leave conflict line markers in file and you can end up committing files with conflict markers still present.

Scenarios

Git refuses to start a merge/pull

Error Messages:

error: Entry '<fileName>' not uptodate. Cannot merge. (Changes in working directory)
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.) OR git checkout <file> (throws out your changes so that you can do a merge)
  2. git status (Verify all changes are staged)
  3. git pull or git merge (Bring in changes from central repository or another branch)
  4. Only if did a 'git stash' in step 1: git stash pop (Will repopulate your changes into your working directory, may 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 conflicts
  3. git add <files>
  4. git commit -m "<Informative commit message>"

A GitHub test repository to experiment with conflicts

You can experiment with resolving a git conflict with this repository: https://github.com/brianleetest/testGit/blob/master/README.md

  1. You will need a GitHub account and be added as a collaborator to push your changes.
  2. Create two separate directories, gitClone and gitCloneLeader in two different terminals and locations. The leader can be the first to push changes (requires being a collaborator).
  3. Do the first "Group Member" steps up until WAIT in the gitClone directory.
  4. Then do all of the "Group Leader" steps in the gitCloneLeader directory (push required to cause conflict).
  5. Continue the "Group Member" steps (first git pull since cloning the repository and editing the file).