@@ -1168,18 +1168,46 @@ the working tree in a special state that gives you all the
11681168information you need to help resolve the merge.
11691169
11701170Files with conflicts are marked specially in the index, so until you
1171- resolve the problem and update the index, git commit will fail:
1171+ resolve the problem and update the index, gitlink:git-commit[1] will
1172+ fail:
11721173
11731174-------------------------------------------------
11741175$ git commit
11751176file.txt: needs merge
11761177-------------------------------------------------
11771178
1178- Also, git status will list those files as "unmerged".
1179+ Also, gitlink:git-status[1] will list those files as "unmerged", and the
1180+ files with conflicts will have conflict markers added, like this:
1181+
1182+ -------------------------------------------------
1183+ <<<<<<< HEAD:file.txt
1184+ Hello world
1185+ =======
1186+ Goodbye
1187+ >>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
1188+ -------------------------------------------------
1189+
1190+ All you need to do is edit the files to resolve the conflicts, and then
1191+
1192+ -------------------------------------------------
1193+ $ git add file.txt
1194+ $ git commit
1195+ -------------------------------------------------
1196+
1197+ Note that the commit message will already be filled in for you with
1198+ some information about the merge. Normally you can just use this
1199+ default message unchanged, but you may add additional commentary of
1200+ your own if desired.
1201+
1202+ The above is all you need to know to resolve a simple merge. But git
1203+ also provides more information to help resolve conflicts:
1204+
1205+ Getting conflict-resolution help during a merge
1206+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11791207
11801208All of the changes that git was able to merge automatically are
11811209already added to the index file, so gitlink:git-diff[1] shows only
1182- the conflicts. Also, it uses a somewhat unusual syntax:
1210+ the conflicts. It uses an unusual syntax:
11831211
11841212-------------------------------------------------
11851213$ git diff
@@ -1200,14 +1228,32 @@ conflict will have two parents instead of the usual one: one parent
12001228will be HEAD, the tip of the current branch; the other will be the
12011229tip of the other branch, which is stored temporarily in MERGE_HEAD.
12021230
1203- The diff above shows the differences between the working-tree version
1204- of file.txt and two previous versions: one version from HEAD, and one
1205- from MERGE_HEAD. So instead of preceding each line by a single "+"
1206- or "-", it now uses two columns: the first column is used for
1207- differences between the first parent and the working directory copy,
1208- and the second for differences between the second parent and the
1209- working directory copy. Thus after resolving the conflict in the
1210- obvious way, the diff will look like:
1231+ During the merge, the index holds three versions of each file. Each of
1232+ these three "file stages" represents a different version of the file:
1233+
1234+ -------------------------------------------------
1235+ $ git show :1:file.txt # the file in a common ancestor of both branches
1236+ $ git show :2:file.txt # the version from HEAD, but including any
1237+ # nonconflicting changes from MERGE_HEAD
1238+ $ git show :3:file.txt # the version from MERGE_HEAD, but including any
1239+ # nonconflicting changes from HEAD.
1240+ -------------------------------------------------
1241+
1242+ Since the stage 2 and stage 3 versions have already been updated with
1243+ nonconflicting changes, the only remaining differences between them are
1244+ the important ones; thus gitlink:git-diff[1] can use the information in
1245+ the index to show only those conflicts.
1246+
1247+ The diff above shows the differences between the working-tree version of
1248+ file.txt and the stage 2 and stage 3 versions. So instead of preceding
1249+ each line by a single "+" or "-", it now uses two columns: the first
1250+ column is used for differences between the first parent and the working
1251+ directory copy, and the second for differences between the second parent
1252+ and the working directory copy. (See the "COMBINED DIFF FORMAT" section
1253+ of gitlink:git-diff-files[1] for a details of the format.)
1254+
1255+ After resolving the conflict in the obvious way (but before updating the
1256+ index), the diff will look like:
12111257
12121258-------------------------------------------------
12131259$ git diff
@@ -1225,26 +1271,37 @@ This shows that our resolved version deleted "Hello world" from the
12251271first parent, deleted "Goodbye" from the second parent, and added
12261272"Goodbye world", which was previously absent from both.
12271273
1228- The gitlink:git-log[1] command also provides special help for merges:
1274+ Some special diff options allow diffing the working directory against
1275+ any of these stages:
1276+
1277+ -------------------------------------------------
1278+ $ git diff -1 file.txt # diff against stage 1
1279+ $ git diff --base file.txt # same as the above
1280+ $ git diff -2 file.txt # diff against stage 2
1281+ $ git diff --ours file.txt # same as the above
1282+ $ git diff -3 file.txt # diff against stage 3
1283+ $ git diff --theirs file.txt # same as the above.
1284+ -------------------------------------------------
1285+
1286+ The gitlink:git-log[1] and gitk[1] commands also provide special help
1287+ for merges:
12291288
12301289-------------------------------------------------
12311290$ git log --merge
1291+ $ gitk --merge
12321292-------------------------------------------------
12331293
1234- This will list all commits which exist only on HEAD or on MERGE_HEAD,
1235- and which touch an unmerged file.
1294+ These will display all commits which exist only on HEAD or on
1295+ MERGE_HEAD, and which touch an unmerged file.
12361296
1237- We can now add the resolved version to the index and commit :
1297+ Each time you resolve the conflicts in a file and update the index :
12381298
12391299-------------------------------------------------
12401300$ git add file.txt
1241- $ git commit
12421301-------------------------------------------------
12431302
1244- Note that the commit message will already be filled in for you with
1245- some information about the merge. Normally you can just use this
1246- default message unchanged, but you may add additional commentary of
1247- your own if desired.
1303+ the different stages of that file will be "collapsed", after which
1304+ git-diff will (by default) no longer show diffs for that file.
12481305
12491306[[undoing-a-merge]]
12501307undoing a merge
@@ -2988,11 +3045,6 @@ provides.
29883045Simplify beginning by suggesting disconnected head instead of
29893046temporary branch creation?
29903047
2991- Explain how to refer to file stages in the "how to resolve a merge"
2992- section: diff -1, -2, -3, --ours, --theirs :1:/path notation. The
2993- "git ls-files --unmerged --stage" thing is sorta useful too,
2994- actually. And note gitk --merge.
2995-
29963048Add more good examples. Entire sections of just cookbook examples
29973049might be a good idea; maybe make an "advanced examples" section a
29983050standard end-of-chapter section?
0 commit comments