THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

678 lines
22KB

  1. gittutorial(7)
  2. ==============
  3. NAME
  4. ----
  5. gittutorial - A tutorial introduction to Git
  6. SYNOPSIS
  7. --------
  8. [verse]
  9. git *
  10. DESCRIPTION
  11. -----------
  12. This tutorial explains how to import a new project into Git, make
  13. changes to it, and share changes with other developers.
  14. If you are instead primarily interested in using Git to fetch a project,
  15. for example, to test the latest version, you may prefer to start with
  16. the first two chapters of link:user-manual.html[The Git User's Manual].
  17. First, note that you can get documentation for a command such as
  18. `git log --graph` with:
  19. ------------------------------------------------
  20. $ man git-log
  21. ------------------------------------------------
  22. or:
  23. ------------------------------------------------
  24. $ git help log
  25. ------------------------------------------------
  26. With the latter, you can use the manual viewer of your choice; see
  27. linkgit:git-help[1] for more information.
  28. It is a good idea to introduce yourself to Git with your name and
  29. public email address before doing any operation. The easiest
  30. way to do so is:
  31. ------------------------------------------------
  32. $ git config --global user.name "Your Name Comes Here"
  33. $ git config --global user.email you@yourdomain.example.com
  34. ------------------------------------------------
  35. Importing a new project
  36. -----------------------
  37. Assume you have a tarball project.tar.gz with your initial work. You
  38. can place it under Git revision control as follows.
  39. ------------------------------------------------
  40. $ tar xzf project.tar.gz
  41. $ cd project
  42. $ git init
  43. ------------------------------------------------
  44. Git will reply
  45. ------------------------------------------------
  46. Initialized empty Git repository in .git/
  47. ------------------------------------------------
  48. You've now initialized the working directory--you may notice a new
  49. directory created, named ".git".
  50. Next, tell Git to take a snapshot of the contents of all files under the
  51. current directory (note the '.'), with 'git add':
  52. ------------------------------------------------
  53. $ git add .
  54. ------------------------------------------------
  55. This snapshot is now stored in a temporary staging area which Git calls
  56. the "index". You can permanently store the contents of the index in the
  57. repository with 'git commit':
  58. ------------------------------------------------
  59. $ git commit
  60. ------------------------------------------------
  61. This will prompt you for a commit message. You've now stored the first
  62. version of your project in Git.
  63. Making changes
  64. --------------
  65. Modify some files, then add their updated contents to the index:
  66. ------------------------------------------------
  67. $ git add file1 file2 file3
  68. ------------------------------------------------
  69. You are now ready to commit. You can see what is about to be committed
  70. using 'git diff' with the --cached option:
  71. ------------------------------------------------
  72. $ git diff --cached
  73. ------------------------------------------------
  74. (Without --cached, 'git diff' will show you any changes that
  75. you've made but not yet added to the index.) You can also get a brief
  76. summary of the situation with 'git status':
  77. ------------------------------------------------
  78. $ git status
  79. On branch master
  80. Changes to be committed:
  81. Your branch is up to date with 'origin/master'.
  82. (use "git restore --staged <file>..." to unstage)
  83. modified: file1
  84. modified: file2
  85. modified: file3
  86. ------------------------------------------------
  87. If you need to make any further adjustments, do so now, and then add any
  88. newly modified content to the index. Finally, commit your changes with:
  89. ------------------------------------------------
  90. $ git commit
  91. ------------------------------------------------
  92. This will again prompt you for a message describing the change, and then
  93. record a new version of the project.
  94. Alternatively, instead of running 'git add' beforehand, you can use
  95. ------------------------------------------------
  96. $ git commit -a
  97. ------------------------------------------------
  98. which will automatically notice any modified (but not new) files, add
  99. them to the index, and commit, all in one step.
  100. A note on commit messages: Though not required, it's a good idea to
  101. begin the commit message with a single short (less than 50 character)
  102. line summarizing the change, followed by a blank line and then a more
  103. thorough description. The text up to the first blank line in a commit
  104. message is treated as the commit title, and that title is used
  105. throughout Git. For example, linkgit:git-format-patch[1] turns a
  106. commit into email, and it uses the title on the Subject line and the
  107. rest of the commit in the body.
  108. Git tracks content not files
  109. ----------------------------
  110. Many revision control systems provide an `add` command that tells the
  111. system to start tracking changes to a new file. Git's `add` command
  112. does something simpler and more powerful: 'git add' is used both for new
  113. and newly modified files, and in both cases it takes a snapshot of the
  114. given files and stages that content in the index, ready for inclusion in
  115. the next commit.
  116. Viewing project history
  117. -----------------------
  118. At any point you can view the history of your changes using
  119. ------------------------------------------------
  120. $ git log
  121. ------------------------------------------------
  122. If you also want to see complete diffs at each step, use
  123. ------------------------------------------------
  124. $ git log -p
  125. ------------------------------------------------
  126. Often the overview of the change is useful to get a feel of
  127. each step
  128. ------------------------------------------------
  129. $ git log --stat --summary
  130. ------------------------------------------------
  131. Managing branches
  132. -----------------
  133. A single Git repository can maintain multiple branches of
  134. development. To create a new branch named "experimental", use
  135. ------------------------------------------------
  136. $ git branch experimental
  137. ------------------------------------------------
  138. If you now run
  139. ------------------------------------------------
  140. $ git branch
  141. ------------------------------------------------
  142. you'll get a list of all existing branches:
  143. ------------------------------------------------
  144. experimental
  145. * master
  146. ------------------------------------------------
  147. The "experimental" branch is the one you just created, and the
  148. "master" branch is a default branch that was created for you
  149. automatically. The asterisk marks the branch you are currently on;
  150. type
  151. ------------------------------------------------
  152. $ git switch experimental
  153. ------------------------------------------------
  154. to switch to the experimental branch. Now edit a file, commit the
  155. change, and switch back to the master branch:
  156. ------------------------------------------------
  157. (edit file)
  158. $ git commit -a
  159. $ git switch master
  160. ------------------------------------------------
  161. Check that the change you made is no longer visible, since it was
  162. made on the experimental branch and you're back on the master branch.
  163. You can make a different change on the master branch:
  164. ------------------------------------------------
  165. (edit file)
  166. $ git commit -a
  167. ------------------------------------------------
  168. at this point the two branches have diverged, with different changes
  169. made in each. To merge the changes made in experimental into master, run
  170. ------------------------------------------------
  171. $ git merge experimental
  172. ------------------------------------------------
  173. If the changes don't conflict, you're done. If there are conflicts,
  174. markers will be left in the problematic files showing the conflict;
  175. ------------------------------------------------
  176. $ git diff
  177. ------------------------------------------------
  178. will show this. Once you've edited the files to resolve the
  179. conflicts,
  180. ------------------------------------------------
  181. $ git commit -a
  182. ------------------------------------------------
  183. will commit the result of the merge. Finally,
  184. ------------------------------------------------
  185. $ gitk
  186. ------------------------------------------------
  187. will show a nice graphical representation of the resulting history.
  188. At this point you could delete the experimental branch with
  189. ------------------------------------------------
  190. $ git branch -d experimental
  191. ------------------------------------------------
  192. This command ensures that the changes in the experimental branch are
  193. already in the current branch.
  194. If you develop on a branch crazy-idea, then regret it, you can always
  195. delete the branch with
  196. -------------------------------------
  197. $ git branch -D crazy-idea
  198. -------------------------------------
  199. Branches are cheap and easy, so this is a good way to try something
  200. out.
  201. Using Git for collaboration
  202. ---------------------------
  203. Suppose that Alice has started a new project with a Git repository in
  204. /home/alice/project, and that Bob, who has a home directory on the
  205. same machine, wants to contribute.
  206. Bob begins with:
  207. ------------------------------------------------
  208. bob$ git clone /home/alice/project myrepo
  209. ------------------------------------------------
  210. This creates a new directory "myrepo" containing a clone of Alice's
  211. repository. The clone is on an equal footing with the original
  212. project, possessing its own copy of the original project's history.
  213. Bob then makes some changes and commits them:
  214. ------------------------------------------------
  215. (edit files)
  216. bob$ git commit -a
  217. (repeat as necessary)
  218. ------------------------------------------------
  219. When he's ready, he tells Alice to pull changes from the repository
  220. at /home/bob/myrepo. She does this with:
  221. ------------------------------------------------
  222. alice$ cd /home/alice/project
  223. alice$ git pull /home/bob/myrepo master
  224. ------------------------------------------------
  225. This merges the changes from Bob's "master" branch into Alice's
  226. current branch. If Alice has made her own changes in the meantime,
  227. then she may need to manually fix any conflicts.
  228. The "pull" command thus performs two operations: it fetches changes
  229. from a remote branch, then merges them into the current branch.
  230. Note that in general, Alice would want her local changes committed before
  231. initiating this "pull". If Bob's work conflicts with what Alice did since
  232. their histories forked, Alice will use her working tree and the index to
  233. resolve conflicts, and existing local changes will interfere with the
  234. conflict resolution process (Git will still perform the fetch but will
  235. refuse to merge --- Alice will have to get rid of her local changes in
  236. some way and pull again when this happens).
  237. Alice can peek at what Bob did without merging first, using the "fetch"
  238. command; this allows Alice to inspect what Bob did, using a special
  239. symbol "FETCH_HEAD", in order to determine if he has anything worth
  240. pulling, like this:
  241. ------------------------------------------------
  242. alice$ git fetch /home/bob/myrepo master
  243. alice$ git log -p HEAD..FETCH_HEAD
  244. ------------------------------------------------
  245. This operation is safe even if Alice has uncommitted local changes.
  246. The range notation "HEAD..FETCH_HEAD" means "show everything that is reachable
  247. from the FETCH_HEAD but exclude anything that is reachable from HEAD".
  248. Alice already knows everything that leads to her current state (HEAD),
  249. and reviews what Bob has in his state (FETCH_HEAD) that she has not
  250. seen with this command.
  251. If Alice wants to visualize what Bob did since their histories forked
  252. she can issue the following command:
  253. ------------------------------------------------
  254. $ gitk HEAD..FETCH_HEAD
  255. ------------------------------------------------
  256. This uses the same two-dot range notation we saw earlier with 'git log'.
  257. Alice may want to view what both of them did since they forked.
  258. She can use three-dot form instead of the two-dot form:
  259. ------------------------------------------------
  260. $ gitk HEAD...FETCH_HEAD
  261. ------------------------------------------------
  262. This means "show everything that is reachable from either one, but
  263. exclude anything that is reachable from both of them".
  264. Please note that these range notation can be used with both gitk
  265. and "git log".
  266. After inspecting what Bob did, if there is nothing urgent, Alice may
  267. decide to continue working without pulling from Bob. If Bob's history
  268. does have something Alice would immediately need, Alice may choose to
  269. stash her work-in-progress first, do a "pull", and then finally unstash
  270. her work-in-progress on top of the resulting history.
  271. When you are working in a small closely knit group, it is not
  272. unusual to interact with the same repository over and over
  273. again. By defining 'remote' repository shorthand, you can make
  274. it easier:
  275. ------------------------------------------------
  276. alice$ git remote add bob /home/bob/myrepo
  277. ------------------------------------------------
  278. With this, Alice can perform the first part of the "pull" operation
  279. alone using the 'git fetch' command without merging them with her own
  280. branch, using:
  281. -------------------------------------
  282. alice$ git fetch bob
  283. -------------------------------------
  284. Unlike the longhand form, when Alice fetches from Bob using a
  285. remote repository shorthand set up with 'git remote', what was
  286. fetched is stored in a remote-tracking branch, in this case
  287. `bob/master`. So after this:
  288. -------------------------------------
  289. alice$ git log -p master..bob/master
  290. -------------------------------------
  291. shows a list of all the changes that Bob made since he branched from
  292. Alice's master branch.
  293. After examining those changes, Alice
  294. could merge the changes into her master branch:
  295. -------------------------------------
  296. alice$ git merge bob/master
  297. -------------------------------------
  298. This `merge` can also be done by 'pulling from her own remote-tracking
  299. branch', like this:
  300. -------------------------------------
  301. alice$ git pull . remotes/bob/master
  302. -------------------------------------
  303. Note that git pull always merges into the current branch,
  304. regardless of what else is given on the command line.
  305. Later, Bob can update his repo with Alice's latest changes using
  306. -------------------------------------
  307. bob$ git pull
  308. -------------------------------------
  309. Note that he doesn't need to give the path to Alice's repository;
  310. when Bob cloned Alice's repository, Git stored the location of her
  311. repository in the repository configuration, and that location is
  312. used for pulls:
  313. -------------------------------------
  314. bob$ git config --get remote.origin.url
  315. /home/alice/project
  316. -------------------------------------
  317. (The complete configuration created by 'git clone' is visible using
  318. `git config -l`, and the linkgit:git-config[1] man page
  319. explains the meaning of each option.)
  320. Git also keeps a pristine copy of Alice's master branch under the
  321. name "origin/master":
  322. -------------------------------------
  323. bob$ git branch -r
  324. origin/master
  325. -------------------------------------
  326. If Bob later decides to work from a different host, he can still
  327. perform clones and pulls using the ssh protocol:
  328. -------------------------------------
  329. bob$ git clone alice.org:/home/alice/project myrepo
  330. -------------------------------------
  331. Alternatively, Git has a native protocol, or can use http;
  332. see linkgit:git-pull[1] for details.
  333. Git can also be used in a CVS-like mode, with a central repository
  334. that various users push changes to; see linkgit:git-push[1] and
  335. linkgit:gitcvs-migration[7].
  336. Exploring history
  337. -----------------
  338. Git history is represented as a series of interrelated commits. We
  339. have already seen that the 'git log' command can list those commits.
  340. Note that first line of each git log entry also gives a name for the
  341. commit:
  342. -------------------------------------
  343. $ git log
  344. commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
  345. Author: Junio C Hamano <junkio@cox.net>
  346. Date: Tue May 16 17:18:22 2006 -0700
  347. merge-base: Clarify the comments on post processing.
  348. -------------------------------------
  349. We can give this name to 'git show' to see the details about this
  350. commit.
  351. -------------------------------------
  352. $ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
  353. -------------------------------------
  354. But there are other ways to refer to commits. You can use any initial
  355. part of the name that is long enough to uniquely identify the commit:
  356. -------------------------------------
  357. $ git show c82a22c39c # the first few characters of the name are
  358. # usually enough
  359. $ git show HEAD # the tip of the current branch
  360. $ git show experimental # the tip of the "experimental" branch
  361. -------------------------------------
  362. Every commit usually has one "parent" commit
  363. which points to the previous state of the project:
  364. -------------------------------------
  365. $ git show HEAD^ # to see the parent of HEAD
  366. $ git show HEAD^^ # to see the grandparent of HEAD
  367. $ git show HEAD~4 # to see the great-great grandparent of HEAD
  368. -------------------------------------
  369. Note that merge commits may have more than one parent:
  370. -------------------------------------
  371. $ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
  372. $ git show HEAD^2 # show the second parent of HEAD
  373. -------------------------------------
  374. You can also give commits names of your own; after running
  375. -------------------------------------
  376. $ git tag v2.5 1b2e1d63ff
  377. -------------------------------------
  378. you can refer to 1b2e1d63ff by the name "v2.5". If you intend to
  379. share this name with other people (for example, to identify a release
  380. version), you should create a "tag" object, and perhaps sign it; see
  381. linkgit:git-tag[1] for details.
  382. Any Git command that needs to know a commit can take any of these
  383. names. For example:
  384. -------------------------------------
  385. $ git diff v2.5 HEAD # compare the current HEAD to v2.5
  386. $ git branch stable v2.5 # start a new branch named "stable" based
  387. # at v2.5
  388. $ git reset --hard HEAD^ # reset your current branch and working
  389. # directory to its state at HEAD^
  390. -------------------------------------
  391. Be careful with that last command: in addition to losing any changes
  392. in the working directory, it will also remove all later commits from
  393. this branch. If this branch is the only branch containing those
  394. commits, they will be lost. Also, don't use 'git reset' on a
  395. publicly-visible branch that other developers pull from, as it will
  396. force needless merges on other developers to clean up the history.
  397. If you need to undo changes that you have pushed, use 'git revert'
  398. instead.
  399. The 'git grep' command can search for strings in any version of your
  400. project, so
  401. -------------------------------------
  402. $ git grep "hello" v2.5
  403. -------------------------------------
  404. searches for all occurrences of "hello" in v2.5.
  405. If you leave out the commit name, 'git grep' will search any of the
  406. files it manages in your current directory. So
  407. -------------------------------------
  408. $ git grep "hello"
  409. -------------------------------------
  410. is a quick way to search just the files that are tracked by Git.
  411. Many Git commands also take sets of commits, which can be specified
  412. in a number of ways. Here are some examples with 'git log':
  413. -------------------------------------
  414. $ git log v2.5..v2.6 # commits between v2.5 and v2.6
  415. $ git log v2.5.. # commits since v2.5
  416. $ git log --since="2 weeks ago" # commits from the last 2 weeks
  417. $ git log v2.5.. Makefile # commits since v2.5 which modify
  418. # Makefile
  419. -------------------------------------
  420. You can also give 'git log' a "range" of commits where the first is not
  421. necessarily an ancestor of the second; for example, if the tips of
  422. the branches "stable" and "master" diverged from a common
  423. commit some time ago, then
  424. -------------------------------------
  425. $ git log stable..master
  426. -------------------------------------
  427. will list commits made in the master branch but not in the
  428. stable branch, while
  429. -------------------------------------
  430. $ git log master..stable
  431. -------------------------------------
  432. will show the list of commits made on the stable branch but not
  433. the master branch.
  434. The 'git log' command has a weakness: it must present commits in a
  435. list. When the history has lines of development that diverged and
  436. then merged back together, the order in which 'git log' presents
  437. those commits is meaningless.
  438. Most projects with multiple contributors (such as the Linux kernel,
  439. or Git itself) have frequent merges, and 'gitk' does a better job of
  440. visualizing their history. For example,
  441. -------------------------------------
  442. $ gitk --since="2 weeks ago" drivers/
  443. -------------------------------------
  444. allows you to browse any commits from the last 2 weeks of commits
  445. that modified files under the "drivers" directory. (Note: you can
  446. adjust gitk's fonts by holding down the control key while pressing
  447. "-" or "+".)
  448. Finally, most commands that take filenames will optionally allow you
  449. to precede any filename by a commit, to specify a particular version
  450. of the file:
  451. -------------------------------------
  452. $ git diff v2.5:Makefile HEAD:Makefile.in
  453. -------------------------------------
  454. You can also use 'git show' to see any such file:
  455. -------------------------------------
  456. $ git show v2.5:Makefile
  457. -------------------------------------
  458. Next Steps
  459. ----------
  460. This tutorial should be enough to perform basic distributed revision
  461. control for your projects. However, to fully understand the depth
  462. and power of Git you need to understand two simple ideas on which it
  463. is based:
  464. * The object database is the rather elegant system used to
  465. store the history of your project--files, directories, and
  466. commits.
  467. * The index file is a cache of the state of a directory tree,
  468. used to create commits, check out working directories, and
  469. hold the various trees involved in a merge.
  470. Part two of this tutorial explains the object
  471. database, the index file, and a few other odds and ends that you'll
  472. need to make the most of Git. You can find it at linkgit:gittutorial-2[7].
  473. If you don't want to continue with that right away, a few other
  474. digressions that may be interesting at this point are:
  475. * linkgit:git-format-patch[1], linkgit:git-am[1]: These convert
  476. series of git commits into emailed patches, and vice versa,
  477. useful for projects such as the Linux kernel which rely heavily
  478. on emailed patches.
  479. * linkgit:git-bisect[1]: When there is a regression in your
  480. project, one way to track down the bug is by searching through
  481. the history to find the exact commit that's to blame. Git bisect
  482. can help you perform a binary search for that commit. It is
  483. smart enough to perform a close-to-optimal search even in the
  484. case of complex non-linear history with lots of merged branches.
  485. * linkgit:gitworkflows[7]: Gives an overview of recommended
  486. workflows.
  487. * linkgit:giteveryday[7]: Everyday Git with 20 Commands Or So.
  488. * linkgit:gitcvs-migration[7]: Git for CVS users.
  489. SEE ALSO
  490. --------
  491. linkgit:gittutorial-2[7],
  492. linkgit:gitcvs-migration[7],
  493. linkgit:gitcore-tutorial[7],
  494. linkgit:gitglossary[7],
  495. linkgit:git-help[1],
  496. linkgit:gitworkflows[7],
  497. linkgit:giteveryday[7],
  498. link:user-manual.html[The Git User's Manual]
  499. GIT
  500. ---
  501. Part of the linkgit:git[1] suite