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.7 (23679B)


  1. '\" t
  2. .\" Title: gittutorial
  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" "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 \- A tutorial introduction to Git
  32. .SH "SYNOPSIS"
  33. .sp
  34. .nf
  35. git *
  36. .fi
  37. .SH "DESCRIPTION"
  38. .sp
  39. This tutorial explains how to import a new project into Git, make changes to it, and share changes with other developers\&.
  40. .sp
  41. If you are instead primarily interested in using Git to fetch a project, for example, to test the latest version, you may prefer to start with the first two chapters of \m[blue]\fBThe Git User\(cqs Manual\fR\m[]\&\s-2\u[1]\d\s+2\&.
  42. .sp
  43. First, note that you can get documentation for a command such as \fBgit\fR \fBlog\fR \fB\-\-graph\fR with:
  44. .sp
  45. .if n \{\
  46. .RS 4
  47. .\}
  48. .nf
  49. $ man git\-log
  50. .fi
  51. .if n \{\
  52. .RE
  53. .\}
  54. .sp
  55. or:
  56. .sp
  57. .if n \{\
  58. .RS 4
  59. .\}
  60. .nf
  61. $ git help log
  62. .fi
  63. .if n \{\
  64. .RE
  65. .\}
  66. .sp
  67. With the latter, you can use the manual viewer of your choice; see \fBgit-help\fR(1) for more information\&.
  68. .sp
  69. It is a good idea to introduce yourself to Git with your name and public email address before doing any operation\&. The easiest way to do so is:
  70. .sp
  71. .if n \{\
  72. .RS 4
  73. .\}
  74. .nf
  75. $ git config \-\-global user\&.name "Your Name Comes Here"
  76. $ git config \-\-global user\&.email you@yourdomain\&.example\&.com
  77. .fi
  78. .if n \{\
  79. .RE
  80. .\}
  81. .SH "IMPORTING A NEW PROJECT"
  82. .sp
  83. Assume you have a tarball \fBproject\&.tar\&.gz\fR with your initial work\&. You can place it under Git revision control as follows\&.
  84. .sp
  85. .if n \{\
  86. .RS 4
  87. .\}
  88. .nf
  89. $ tar xzf project\&.tar\&.gz
  90. $ cd project
  91. $ git init
  92. .fi
  93. .if n \{\
  94. .RE
  95. .\}
  96. .sp
  97. Git will reply
  98. .sp
  99. .if n \{\
  100. .RS 4
  101. .\}
  102. .nf
  103. Initialized empty Git repository in \&.git/
  104. .fi
  105. .if n \{\
  106. .RE
  107. .\}
  108. .sp
  109. You\(cqve now initialized the working directory\(em\:you may notice a new directory created, named \&.\fBgit\fR\&.
  110. .sp
  111. Next, tell Git to take a snapshot of the contents of all files under the current directory (note the \&.), with \fBgit\fR \fBadd\fR:
  112. .sp
  113. .if n \{\
  114. .RS 4
  115. .\}
  116. .nf
  117. $ git add \&.
  118. .fi
  119. .if n \{\
  120. .RE
  121. .\}
  122. .sp
  123. This snapshot is now stored in a temporary staging area which Git calls the "index"\&. You can permanently store the contents of the index in the repository with \fBgit\fR \fBcommit\fR:
  124. .sp
  125. .if n \{\
  126. .RS 4
  127. .\}
  128. .nf
  129. $ git commit
  130. .fi
  131. .if n \{\
  132. .RE
  133. .\}
  134. .sp
  135. This will prompt you for a commit message\&. You\(cqve now stored the first version of your project in Git\&.
  136. .SH "MAKING CHANGES"
  137. .sp
  138. Modify some files, then add their updated contents to the index:
  139. .sp
  140. .if n \{\
  141. .RS 4
  142. .\}
  143. .nf
  144. $ git add file1 file2 file3
  145. .fi
  146. .if n \{\
  147. .RE
  148. .\}
  149. .sp
  150. You are now ready to commit\&. You can see what is about to be committed using \fBgit\fR \fBdiff\fR with the \fB\-\-cached\fR option:
  151. .sp
  152. .if n \{\
  153. .RS 4
  154. .\}
  155. .nf
  156. $ git diff \-\-cached
  157. .fi
  158. .if n \{\
  159. .RE
  160. .\}
  161. .sp
  162. (Without \fB\-\-cached\fR, \fBgit\fR \fBdiff\fR will show you any changes that you\(cqve made but not yet added to the index\&.) You can also get a brief summary of the situation with \fBgit\fR \fBstatus\fR:
  163. .sp
  164. .if n \{\
  165. .RS 4
  166. .\}
  167. .nf
  168. $ git status
  169. On branch master
  170. Changes to be committed:
  171. (use "git restore \-\-staged <file>\&.\&.\&." to unstage)
  172. modified: file1
  173. modified: file2
  174. modified: file3
  175. .fi
  176. .if n \{\
  177. .RE
  178. .\}
  179. .sp
  180. If you need to make any further adjustments, do so now, and then add any newly modified content to the index\&. Finally, commit your changes with:
  181. .sp
  182. .if n \{\
  183. .RS 4
  184. .\}
  185. .nf
  186. $ git commit
  187. .fi
  188. .if n \{\
  189. .RE
  190. .\}
  191. .sp
  192. This will again prompt you for a message describing the change, and then record a new version of the project\&.
  193. .sp
  194. Alternatively, instead of running \fBgit\fR \fBadd\fR beforehand, you can use
  195. .sp
  196. .if n \{\
  197. .RS 4
  198. .\}
  199. .nf
  200. $ git commit \-a
  201. .fi
  202. .if n \{\
  203. .RE
  204. .\}
  205. .sp
  206. which will automatically notice any modified (but not new) files, add them to the index, and commit, all in one step\&.
  207. .sp
  208. A note on commit messages: Though not required, it\(cqs a good idea to begin the commit message with a single short (no more than 50 characters) line summarizing the change, followed by a blank line and then a more thorough description\&. The text up to the first blank line in a commit message is treated as the commit title, and that title is used throughout Git\&. For example, \fBgit-format-patch\fR(1) turns a commit into email, and it uses the title on the Subject line and the rest of the commit in the body\&.
  209. .SH "GIT TRACKS CONTENT NOT FILES"
  210. .sp
  211. Many revision control systems provide an \fBadd\fR command that tells the system to start tracking changes to a new file\&. Git\(cqs \fBadd\fR command does something simpler and more powerful: \fBgit\fR \fBadd\fR is used both for new and newly modified files, and in both cases it takes a snapshot of the given files and stages that content in the index, ready for inclusion in the next commit\&.
  212. .SH "VIEWING PROJECT HISTORY"
  213. .sp
  214. At any point you can view the history of your changes using
  215. .sp
  216. .if n \{\
  217. .RS 4
  218. .\}
  219. .nf
  220. $ git log
  221. .fi
  222. .if n \{\
  223. .RE
  224. .\}
  225. .sp
  226. If you also want to see complete diffs at each step, use
  227. .sp
  228. .if n \{\
  229. .RS 4
  230. .\}
  231. .nf
  232. $ git log \-p
  233. .fi
  234. .if n \{\
  235. .RE
  236. .\}
  237. .sp
  238. Often the overview of the change is useful to get a feel of each step
  239. .sp
  240. .if n \{\
  241. .RS 4
  242. .\}
  243. .nf
  244. $ git log \-\-stat \-\-summary
  245. .fi
  246. .if n \{\
  247. .RE
  248. .\}
  249. .SH "MANAGING BRANCHES"
  250. .sp
  251. A single Git repository can maintain multiple branches of development\&. To create a new branch named \fBexperimental\fR, use
  252. .sp
  253. .if n \{\
  254. .RS 4
  255. .\}
  256. .nf
  257. $ git branch experimental
  258. .fi
  259. .if n \{\
  260. .RE
  261. .\}
  262. .sp
  263. If you now run
  264. .sp
  265. .if n \{\
  266. .RS 4
  267. .\}
  268. .nf
  269. $ git branch
  270. .fi
  271. .if n \{\
  272. .RE
  273. .\}
  274. .sp
  275. you\(cqll get a list of all existing branches:
  276. .sp
  277. .if n \{\
  278. .RS 4
  279. .\}
  280. .nf
  281. experimental
  282. * master
  283. .fi
  284. .if n \{\
  285. .RE
  286. .\}
  287. .sp
  288. The \fBexperimental\fR branch is the one you just created, and the \fBmaster\fR branch is a default branch that was created for you automatically\&. The asterisk marks the branch you are currently on; type
  289. .sp
  290. .if n \{\
  291. .RS 4
  292. .\}
  293. .nf
  294. $ git switch experimental
  295. .fi
  296. .if n \{\
  297. .RE
  298. .\}
  299. .sp
  300. to switch to the \fBexperimental\fR branch\&. Now edit a file, commit the change, and switch back to the \fBmaster\fR branch:
  301. .sp
  302. .if n \{\
  303. .RS 4
  304. .\}
  305. .nf
  306. (edit file)
  307. $ git commit \-a
  308. $ git switch master
  309. .fi
  310. .if n \{\
  311. .RE
  312. .\}
  313. .sp
  314. Check that the change you made is no longer visible, since it was made on the \fBexperimental\fR branch and you\(cqre back on the \fBmaster\fR branch\&.
  315. .sp
  316. You can make a different change on the \fBmaster\fR branch:
  317. .sp
  318. .if n \{\
  319. .RS 4
  320. .\}
  321. .nf
  322. (edit file)
  323. $ git commit \-a
  324. .fi
  325. .if n \{\
  326. .RE
  327. .\}
  328. .sp
  329. at this point the two branches have diverged, with different changes made in each\&. To merge the changes made in \fBexperimental\fR into \fBmaster\fR, run
  330. .sp
  331. .if n \{\
  332. .RS 4
  333. .\}
  334. .nf
  335. $ git merge experimental
  336. .fi
  337. .if n \{\
  338. .RE
  339. .\}
  340. .sp
  341. If the changes don\(cqt conflict, you\(cqre done\&. If there are conflicts, markers will be left in the problematic files showing the conflict;
  342. .sp
  343. .if n \{\
  344. .RS 4
  345. .\}
  346. .nf
  347. $ git diff
  348. .fi
  349. .if n \{\
  350. .RE
  351. .\}
  352. .sp
  353. will show this\&. Once you\(cqve edited the files to resolve the conflicts,
  354. .sp
  355. .if n \{\
  356. .RS 4
  357. .\}
  358. .nf
  359. $ git commit \-a
  360. .fi
  361. .if n \{\
  362. .RE
  363. .\}
  364. .sp
  365. will commit the result of the merge\&. Finally,
  366. .sp
  367. .if n \{\
  368. .RS 4
  369. .\}
  370. .nf
  371. $ gitk
  372. .fi
  373. .if n \{\
  374. .RE
  375. .\}
  376. .sp
  377. will show a nice graphical representation of the resulting history\&.
  378. .sp
  379. At this point you could delete the \fBexperimental\fR branch with
  380. .sp
  381. .if n \{\
  382. .RS 4
  383. .\}
  384. .nf
  385. $ git branch \-d experimental
  386. .fi
  387. .if n \{\
  388. .RE
  389. .\}
  390. .sp
  391. This command ensures that the changes in the \fBexperimental\fR branch are already in the current branch\&.
  392. .sp
  393. If you develop on a branch \fBcrazy\-idea\fR, then regret it, you can always delete the branch with
  394. .sp
  395. .if n \{\
  396. .RS 4
  397. .\}
  398. .nf
  399. $ git branch \-D crazy\-idea
  400. .fi
  401. .if n \{\
  402. .RE
  403. .\}
  404. .sp
  405. Branches are cheap and easy, so this is a good way to try something out\&.
  406. .SH "USING GIT FOR COLLABORATION"
  407. .sp
  408. Suppose that Alice has started a new project with a Git repository in \fB/home/alice/project\fR, and that Bob, who has a home directory on the same machine, wants to contribute\&.
  409. .sp
  410. Bob begins with:
  411. .sp
  412. .if n \{\
  413. .RS 4
  414. .\}
  415. .nf
  416. bob$ git clone /home/alice/project myrepo
  417. .fi
  418. .if n \{\
  419. .RE
  420. .\}
  421. .sp
  422. This creates a new directory \fBmyrepo\fR containing a clone of Alice\(cqs repository\&. The clone is on an equal footing with the original project, possessing its own copy of the original project\(cqs history\&.
  423. .sp
  424. Bob then makes some changes and commits them:
  425. .sp
  426. .if n \{\
  427. .RS 4
  428. .\}
  429. .nf
  430. (edit files)
  431. bob$ git commit \-a
  432. (repeat as necessary)
  433. .fi
  434. .if n \{\
  435. .RE
  436. .\}
  437. .sp
  438. When he\(cqs ready, he tells Alice to pull changes from the repository at \fB/home/bob/myrepo\fR\&. She does this with:
  439. .sp
  440. .if n \{\
  441. .RS 4
  442. .\}
  443. .nf
  444. alice$ cd /home/alice/project
  445. alice$ git pull /home/bob/myrepo master
  446. .fi
  447. .if n \{\
  448. .RE
  449. .\}
  450. .sp
  451. This merges the changes from Bob\(cqs \fBmaster\fR branch into Alice\(cqs current branch\&. If Alice has made her own changes in the meantime, then she may need to manually fix any conflicts\&.
  452. .sp
  453. The \fBpull\fR command thus performs two operations: it fetches changes from a remote branch, then merges them into the current branch\&.
  454. .sp
  455. Note that in general, Alice would want her local changes committed before initiating this \fBpull\fR\&. If Bob\(cqs work conflicts with what Alice did since their histories forked, Alice will use her working tree and the index to resolve conflicts, and existing local changes will interfere with the conflict resolution process (Git will still perform the fetch but will refuse to merge \(em Alice will have to get rid of her local changes in some way and pull again when this happens)\&.
  456. .sp
  457. Alice can peek at what Bob did without merging first, using the \fBfetch\fR command; this allows Alice to inspect what Bob did, using a special symbol \fBFETCH_HEAD\fR, in order to determine if he has anything worth pulling, like this:
  458. .sp
  459. .if n \{\
  460. .RS 4
  461. .\}
  462. .nf
  463. alice$ git fetch /home/bob/myrepo master
  464. alice$ git log \-p HEAD\&.\&.FETCH_HEAD
  465. .fi
  466. .if n \{\
  467. .RE
  468. .\}
  469. .sp
  470. This operation is safe even if Alice has uncommitted local changes\&. The range notation \fBHEAD\fR\fB\&.\&.\fR\fBFETCH_HEAD\fR means "show everything that is reachable from the \fBFETCH_HEAD\fR but exclude anything that is reachable from \fBHEAD\fR"\&. Alice already knows everything that leads to her current state (\fBHEAD\fR), and reviews what Bob has in his state (\fBFETCH_HEAD\fR) that she has not seen with this command\&.
  471. .sp
  472. If Alice wants to visualize what Bob did since their histories forked she can issue the following command:
  473. .sp
  474. .if n \{\
  475. .RS 4
  476. .\}
  477. .nf
  478. $ gitk HEAD\&.\&.FETCH_HEAD
  479. .fi
  480. .if n \{\
  481. .RE
  482. .\}
  483. .sp
  484. This uses the same two\-dot range notation we saw earlier with \fBgit\fR \fBlog\fR\&.
  485. .sp
  486. Alice may want to view what both of them did since they forked\&. She can use three\-dot form instead of the two\-dot form:
  487. .sp
  488. .if n \{\
  489. .RS 4
  490. .\}
  491. .nf
  492. $ gitk HEAD\&.\&.\&.FETCH_HEAD
  493. .fi
  494. .if n \{\
  495. .RE
  496. .\}
  497. .sp
  498. This means "show everything that is reachable from either one, but exclude anything that is reachable from both of them"\&.
  499. .sp
  500. Please note that these range notations can be used with both \fBgitk\fR and \fBgit\fR \fBlog\fR\&.
  501. .sp
  502. After inspecting what Bob did, if there is nothing urgent, Alice may decide to continue working without pulling from Bob\&. If Bob\(cqs history does have something Alice would immediately need, Alice may choose to stash her work\-in\-progress first, do a \fBpull\fR, and then finally unstash her work\-in\-progress on top of the resulting history\&.
  503. .sp
  504. When you are working in a small closely knit group, it is not unusual to interact with the same repository over and over again\&. By defining \fIremote\fR repository shorthand, you can make it easier:
  505. .sp
  506. .if n \{\
  507. .RS 4
  508. .\}
  509. .nf
  510. alice$ git remote add bob /home/bob/myrepo
  511. .fi
  512. .if n \{\
  513. .RE
  514. .\}
  515. .sp
  516. With this, Alice can perform the first part of the \fBpull\fR operation alone using the \fBgit\fR \fBfetch\fR command without merging them with her own branch, using:
  517. .sp
  518. .if n \{\
  519. .RS 4
  520. .\}
  521. .nf
  522. alice$ git fetch bob
  523. .fi
  524. .if n \{\
  525. .RE
  526. .\}
  527. .sp
  528. Unlike the longhand form, when Alice fetches from Bob using a remote repository shorthand set up with \fBgit\fR \fBremote\fR, what was fetched is stored in a remote\-tracking branch, in this case \fBbob/master\fR\&. So after this:
  529. .sp
  530. .if n \{\
  531. .RS 4
  532. .\}
  533. .nf
  534. alice$ git log \-p master\&.\&.bob/master
  535. .fi
  536. .if n \{\
  537. .RE
  538. .\}
  539. .sp
  540. shows a list of all the changes that Bob made since he branched from Alice\(cqs \fBmaster\fR branch\&.
  541. .sp
  542. After examining those changes, Alice could merge the changes into her \fBmaster\fR branch:
  543. .sp
  544. .if n \{\
  545. .RS 4
  546. .\}
  547. .nf
  548. alice$ git merge bob/master
  549. .fi
  550. .if n \{\
  551. .RE
  552. .\}
  553. .sp
  554. This \fBmerge\fR can also be done by \fIpulling from her own remote\-tracking branch\fR, like this:
  555. .sp
  556. .if n \{\
  557. .RS 4
  558. .\}
  559. .nf
  560. alice$ git pull \&. remotes/bob/master
  561. .fi
  562. .if n \{\
  563. .RE
  564. .\}
  565. .sp
  566. Note that git pull always merges into the current branch, regardless of what else is given on the command line\&.
  567. .sp
  568. Later, Bob can update his repo with Alice\(cqs latest changes using
  569. .sp
  570. .if n \{\
  571. .RS 4
  572. .\}
  573. .nf
  574. bob$ git pull
  575. .fi
  576. .if n \{\
  577. .RE
  578. .\}
  579. .sp
  580. Note that he doesn\(cqt need to give the path to Alice\(cqs repository; when Bob cloned Alice\(cqs repository, Git stored the location of her repository in the repository configuration, and that location is used for pulls:
  581. .sp
  582. .if n \{\
  583. .RS 4
  584. .\}
  585. .nf
  586. bob$ git config \-\-get remote\&.origin\&.url
  587. /home/alice/project
  588. .fi
  589. .if n \{\
  590. .RE
  591. .\}
  592. .sp
  593. (The complete configuration created by \fBgit\fR \fBclone\fR is visible using \fBgit\fR \fBconfig\fR \fB\-l\fR, and the \fBgit-config\fR(1) man page explains the meaning of each option\&.)
  594. .sp
  595. Git also keeps a pristine copy of Alice\(cqs \fBmaster\fR branch under the name \fBorigin/master\fR:
  596. .sp
  597. .if n \{\
  598. .RS 4
  599. .\}
  600. .nf
  601. bob$ git branch \-r
  602. origin/master
  603. .fi
  604. .if n \{\
  605. .RE
  606. .\}
  607. .sp
  608. If Bob later decides to work from a different host, he can still perform clones and pulls using the ssh protocol:
  609. .sp
  610. .if n \{\
  611. .RS 4
  612. .\}
  613. .nf
  614. bob$ git clone alice\&.org:/home/alice/project myrepo
  615. .fi
  616. .if n \{\
  617. .RE
  618. .\}
  619. .sp
  620. Alternatively, Git has a native protocol, or can use http; see \fBgit-pull\fR(1) for details\&.
  621. .sp
  622. Git can also be used in a CVS\-like mode, with a central repository that various users push changes to; see \fBgit-push\fR(1) and \fBgitcvs-migration\fR(7)\&.
  623. .SH "EXPLORING HISTORY"
  624. .sp
  625. Git history is represented as a series of interrelated commits\&. We have already seen that the \fBgit\fR \fBlog\fR command can list those commits\&. Note that first line of each \fBgit\fR \fBlog\fR entry also gives a name for the commit:
  626. .sp
  627. .if n \{\
  628. .RS 4
  629. .\}
  630. .nf
  631. $ git log
  632. commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
  633. Author: Junio C Hamano <junkio@cox\&.net>
  634. Date: Tue May 16 17:18:22 2006 \-0700
  635. merge\-base: Clarify the comments on post processing\&.
  636. .fi
  637. .if n \{\
  638. .RE
  639. .\}
  640. .sp
  641. We can give this name to \fBgit\fR \fBshow\fR to see the details about this commit\&.
  642. .sp
  643. .if n \{\
  644. .RS 4
  645. .\}
  646. .nf
  647. $ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
  648. .fi
  649. .if n \{\
  650. .RE
  651. .\}
  652. .sp
  653. But there are other ways to refer to commits\&. You can use any initial part of the name that is long enough to uniquely identify the commit:
  654. .sp
  655. .if n \{\
  656. .RS 4
  657. .\}
  658. .nf
  659. $ git show c82a22c39c # the first few characters of the name are
  660. # usually enough
  661. $ git show HEAD # the tip of the current branch
  662. $ git show experimental # the tip of the "experimental" branch
  663. .fi
  664. .if n \{\
  665. .RE
  666. .\}
  667. .sp
  668. Every commit usually has one "parent" commit which points to the previous state of the project:
  669. .sp
  670. .if n \{\
  671. .RS 4
  672. .\}
  673. .nf
  674. $ git show HEAD^ # to see the parent of HEAD
  675. $ git show HEAD^^ # to see the grandparent of HEAD
  676. $ git show HEAD~4 # to see the great\-great grandparent of HEAD
  677. .fi
  678. .if n \{\
  679. .RE
  680. .\}
  681. .sp
  682. Note that merge commits may have more than one parent:
  683. .sp
  684. .if n \{\
  685. .RS 4
  686. .\}
  687. .nf
  688. $ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
  689. $ git show HEAD^2 # show the second parent of HEAD
  690. .fi
  691. .if n \{\
  692. .RE
  693. .\}
  694. .sp
  695. You can also give commits names of your own; after running
  696. .sp
  697. .if n \{\
  698. .RS 4
  699. .\}
  700. .nf
  701. $ git tag v2\&.5 1b2e1d63ff
  702. .fi
  703. .if n \{\
  704. .RE
  705. .\}
  706. .sp
  707. you can refer to \fB1b2e1d63ff\fR by the name \fBv2\&.5\fR\&. If you intend to share this name with other people (for example, to identify a release version), you should create a "tag" object, and perhaps sign it; see \fBgit-tag\fR(1) for details\&.
  708. .sp
  709. Any Git command that needs to know a commit can take any of these names\&. For example:
  710. .sp
  711. .if n \{\
  712. .RS 4
  713. .\}
  714. .nf
  715. $ git diff v2\&.5 HEAD # compare the current HEAD to v2\&.5
  716. $ git branch stable v2\&.5 # start a new branch named "stable" based
  717. # at v2\&.5
  718. $ git reset \-\-hard HEAD^ # reset your current branch and working
  719. # directory to its state at HEAD^
  720. .fi
  721. .if n \{\
  722. .RE
  723. .\}
  724. .sp
  725. Be careful with that last command: in addition to losing any changes in the working directory, it will also remove all later commits from this branch\&. If this branch is the only branch containing those commits, they will be lost\&. Also, don\(cqt use \fBgit\fR \fBreset\fR on a publicly\-visible branch that other developers pull from, as it will force needless merges on other developers to clean up the history\&. If you need to undo changes that you have pushed, use \fBgit\fR \fBrevert\fR instead\&.
  726. .sp
  727. The \fBgit\fR \fBgrep\fR command can search for strings in any version of your project, so
  728. .sp
  729. .if n \{\
  730. .RS 4
  731. .\}
  732. .nf
  733. $ git grep "hello" v2\&.5
  734. .fi
  735. .if n \{\
  736. .RE
  737. .\}
  738. .sp
  739. searches for all occurrences of "hello" in \fBv2\&.5\fR\&.
  740. .sp
  741. If you leave out the commit name, \fBgit\fR \fBgrep\fR will search any of the files it manages in your current directory\&. So
  742. .sp
  743. .if n \{\
  744. .RS 4
  745. .\}
  746. .nf
  747. $ git grep "hello"
  748. .fi
  749. .if n \{\
  750. .RE
  751. .\}
  752. .sp
  753. is a quick way to search just the files that are tracked by Git\&.
  754. .sp
  755. Many Git commands also take sets of commits, which can be specified in a number of ways\&. Here are some examples with \fBgit\fR \fBlog\fR:
  756. .sp
  757. .if n \{\
  758. .RS 4
  759. .\}
  760. .nf
  761. $ git log v2\&.5\&.\&.v2\&.6 # commits between v2\&.5 and v2\&.6
  762. $ git log v2\&.5\&.\&. # commits since v2\&.5
  763. $ git log \-\-since="2 weeks ago" # commits from the last 2 weeks
  764. $ git log v2\&.5\&.\&. Makefile # commits since v2\&.5 which modify
  765. # Makefile
  766. .fi
  767. .if n \{\
  768. .RE
  769. .\}
  770. .sp
  771. You can also give \fBgit\fR \fBlog\fR a "range" of commits where the first is not necessarily an ancestor of the second; for example, if the tips of the branches \fBstable\fR and \fBmaster\fR diverged from a common commit some time ago, then
  772. .sp
  773. .if n \{\
  774. .RS 4
  775. .\}
  776. .nf
  777. $ git log stable\&.\&.master
  778. .fi
  779. .if n \{\
  780. .RE
  781. .\}
  782. .sp
  783. will list commits made in the \fBmaster\fR branch but not in the stable branch, while
  784. .sp
  785. .if n \{\
  786. .RS 4
  787. .\}
  788. .nf
  789. $ git log master\&.\&.stable
  790. .fi
  791. .if n \{\
  792. .RE
  793. .\}
  794. .sp
  795. will show the list of commits made on the stable branch but not the \fBmaster\fR branch\&.
  796. .sp
  797. The \fBgit\fR \fBlog\fR command has a weakness: it must present commits in a list\&. When the history has lines of development that diverged and then merged back together, the order in which \fBgit\fR \fBlog\fR presents those commits is meaningless\&.
  798. .sp
  799. Most projects with multiple contributors (such as the Linux kernel, or Git itself) have frequent merges, and \fBgitk\fR does a better job of visualizing their history\&. For example,
  800. .sp
  801. .if n \{\
  802. .RS 4
  803. .\}
  804. .nf
  805. $ gitk \-\-since="2 weeks ago" drivers/
  806. .fi
  807. .if n \{\
  808. .RE
  809. .\}
  810. .sp
  811. allows you to browse any commits from the last 2 weeks of commits that modified files under the \fBdrivers\fR directory\&. (Note: you can adjust gitk\(cqs fonts by holding down the control key while pressing "\-" or "+"\&.)
  812. .sp
  813. Finally, most commands that take filenames will optionally allow you to precede any filename by a commit, to specify a particular version of the file:
  814. .sp
  815. .if n \{\
  816. .RS 4
  817. .\}
  818. .nf
  819. $ git diff v2\&.5:Makefile HEAD:Makefile\&.in
  820. .fi
  821. .if n \{\
  822. .RE
  823. .\}
  824. .sp
  825. You can also use \fBgit\fR \fBshow\fR to see any such file:
  826. .sp
  827. .if n \{\
  828. .RS 4
  829. .\}
  830. .nf
  831. $ git show v2\&.5:Makefile
  832. .fi
  833. .if n \{\
  834. .RE
  835. .\}
  836. .SH "NEXT STEPS"
  837. .sp
  838. This tutorial should be enough to perform basic distributed revision control for your projects\&. However, to fully understand the depth and power of Git you need to understand two simple ideas on which it is based:
  839. .sp
  840. .RS 4
  841. .ie n \{\
  842. \h'-04'\(bu\h'+03'\c
  843. .\}
  844. .el \{\
  845. .sp -1
  846. .IP \(bu 2.3
  847. .\}
  848. The object database is the rather elegant system used to store the history of your project\(em\:files, directories, and commits\&.
  849. .RE
  850. .sp
  851. .RS 4
  852. .ie n \{\
  853. \h'-04'\(bu\h'+03'\c
  854. .\}
  855. .el \{\
  856. .sp -1
  857. .IP \(bu 2.3
  858. .\}
  859. The index file is a cache of the state of a directory tree, used to create commits, check out working directories, and hold the various trees involved in a merge\&.
  860. .RE
  861. .sp
  862. Part two of this tutorial explains the object database, the index file, and a few other odds and ends that you\(cqll need to make the most of Git\&. You can find it at \fBgittutorial-2\fR(7)\&.
  863. .sp
  864. If you don\(cqt want to continue with that right away, a few other digressions that may be interesting at this point are:
  865. .sp
  866. .RS 4
  867. .ie n \{\
  868. \h'-04'\(bu\h'+03'\c
  869. .\}
  870. .el \{\
  871. .sp -1
  872. .IP \(bu 2.3
  873. .\}
  874. \fBgit-format-patch\fR(1),
  875. \fBgit-am\fR(1): These convert series of git commits into emailed patches, and vice versa, useful for projects such as the Linux kernel which rely heavily on emailed patches\&.
  876. .RE
  877. .sp
  878. .RS 4
  879. .ie n \{\
  880. \h'-04'\(bu\h'+03'\c
  881. .\}
  882. .el \{\
  883. .sp -1
  884. .IP \(bu 2.3
  885. .\}
  886. \fBgit-bisect\fR(1): When there is a regression in your project, one way to track down the bug is by searching through the history to find the exact commit that\(cqs to blame\&.
  887. \fBgit\fR
  888. \fBbisect\fR
  889. can help you perform a binary search for that commit\&. It is smart enough to perform a close\-to\-optimal search even in the case of complex non\-linear history with lots of merged branches\&.
  890. .RE
  891. .sp
  892. .RS 4
  893. .ie n \{\
  894. \h'-04'\(bu\h'+03'\c
  895. .\}
  896. .el \{\
  897. .sp -1
  898. .IP \(bu 2.3
  899. .\}
  900. \fBgitworkflows\fR(7): Gives an overview of recommended workflows\&.
  901. .RE
  902. .sp
  903. .RS 4
  904. .ie n \{\
  905. \h'-04'\(bu\h'+03'\c
  906. .\}
  907. .el \{\
  908. .sp -1
  909. .IP \(bu 2.3
  910. .\}
  911. \fBgiteveryday\fR(7): Everyday Git with 20 Commands Or So\&.
  912. .RE
  913. .sp
  914. .RS 4
  915. .ie n \{\
  916. \h'-04'\(bu\h'+03'\c
  917. .\}
  918. .el \{\
  919. .sp -1
  920. .IP \(bu 2.3
  921. .\}
  922. \fBgitcvs-migration\fR(7): Git for CVS users\&.
  923. .RE
  924. .SH "SEE ALSO"
  925. .sp
  926. \fBgittutorial-2\fR(7), \fBgitcvs-migration\fR(7), \fBgitcore-tutorial\fR(7), \fBgitglossary\fR(7), \fBgit-help\fR(1), \fBgitworkflows\fR(7), \fBgiteveryday\fR(7), \m[blue]\fBThe Git User\(cqs Manual\fR\m[]\&\s-2\u[1]\d\s+2
  927. .SH "GIT"
  928. .sp
  929. Part of the \fBgit\fR(1) suite
  930. .SH "NOTES"
  931. .IP " 1." 4
  932. The Git User\(cqs Manual
  933. .RS 4
  934. \%git-htmldocs/user-manual.html
  935. .RE