logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

gittutorial-2.7 (16120B)


  1. '\" t
  2. .\" Title: gittutorial-2
  3. .\" Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
  4. .\" Generator: DocBook XSL Stylesheets v1.79.2 <http://docbook.sf.net/>
  5. .\" Date: 2025-03-14
  6. .\" Manual: Git Manual
  7. .\" Source: Git 2.49.0
  8. .\" Language: English
  9. .\"
  10. .TH "GITTUTORIAL\-2" "7" "2025-03-14" "Git 2\&.49\&.0" "Git Manual"
  11. .\" -----------------------------------------------------------------
  12. .\" * Define some portability stuff
  13. .\" -----------------------------------------------------------------
  14. .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  15. .\" http://bugs.debian.org/507673
  16. .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
  17. .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  18. .ie \n(.g .ds Aq \(aq
  19. .el .ds Aq '
  20. .\" -----------------------------------------------------------------
  21. .\" * set default formatting
  22. .\" -----------------------------------------------------------------
  23. .\" disable hyphenation
  24. .nh
  25. .\" disable justification (adjust text to left margin only)
  26. .ad l
  27. .\" -----------------------------------------------------------------
  28. .\" * MAIN CONTENT STARTS HERE *
  29. .\" -----------------------------------------------------------------
  30. .SH "NAME"
  31. gittutorial-2 \- A tutorial introduction to Git: part two
  32. .SH "SYNOPSIS"
  33. .sp
  34. .nf
  35. git *
  36. .fi
  37. .SH "DESCRIPTION"
  38. .sp
  39. You should work through \fBgittutorial\fR(7) before reading this tutorial\&.
  40. .sp
  41. The goal of this tutorial is to introduce two fundamental pieces of Git\(cqs architecture\(em\:the object database and the index file\(em\:and to provide the reader with everything necessary to understand the rest of the Git documentation\&.
  42. .SH "THE GIT OBJECT DATABASE"
  43. .sp
  44. Let\(cqs start a new project and create a small amount of history:
  45. .sp
  46. .if n \{\
  47. .RS 4
  48. .\}
  49. .nf
  50. $ mkdir test\-project
  51. $ cd test\-project
  52. $ git init
  53. Initialized empty Git repository in \&.git/
  54. $ echo \*(Aqhello world\*(Aq > file\&.txt
  55. $ git add \&.
  56. $ git commit \-a \-m "initial commit"
  57. [master (root\-commit) 54196cc] initial commit
  58. 1 file changed, 1 insertion(+)
  59. create mode 100644 file\&.txt
  60. $ echo \*(Aqhello world!\*(Aq >file\&.txt
  61. $ git commit \-a \-m "add emphasis"
  62. [master c4d59f3] add emphasis
  63. 1 file changed, 1 insertion(+), 1 deletion(\-)
  64. .fi
  65. .if n \{\
  66. .RE
  67. .\}
  68. .sp
  69. What are the 7 digits of hex that Git responded to the commit with?
  70. .sp
  71. We saw in part one of the tutorial that commits have names like this\&. It turns out that every object in the Git history is stored under a 40\-digit hex name\&. That name is the SHA\-1 hash of the object\(cqs contents; among other things, this ensures that Git will never store the same data twice (since identical data is given an identical SHA\-1 name), and that the contents of a Git object will never change (since that would change the object\(cqs name as well)\&. The 7 char hex strings here are simply the abbreviation of such 40 character long strings\&. Abbreviations can be used everywhere where the 40 character strings can be used, so long as they are unambiguous\&.
  72. .sp
  73. It is expected that the content of the commit object you created while following the example above generates a different SHA\-1 hash than the one shown above because the commit object records the time when it was created and the name of the person performing the commit\&.
  74. .sp
  75. We can ask Git about this particular object with the \fBcat\-file\fR command\&. Don\(cqt copy the 40 hex digits from this example but use those from your own version\&. Note that you can shorten it to only a few characters to save yourself typing all 40 hex digits:
  76. .sp
  77. .if n \{\
  78. .RS 4
  79. .\}
  80. .nf
  81. $ git cat\-file \-t 54196cc2
  82. commit
  83. $ git cat\-file commit 54196cc2
  84. tree 92b8b694ffb1675e5975148e1121810081dbdffe
  85. author J\&. Bruce Fields <bfields@puzzle\&.fieldses\&.org> 1143414668 \-0500
  86. committer J\&. Bruce Fields <bfields@puzzle\&.fieldses\&.org> 1143414668 \-0500
  87. initial commit
  88. .fi
  89. .if n \{\
  90. .RE
  91. .\}
  92. .sp
  93. A tree can refer to one or more "blob" objects, each corresponding to a file\&. In addition, a tree can also refer to other tree objects, thus creating a directory hierarchy\&. You can examine the contents of any tree using ls\-tree (remember that a long enough initial portion of the SHA\-1 will also work):
  94. .sp
  95. .if n \{\
  96. .RS 4
  97. .\}
  98. .nf
  99. $ git ls\-tree 92b8b694
  100. 100644 blob 3b18e512dba79e4c8300dd08aeb37f8e728b8dad file\&.txt
  101. .fi
  102. .if n \{\
  103. .RE
  104. .\}
  105. .sp
  106. Thus we see that this tree has one file in it\&. The SHA\-1 hash is a reference to that file\(cqs data:
  107. .sp
  108. .if n \{\
  109. .RS 4
  110. .\}
  111. .nf
  112. $ git cat\-file \-t 3b18e512
  113. blob
  114. .fi
  115. .if n \{\
  116. .RE
  117. .\}
  118. .sp
  119. A "blob" is just file data, which we can also examine with cat\-file:
  120. .sp
  121. .if n \{\
  122. .RS 4
  123. .\}
  124. .nf
  125. $ git cat\-file blob 3b18e512
  126. hello world
  127. .fi
  128. .if n \{\
  129. .RE
  130. .\}
  131. .sp
  132. Note that this is the old file data; so the object that Git named in its response to the initial tree was a tree with a snapshot of the directory state that was recorded by the first commit\&.
  133. .sp
  134. All of these objects are stored under their SHA\-1 names inside the Git directory:
  135. .sp
  136. .if n \{\
  137. .RS 4
  138. .\}
  139. .nf
  140. $ find \&.git/objects/
  141. \&.git/objects/
  142. \&.git/objects/pack
  143. \&.git/objects/info
  144. \&.git/objects/3b
  145. \&.git/objects/3b/18e512dba79e4c8300dd08aeb37f8e728b8dad
  146. \&.git/objects/92
  147. \&.git/objects/92/b8b694ffb1675e5975148e1121810081dbdffe
  148. \&.git/objects/54
  149. \&.git/objects/54/196cc2703dc165cbd373a65a4dcf22d50ae7f7
  150. \&.git/objects/a0
  151. \&.git/objects/a0/423896973644771497bdc03eb99d5281615b51
  152. \&.git/objects/d0
  153. \&.git/objects/d0/492b368b66bdabf2ac1fd8c92b39d3db916e59
  154. \&.git/objects/c4
  155. \&.git/objects/c4/d59f390b9cfd4318117afde11d601c1085f241
  156. .fi
  157. .if n \{\
  158. .RE
  159. .\}
  160. .sp
  161. and the contents of these files is just the compressed data plus a header identifying their length and their type\&. The type is either a blob, a tree, a commit, or a tag\&.
  162. .sp
  163. The simplest commit to find is the HEAD commit, which we can find from \&.git/HEAD:
  164. .sp
  165. .if n \{\
  166. .RS 4
  167. .\}
  168. .nf
  169. $ cat \&.git/HEAD
  170. ref: refs/heads/master
  171. .fi
  172. .if n \{\
  173. .RE
  174. .\}
  175. .sp
  176. As you can see, this tells us which branch we\(cqre currently on, and it tells us this by naming a file under the \&.git directory, which itself contains a SHA\-1 name referring to a commit object, which we can examine with cat\-file:
  177. .sp
  178. .if n \{\
  179. .RS 4
  180. .\}
  181. .nf
  182. $ cat \&.git/refs/heads/master
  183. c4d59f390b9cfd4318117afde11d601c1085f241
  184. $ git cat\-file \-t c4d59f39
  185. commit
  186. $ git cat\-file commit c4d59f39
  187. tree d0492b368b66bdabf2ac1fd8c92b39d3db916e59
  188. parent 54196cc2703dc165cbd373a65a4dcf22d50ae7f7
  189. author J\&. Bruce Fields <bfields@puzzle\&.fieldses\&.org> 1143418702 \-0500
  190. committer J\&. Bruce Fields <bfields@puzzle\&.fieldses\&.org> 1143418702 \-0500
  191. add emphasis
  192. .fi
  193. .if n \{\
  194. .RE
  195. .\}
  196. .sp
  197. The "tree" object here refers to the new state of the tree:
  198. .sp
  199. .if n \{\
  200. .RS 4
  201. .\}
  202. .nf
  203. $ git ls\-tree d0492b36
  204. 100644 blob a0423896973644771497bdc03eb99d5281615b51 file\&.txt
  205. $ git cat\-file blob a0423896
  206. hello world!
  207. .fi
  208. .if n \{\
  209. .RE
  210. .\}
  211. .sp
  212. and the "parent" object refers to the previous commit:
  213. .sp
  214. .if n \{\
  215. .RS 4
  216. .\}
  217. .nf
  218. $ git cat\-file commit 54196cc2
  219. tree 92b8b694ffb1675e5975148e1121810081dbdffe
  220. author J\&. Bruce Fields <bfields@puzzle\&.fieldses\&.org> 1143414668 \-0500
  221. committer J\&. Bruce Fields <bfields@puzzle\&.fieldses\&.org> 1143414668 \-0500
  222. initial commit
  223. .fi
  224. .if n \{\
  225. .RE
  226. .\}
  227. .sp
  228. The tree object is the tree we examined first, and this commit is unusual in that it lacks any parent\&.
  229. .sp
  230. Most commits have only one parent, but it is also common for a commit to have multiple parents\&. In that case the commit represents a merge, with the parent references pointing to the heads of the merged branches\&.
  231. .sp
  232. Besides blobs, trees, and commits, the only remaining type of object is a "tag", which we won\(cqt discuss here; refer to \fBgit-tag\fR(1) for details\&.
  233. .sp
  234. So now we know how Git uses the object database to represent a project\(cqs history:
  235. .sp
  236. .RS 4
  237. .ie n \{\
  238. \h'-04'\(bu\h'+03'\c
  239. .\}
  240. .el \{\
  241. .sp -1
  242. .IP \(bu 2.3
  243. .\}
  244. "commit" objects refer to "tree" objects representing the snapshot of a directory tree at a particular point in the history, and refer to "parent" commits to show how they\(cqre connected into the project history\&.
  245. .RE
  246. .sp
  247. .RS 4
  248. .ie n \{\
  249. \h'-04'\(bu\h'+03'\c
  250. .\}
  251. .el \{\
  252. .sp -1
  253. .IP \(bu 2.3
  254. .\}
  255. "tree" objects represent the state of a single directory, associating directory names to "blob" objects containing file data and "tree" objects containing subdirectory information\&.
  256. .RE
  257. .sp
  258. .RS 4
  259. .ie n \{\
  260. \h'-04'\(bu\h'+03'\c
  261. .\}
  262. .el \{\
  263. .sp -1
  264. .IP \(bu 2.3
  265. .\}
  266. "blob" objects contain file data without any other structure\&.
  267. .RE
  268. .sp
  269. .RS 4
  270. .ie n \{\
  271. \h'-04'\(bu\h'+03'\c
  272. .\}
  273. .el \{\
  274. .sp -1
  275. .IP \(bu 2.3
  276. .\}
  277. References to commit objects at the head of each branch are stored in files under \&.git/refs/heads/\&.
  278. .RE
  279. .sp
  280. .RS 4
  281. .ie n \{\
  282. \h'-04'\(bu\h'+03'\c
  283. .\}
  284. .el \{\
  285. .sp -1
  286. .IP \(bu 2.3
  287. .\}
  288. The name of the current branch is stored in \&.git/HEAD\&.
  289. .RE
  290. .sp
  291. Note, by the way, that lots of commands take a tree as an argument\&. But as we can see above, a tree can be referred to in many different ways\(em\:by the SHA\-1 name for that tree, by the name of a commit that refers to the tree, by the name of a branch whose head refers to that tree, etc\&.\-\-and most such commands can accept any of these names\&.
  292. .sp
  293. In command synopses, the word "tree\-ish" is sometimes used to designate such an argument\&.
  294. .SH "THE INDEX FILE"
  295. .sp
  296. The primary tool we\(cqve been using to create commits is \fBgit\-commit\fR \fB\-a\fR, which creates a commit including every change you\(cqve made to your working tree\&. But what if you want to commit changes only to certain files? Or only certain changes to certain files?
  297. .sp
  298. If we look at the way commits are created under the cover, we\(cqll see that there are more flexible ways creating commits\&.
  299. .sp
  300. Continuing with our test\-project, let\(cqs modify file\&.txt again:
  301. .sp
  302. .if n \{\
  303. .RS 4
  304. .\}
  305. .nf
  306. $ echo "hello world, again" >>file\&.txt
  307. .fi
  308. .if n \{\
  309. .RE
  310. .\}
  311. .sp
  312. but this time instead of immediately making the commit, let\(cqs take an intermediate step, and ask for diffs along the way to keep track of what\(cqs happening:
  313. .sp
  314. .if n \{\
  315. .RS 4
  316. .\}
  317. .nf
  318. $ git diff
  319. \-\-\- a/file\&.txt
  320. +++ b/file\&.txt
  321. @@ \-1 +1,2 @@
  322. hello world!
  323. +hello world, again
  324. $ git add file\&.txt
  325. $ git diff
  326. .fi
  327. .if n \{\
  328. .RE
  329. .\}
  330. .sp
  331. The last diff is empty, but no new commits have been made, and the head still doesn\(cqt contain the new line:
  332. .sp
  333. .if n \{\
  334. .RS 4
  335. .\}
  336. .nf
  337. $ git diff HEAD
  338. diff \-\-git a/file\&.txt b/file\&.txt
  339. index a042389\&.\&.513feba 100644
  340. \-\-\- a/file\&.txt
  341. +++ b/file\&.txt
  342. @@ \-1 +1,2 @@
  343. hello world!
  344. +hello world, again
  345. .fi
  346. .if n \{\
  347. .RE
  348. .\}
  349. .sp
  350. So \fIgit diff\fR is comparing against something other than the head\&. The thing that it\(cqs comparing against is actually the index file, which is stored in \&.git/index in a binary format, but whose contents we can examine with ls\-files:
  351. .sp
  352. .if n \{\
  353. .RS 4
  354. .\}
  355. .nf
  356. $ git ls\-files \-\-stage
  357. 100644 513feba2e53ebbd2532419ded848ba19de88ba00 0 file\&.txt
  358. $ git cat\-file \-t 513feba2
  359. blob
  360. $ git cat\-file blob 513feba2
  361. hello world!
  362. hello world, again
  363. .fi
  364. .if n \{\
  365. .RE
  366. .\}
  367. .sp
  368. So what our \fIgit add\fR did was store a new blob and then put a reference to it in the index file\&. If we modify the file again, we\(cqll see that the new modifications are reflected in the \fIgit diff\fR output:
  369. .sp
  370. .if n \{\
  371. .RS 4
  372. .\}
  373. .nf
  374. $ echo \*(Aqagain?\*(Aq >>file\&.txt
  375. $ git diff
  376. index 513feba\&.\&.ba3da7b 100644
  377. \-\-\- a/file\&.txt
  378. +++ b/file\&.txt
  379. @@ \-1,2 +1,3 @@
  380. hello world!
  381. hello world, again
  382. +again?
  383. .fi
  384. .if n \{\
  385. .RE
  386. .\}
  387. .sp
  388. With the right arguments, \fIgit diff\fR can also show us the difference between the working directory and the last commit, or between the index and the last commit:
  389. .sp
  390. .if n \{\
  391. .RS 4
  392. .\}
  393. .nf
  394. $ git diff HEAD
  395. diff \-\-git a/file\&.txt b/file\&.txt
  396. index a042389\&.\&.ba3da7b 100644
  397. \-\-\- a/file\&.txt
  398. +++ b/file\&.txt
  399. @@ \-1 +1,3 @@
  400. hello world!
  401. +hello world, again
  402. +again?
  403. $ git diff \-\-cached
  404. diff \-\-git a/file\&.txt b/file\&.txt
  405. index a042389\&.\&.513feba 100644
  406. \-\-\- a/file\&.txt
  407. +++ b/file\&.txt
  408. @@ \-1 +1,2 @@
  409. hello world!
  410. +hello world, again
  411. .fi
  412. .if n \{\
  413. .RE
  414. .\}
  415. .sp
  416. At any time, we can create a new commit using \fIgit commit\fR (without the "\-a" option), and verify that the state committed only includes the changes stored in the index file, not the additional change that is still only in our working tree:
  417. .sp
  418. .if n \{\
  419. .RS 4
  420. .\}
  421. .nf
  422. $ git commit \-m "repeat"
  423. $ git diff HEAD
  424. diff \-\-git a/file\&.txt b/file\&.txt
  425. index 513feba\&.\&.ba3da7b 100644
  426. \-\-\- a/file\&.txt
  427. +++ b/file\&.txt
  428. @@ \-1,2 +1,3 @@
  429. hello world!
  430. hello world, again
  431. +again?
  432. .fi
  433. .if n \{\
  434. .RE
  435. .\}
  436. .sp
  437. So by default \fIgit commit\fR uses the index to create the commit, not the working tree; the "\-a" option to commit tells it to first update the index with all changes in the working tree\&.
  438. .sp
  439. Finally, it\(cqs worth looking at the effect of \fIgit add\fR on the index file:
  440. .sp
  441. .if n \{\
  442. .RS 4
  443. .\}
  444. .nf
  445. $ echo "goodbye, world" >closing\&.txt
  446. $ git add closing\&.txt
  447. .fi
  448. .if n \{\
  449. .RE
  450. .\}
  451. .sp
  452. The effect of the \fIgit add\fR was to add one entry to the index file:
  453. .sp
  454. .if n \{\
  455. .RS 4
  456. .\}
  457. .nf
  458. $ git ls\-files \-\-stage
  459. 100644 8b9743b20d4b15be3955fc8d5cd2b09cd2336138 0 closing\&.txt
  460. 100644 513feba2e53ebbd2532419ded848ba19de88ba00 0 file\&.txt
  461. .fi
  462. .if n \{\
  463. .RE
  464. .\}
  465. .sp
  466. And, as you can see with cat\-file, this new entry refers to the current contents of the file:
  467. .sp
  468. .if n \{\
  469. .RS 4
  470. .\}
  471. .nf
  472. $ git cat\-file blob 8b9743b2
  473. goodbye, world
  474. .fi
  475. .if n \{\
  476. .RE
  477. .\}
  478. .sp
  479. The "status" command is a useful way to get a quick summary of the situation:
  480. .sp
  481. .if n \{\
  482. .RS 4
  483. .\}
  484. .nf
  485. $ git status
  486. On branch master
  487. Changes to be committed:
  488. (use "git restore \-\-staged <file>\&.\&.\&." to unstage)
  489. new file: closing\&.txt
  490. Changes not staged for commit:
  491. (use "git add <file>\&.\&.\&." to update what will be committed)
  492. (use "git restore <file>\&.\&.\&." to discard changes in working directory)
  493. modified: file\&.txt
  494. .fi
  495. .if n \{\
  496. .RE
  497. .\}
  498. .sp
  499. Since the current state of closing\&.txt is cached in the index file, it is listed as "Changes to be committed"\&. Since file\&.txt has changes in the working directory that aren\(cqt reflected in the index, it is marked "changed but not updated"\&. At this point, running "git commit" would create a commit that added closing\&.txt (with its new contents), but that didn\(cqt modify file\&.txt\&.
  500. .sp
  501. Also, note that a bare \fBgit\fR \fBdiff\fR shows the changes to file\&.txt, but not the addition of closing\&.txt, because the version of closing\&.txt in the index file is identical to the one in the working directory\&.
  502. .sp
  503. In addition to being the staging area for new commits, the index file is also populated from the object database when checking out a branch, and is used to hold the trees involved in a merge operation\&. See \fBgitcore-tutorial\fR(7) and the relevant man pages for details\&.
  504. .SH "WHAT NEXT?"
  505. .sp
  506. At this point you should know everything necessary to read the man pages for any of the git commands; one good place to start would be with the commands mentioned in \fBgiteveryday\fR(7)\&. You should be able to find any unknown jargon in \fBgitglossary\fR(7)\&.
  507. .sp
  508. The \m[blue]\fBGit User\(cqs Manual\fR\m[]\&\s-2\u[1]\d\s+2 provides a more comprehensive introduction to Git\&.
  509. .sp
  510. \fBgitcvs-migration\fR(7) explains how to import a CVS repository into Git, and shows how to use Git in a CVS\-like way\&.
  511. .sp
  512. For some interesting examples of Git use, see the \m[blue]\fBhowtos\fR\m[]\&\s-2\u[2]\d\s+2\&.
  513. .sp
  514. For Git developers, \fBgitcore-tutorial\fR(7) goes into detail on the lower\-level Git mechanisms involved in, for example, creating a new commit\&.
  515. .SH "SEE ALSO"
  516. .sp
  517. \fBgittutorial\fR(7), \fBgitcvs-migration\fR(7), \fBgitcore-tutorial\fR(7), \fBgitglossary\fR(7), \fBgit-help\fR(1), \fBgiteveryday\fR(7), \m[blue]\fBThe Git User\(cqs Manual\fR\m[]\&\s-2\u[1]\d\s+2
  518. .SH "GIT"
  519. .sp
  520. Part of the \fBgit\fR(1) suite
  521. .SH "NOTES"
  522. .IP " 1." 4
  523. Git User\(cqs Manual
  524. .RS 4
  525. \%git-htmldocs/user-manual.html
  526. .RE
  527. .IP " 2." 4
  528. howtos
  529. .RS 4
  530. \%git-htmldocs/howto-index.html
  531. .RE