@@ -391,15 +391,20 @@ index 8be626f..d7aac9d 100644
391391As you can see, a commit shows who made the latest change, what they
392392did, and why.
393393
394- Every commit has a 40-hexdigit id, sometimes called the "object name"
395- or the "SHA1 id", shown on the first line of the "git show" output.
396- You can usually refer to a commit by a shorter name, such as a tag or a
397- branch name, but this longer name can also be useful. Most
398- importantly, it is a globally unique name for this commit: so if you
399- tell somebody else the object name (for example in email), then you are
400- guaranteed that name will refer to the same commit in their repository
401- that it does in yours (assuming their repository has that commit at
402- all).
394+ Every commit has a 40-hexdigit id, sometimes called the "object name" or the
395+ "SHA1 id", shown on the first line of the "git show" output. You can usually
396+ refer to a commit by a shorter name, such as a tag or a branch name, but this
397+ longer name can also be useful. Most importantly, it is a globally unique
398+ name for this commit: so if you tell somebody else the object name (for
399+ example in email), then you are guaranteed that name will refer to the same
400+ commit in their repository that it does in yours (assuming their repository
401+ has that commit at all). Since the object name is computed as a hash over the
402+ contents of the commit, you are guaranteed that the commit can never change
403+ without its name also changing.
404+
405+ In fact, in <<git-internals>> we shall see that everything stored in git
406+ history, including file data and directory contents, is stored in an object
407+ with a name that is a hash of its contents.
403408
404409Understanding history: commits, parents, and reachability
405410~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1163,18 +1168,46 @@ the working tree in a special state that gives you all the
11631168information you need to help resolve the merge.
11641169
11651170Files with conflicts are marked specially in the index, so until you
1166- 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:
11671173
11681174-------------------------------------------------
11691175$ git commit
11701176file.txt: needs merge
11711177-------------------------------------------------
11721178
1173- 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+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11741207
11751208All of the changes that git was able to merge automatically are
11761209already added to the index file, so gitlink:git-diff[1] shows only
1177- the conflicts. Also, it uses a somewhat unusual syntax:
1210+ the conflicts. It uses an unusual syntax:
11781211
11791212-------------------------------------------------
11801213$ git diff
@@ -1195,14 +1228,32 @@ conflict will have two parents instead of the usual one: one parent
11951228will be HEAD, the tip of the current branch; the other will be the
11961229tip of the other branch, which is stored temporarily in MERGE_HEAD.
11971230
1198- The diff above shows the differences between the working-tree version
1199- of file.txt and two previous versions: one version from HEAD, and one
1200- from MERGE_HEAD. So instead of preceding each line by a single "+"
1201- or "-", it now uses two columns: the first column is used for
1202- differences between the first parent and the working directory copy,
1203- and the second for differences between the second parent and the
1204- working directory copy. Thus after resolving the conflict in the
1205- 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:
12061257
12071258-------------------------------------------------
12081259$ git diff
@@ -1220,26 +1271,37 @@ This shows that our resolved version deleted "Hello world" from the
12201271first parent, deleted "Goodbye" from the second parent, and added
12211272"Goodbye world", which was previously absent from both.
12221273
1223- 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:
12241288
12251289-------------------------------------------------
12261290$ git log --merge
1291+ $ gitk --merge
12271292-------------------------------------------------
12281293
1229- This will list all commits which exist only on HEAD or on MERGE_HEAD,
1230- 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.
12311296
1232- 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 :
12331298
12341299-------------------------------------------------
12351300$ git add file.txt
1236- $ git commit
12371301-------------------------------------------------
12381302
1239- Note that the commit message will already be filled in for you with
1240- some information about the merge. Normally you can just use this
1241- default message unchanged, but you may add additional commentary of
1242- 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.
12431305
12441306[[undoing-a-merge]]
12451307undoing a merge
@@ -1255,7 +1317,7 @@ $ git reset --hard HEAD
12551317Or, if you've already commited the merge that you want to throw away,
12561318
12571319-------------------------------------------------
1258- $ git reset --hard HEAD^
1320+ $ git reset --hard ORIG_HEAD
12591321-------------------------------------------------
12601322
12611323However, this last command can be dangerous in some cases--never
@@ -1328,6 +1390,7 @@ with the changes to be reverted, then you will be asked to fix
13281390conflicts manually, just as in the case of <<resolving-a-merge,
13291391resolving a merge>>.
13301392
1393+ [[fixing-a-mistake-by-editing-history]]
13311394Fixing a mistake by editing history
13321395~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13331396
@@ -1930,6 +1993,51 @@ return mywork to the state it had before you started the rebase:
19301993$ git rebase --abort
19311994-------------------------------------------------
19321995
1996+ Modifying a single commit
1997+ -------------------------
1998+
1999+ We saw in <<fixing-a-mistake-by-editing-history>> that you can replace the
2000+ most recent commit using
2001+
2002+ -------------------------------------------------
2003+ $ git commit --amend
2004+ -------------------------------------------------
2005+
2006+ which will replace the old commit by a new commit incorporating your
2007+ changes, giving you a chance to edit the old commit message first.
2008+
2009+ You can also use a combination of this and gitlink:git-rebase[1] to edit
2010+ commits further back in your history. First, tag the problematic commit with
2011+
2012+ -------------------------------------------------
2013+ $ git tag bad mywork~5
2014+ -------------------------------------------------
2015+
2016+ (Either gitk or git-log may be useful for finding the commit.)
2017+
2018+ Then check out a new branch at that commit, edit it, and rebase the rest of
2019+ the series on top of it:
2020+
2021+ -------------------------------------------------
2022+ $ git checkout -b TMP bad
2023+ $ # make changes here and update the index
2024+ $ git commit --amend
2025+ $ git rebase --onto TMP bad mywork
2026+ -------------------------------------------------
2027+
2028+ When you're done, you'll be left with mywork checked out, with the top patches
2029+ on mywork reapplied on top of the modified commit you created in TMP. You can
2030+ then clean up with
2031+
2032+ -------------------------------------------------
2033+ $ git branch -d TMP
2034+ $ git tag -d bad
2035+ -------------------------------------------------
2036+
2037+ Note that the immutable nature of git history means that you haven't really
2038+ "modified" existing commits; instead, you have replaced the old commits with
2039+ new commits having new object names.
2040+
19332041Reordering or selecting from a patch series
19342042-------------------------------------------
19352043
@@ -2155,6 +2263,7 @@ See gitlink:git-config[1] for more details on the configuration
21552263options mentioned above.
21562264
21572265
2266+ [[git-internals]]
21582267Git internals
21592268=============
21602269
@@ -2936,11 +3045,6 @@ provides.
29363045Simplify beginning by suggesting disconnected head instead of
29373046temporary branch creation?
29383047
2939- Explain how to refer to file stages in the "how to resolve a merge"
2940- section: diff -1, -2, -3, --ours, --theirs :1:/path notation. The
2941- "git ls-files --unmerged --stage" thing is sorta useful too,
2942- actually. And note gitk --merge.
2943-
29443048Add more good examples. Entire sections of just cookbook examples
29453049might be a good idea; maybe make an "advanced examples" section a
29463050standard end-of-chapter section?
0 commit comments