gitcore-tutorial.txt 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  1. gitcore-tutorial(7)
  2. ===================
  3. NAME
  4. ----
  5. gitcore-tutorial - A Git core tutorial for developers
  6. SYNOPSIS
  7. --------
  8. git *
  9. DESCRIPTION
  10. -----------
  11. This tutorial explains how to use the "core" Git commands to set up and
  12. work with a Git repository.
  13. If you just need to use Git as a revision control system you may prefer
  14. to start with "A Tutorial Introduction to Git" (linkgit:gittutorial[7]) or
  15. link:user-manual.html[the Git User Manual].
  16. However, an understanding of these low-level tools can be helpful if
  17. you want to understand Git's internals.
  18. The core Git is often called "plumbing", with the prettier user
  19. interfaces on top of it called "porcelain". You may not want to use the
  20. plumbing directly very often, but it can be good to know what the
  21. plumbing does when the porcelain isn't flushing.
  22. Back when this document was originally written, many porcelain
  23. commands were shell scripts. For simplicity, it still uses them as
  24. examples to illustrate how plumbing is fit together to form the
  25. porcelain commands. The source tree includes some of these scripts in
  26. contrib/examples/ for reference. Although these are not implemented as
  27. shell scripts anymore, the description of what the plumbing layer
  28. commands do is still valid.
  29. [NOTE]
  30. Deeper technical details are often marked as Notes, which you can
  31. skip on your first reading.
  32. Creating a Git repository
  33. -------------------------
  34. Creating a new Git repository couldn't be easier: all Git repositories start
  35. out empty, and the only thing you need to do is find yourself a
  36. subdirectory that you want to use as a working tree - either an empty
  37. one for a totally new project, or an existing working tree that you want
  38. to import into Git.
  39. For our first example, we're going to start a totally new repository from
  40. scratch, with no pre-existing files, and we'll call it 'git-tutorial'.
  41. To start up, create a subdirectory for it, change into that
  42. subdirectory, and initialize the Git infrastructure with 'git init':
  43. ------------------------------------------------
  44. $ mkdir git-tutorial
  45. $ cd git-tutorial
  46. $ git init
  47. ------------------------------------------------
  48. to which Git will reply
  49. ----------------
  50. Initialized empty Git repository in .git/
  51. ----------------
  52. which is just Git's way of saying that you haven't been doing anything
  53. strange, and that it will have created a local `.git` directory setup for
  54. your new project. You will now have a `.git` directory, and you can
  55. inspect that with 'ls'. For your new empty project, it should show you
  56. three entries, among other things:
  57. - a file called `HEAD`, that has `ref: refs/heads/master` in it.
  58. This is similar to a symbolic link and points at
  59. `refs/heads/master` relative to the `HEAD` file.
  60. +
  61. Don't worry about the fact that the file that the `HEAD` link points to
  62. doesn't even exist yet -- you haven't created the commit that will
  63. start your `HEAD` development branch yet.
  64. - a subdirectory called `objects`, which will contain all the
  65. objects of your project. You should never have any real reason to
  66. look at the objects directly, but you might want to know that these
  67. objects are what contains all the real 'data' in your repository.
  68. - a subdirectory called `refs`, which contains references to objects.
  69. In particular, the `refs` subdirectory will contain two other
  70. subdirectories, named `heads` and `tags` respectively. They do
  71. exactly what their names imply: they contain references to any number
  72. of different 'heads' of development (aka 'branches'), and to any
  73. 'tags' that you have created to name specific versions in your
  74. repository.
  75. One note: the special `master` head is the default branch, which is
  76. why the `.git/HEAD` file was created points to it even if it
  77. doesn't yet exist. Basically, the `HEAD` link is supposed to always
  78. point to the branch you are working on right now, and you always
  79. start out expecting to work on the `master` branch.
  80. However, this is only a convention, and you can name your branches
  81. anything you want, and don't have to ever even 'have' a `master`
  82. branch. A number of the Git tools will assume that `.git/HEAD` is
  83. valid, though.
  84. [NOTE]
  85. An 'object' is identified by its 160-bit SHA-1 hash, aka 'object name',
  86. and a reference to an object is always the 40-byte hex
  87. representation of that SHA-1 name. The files in the `refs`
  88. subdirectory are expected to contain these hex references
  89. (usually with a final `\n` at the end), and you should thus
  90. expect to see a number of 41-byte files containing these
  91. references in these `refs` subdirectories when you actually start
  92. populating your tree.
  93. [NOTE]
  94. An advanced user may want to take a look at linkgit:gitrepository-layout[5]
  95. after finishing this tutorial.
  96. You have now created your first Git repository. Of course, since it's
  97. empty, that's not very useful, so let's start populating it with data.
  98. Populating a Git repository
  99. ---------------------------
  100. We'll keep this simple and stupid, so we'll start off with populating a
  101. few trivial files just to get a feel for it.
  102. Start off with just creating any random files that you want to maintain
  103. in your Git repository. We'll start off with a few bad examples, just to
  104. get a feel for how this works:
  105. ------------------------------------------------
  106. $ echo "Hello World" >hello
  107. $ echo "Silly example" >example
  108. ------------------------------------------------
  109. you have now created two files in your working tree (aka 'working directory'),
  110. but to actually check in your hard work, you will have to go through two steps:
  111. - fill in the 'index' file (aka 'cache') with the information about your
  112. working tree state.
  113. - commit that index file as an object.
  114. The first step is trivial: when you want to tell Git about any changes
  115. to your working tree, you use the 'git update-index' program. That
  116. program normally just takes a list of filenames you want to update, but
  117. to avoid trivial mistakes, it refuses to add new entries to the index
  118. (or remove existing ones) unless you explicitly tell it that you're
  119. adding a new entry with the `--add` flag (or removing an entry with the
  120. `--remove`) flag.
  121. So to populate the index with the two files you just created, you can do
  122. ------------------------------------------------
  123. $ git update-index --add hello example
  124. ------------------------------------------------
  125. and you have now told Git to track those two files.
  126. In fact, as you did that, if you now look into your object directory,
  127. you'll notice that Git will have added two new objects to the object
  128. database. If you did exactly the steps above, you should now be able to do
  129. ----------------
  130. $ ls .git/objects/??/*
  131. ----------------
  132. and see two files:
  133. ----------------
  134. .git/objects/55/7db03de997c86a4a028e1ebd3a1ceb225be238
  135. .git/objects/f2/4c74a2e500f5ee1332c86b94199f52b1d1d962
  136. ----------------
  137. which correspond with the objects with names of `557db...` and
  138. `f24c7...` respectively.
  139. If you want to, you can use 'git cat-file' to look at those objects, but
  140. you'll have to use the object name, not the filename of the object:
  141. ----------------
  142. $ git cat-file -t 557db03de997c86a4a028e1ebd3a1ceb225be238
  143. ----------------
  144. where the `-t` tells 'git cat-file' to tell you what the "type" of the
  145. object is. Git will tell you that you have a "blob" object (i.e., just a
  146. regular file), and you can see the contents with
  147. ----------------
  148. $ git cat-file blob 557db03
  149. ----------------
  150. which will print out "Hello World". The object `557db03` is nothing
  151. more than the contents of your file `hello`.
  152. [NOTE]
  153. Don't confuse that object with the file `hello` itself. The
  154. object is literally just those specific *contents* of the file, and
  155. however much you later change the contents in file `hello`, the object
  156. we just looked at will never change. Objects are immutable.
  157. [NOTE]
  158. The second example demonstrates that you can
  159. abbreviate the object name to only the first several
  160. hexadecimal digits in most places.
  161. Anyway, as we mentioned previously, you normally never actually take a
  162. look at the objects themselves, and typing long 40-character hex
  163. names is not something you'd normally want to do. The above digression
  164. was just to show that 'git update-index' did something magical, and
  165. actually saved away the contents of your files into the Git object
  166. database.
  167. Updating the index did something else too: it created a `.git/index`
  168. file. This is the index that describes your current working tree, and
  169. something you should be very aware of. Again, you normally never worry
  170. about the index file itself, but you should be aware of the fact that
  171. you have not actually really "checked in" your files into Git so far,
  172. you've only *told* Git about them.
  173. However, since Git knows about them, you can now start using some of the
  174. most basic Git commands to manipulate the files or look at their status.
  175. In particular, let's not even check in the two files into Git yet, we'll
  176. start off by adding another line to `hello` first:
  177. ------------------------------------------------
  178. $ echo "It's a new day for git" >>hello
  179. ------------------------------------------------
  180. and you can now, since you told Git about the previous state of `hello`, ask
  181. Git what has changed in the tree compared to your old index, using the
  182. 'git diff-files' command:
  183. ------------
  184. $ git diff-files
  185. ------------
  186. Oops. That wasn't very readable. It just spit out its own internal
  187. version of a 'diff', but that internal version really just tells you
  188. that it has noticed that "hello" has been modified, and that the old object
  189. contents it had have been replaced with something else.
  190. To make it readable, we can tell 'git diff-files' to output the
  191. differences as a patch, using the `-p` flag:
  192. ------------
  193. $ git diff-files -p
  194. diff --git a/hello b/hello
  195. index 557db03..263414f 100644
  196. --- a/hello
  197. +++ b/hello
  198. @@ -1 +1,2 @@
  199. Hello World
  200. +It's a new day for git
  201. ------------
  202. i.e. the diff of the change we caused by adding another line to `hello`.
  203. In other words, 'git diff-files' always shows us the difference between
  204. what is recorded in the index, and what is currently in the working
  205. tree. That's very useful.
  206. A common shorthand for `git diff-files -p` is to just write `git
  207. diff`, which will do the same thing.
  208. ------------
  209. $ git diff
  210. diff --git a/hello b/hello
  211. index 557db03..263414f 100644
  212. --- a/hello
  213. +++ b/hello
  214. @@ -1 +1,2 @@
  215. Hello World
  216. +It's a new day for git
  217. ------------
  218. Committing Git state
  219. --------------------
  220. Now, we want to go to the next stage in Git, which is to take the files
  221. that Git knows about in the index, and commit them as a real tree. We do
  222. that in two phases: creating a 'tree' object, and committing that 'tree'
  223. object as a 'commit' object together with an explanation of what the
  224. tree was all about, along with information of how we came to that state.
  225. Creating a tree object is trivial, and is done with 'git write-tree'.
  226. There are no options or other input: `git write-tree` will take the
  227. current index state, and write an object that describes that whole
  228. index. In other words, we're now tying together all the different
  229. filenames with their contents (and their permissions), and we're
  230. creating the equivalent of a Git "directory" object:
  231. ------------------------------------------------
  232. $ git write-tree
  233. ------------------------------------------------
  234. and this will just output the name of the resulting tree, in this case
  235. (if you have done exactly as I've described) it should be
  236. ----------------
  237. 8988da15d077d4829fc51d8544c097def6644dbb
  238. ----------------
  239. which is another incomprehensible object name. Again, if you want to,
  240. you can use `git cat-file -t 8988d...` to see that this time the object
  241. is not a "blob" object, but a "tree" object (you can also use
  242. `git cat-file` to actually output the raw object contents, but you'll see
  243. mainly a binary mess, so that's less interesting).
  244. However -- normally you'd never use 'git write-tree' on its own, because
  245. normally you always commit a tree into a commit object using the
  246. 'git commit-tree' command. In fact, it's easier to not actually use
  247. 'git write-tree' on its own at all, but to just pass its result in as an
  248. argument to 'git commit-tree'.
  249. 'git commit-tree' normally takes several arguments -- it wants to know
  250. what the 'parent' of a commit was, but since this is the first commit
  251. ever in this new repository, and it has no parents, we only need to pass in
  252. the object name of the tree. However, 'git commit-tree' also wants to get a
  253. commit message on its standard input, and it will write out the resulting
  254. object name for the commit to its standard output.
  255. And this is where we create the `.git/refs/heads/master` file
  256. which is pointed at by `HEAD`. This file is supposed to contain
  257. the reference to the top-of-tree of the master branch, and since
  258. that's exactly what 'git commit-tree' spits out, we can do this
  259. all with a sequence of simple shell commands:
  260. ------------------------------------------------
  261. $ tree=$(git write-tree)
  262. $ commit=$(echo 'Initial commit' | git commit-tree $tree)
  263. $ git update-ref HEAD $commit
  264. ------------------------------------------------
  265. In this case this creates a totally new commit that is not related to
  266. anything else. Normally you do this only *once* for a project ever, and
  267. all later commits will be parented on top of an earlier commit.
  268. Again, normally you'd never actually do this by hand. There is a
  269. helpful script called `git commit` that will do all of this for you. So
  270. you could have just written `git commit`
  271. instead, and it would have done the above magic scripting for you.
  272. Making a change
  273. ---------------
  274. Remember how we did the 'git update-index' on file `hello` and then we
  275. changed `hello` afterward, and could compare the new state of `hello` with the
  276. state we saved in the index file?
  277. Further, remember how I said that 'git write-tree' writes the contents
  278. of the *index* file to the tree, and thus what we just committed was in
  279. fact the *original* contents of the file `hello`, not the new ones. We did
  280. that on purpose, to show the difference between the index state, and the
  281. state in the working tree, and how they don't have to match, even
  282. when we commit things.
  283. As before, if we do `git diff-files -p` in our git-tutorial project,
  284. we'll still see the same difference we saw last time: the index file
  285. hasn't changed by the act of committing anything. However, now that we
  286. have committed something, we can also learn to use a new command:
  287. 'git diff-index'.
  288. Unlike 'git diff-files', which showed the difference between the index
  289. file and the working tree, 'git diff-index' shows the differences
  290. between a committed *tree* and either the index file or the working
  291. tree. In other words, 'git diff-index' wants a tree to be diffed
  292. against, and before we did the commit, we couldn't do that, because we
  293. didn't have anything to diff against.
  294. But now we can do
  295. ----------------
  296. $ git diff-index -p HEAD
  297. ----------------
  298. (where `-p` has the same meaning as it did in 'git diff-files'), and it
  299. will show us the same difference, but for a totally different reason.
  300. Now we're comparing the working tree not against the index file,
  301. but against the tree we just wrote. It just so happens that those two
  302. are obviously the same, so we get the same result.
  303. Again, because this is a common operation, you can also just shorthand
  304. it with
  305. ----------------
  306. $ git diff HEAD
  307. ----------------
  308. which ends up doing the above for you.
  309. In other words, 'git diff-index' normally compares a tree against the
  310. working tree, but when given the `--cached` flag, it is told to
  311. instead compare against just the index cache contents, and ignore the
  312. current working tree state entirely. Since we just wrote the index
  313. file to HEAD, doing `git diff-index --cached -p HEAD` should thus return
  314. an empty set of differences, and that's exactly what it does.
  315. [NOTE]
  316. ================
  317. 'git diff-index' really always uses the index for its
  318. comparisons, and saying that it compares a tree against the working
  319. tree is thus not strictly accurate. In particular, the list of
  320. files to compare (the "meta-data") *always* comes from the index file,
  321. regardless of whether the `--cached` flag is used or not. The `--cached`
  322. flag really only determines whether the file *contents* to be compared
  323. come from the working tree or not.
  324. This is not hard to understand, as soon as you realize that Git simply
  325. never knows (or cares) about files that it is not told about
  326. explicitly. Git will never go *looking* for files to compare, it
  327. expects you to tell it what the files are, and that's what the index
  328. is there for.
  329. ================
  330. However, our next step is to commit the *change* we did, and again, to
  331. understand what's going on, keep in mind the difference between "working
  332. tree contents", "index file" and "committed tree". We have changes
  333. in the working tree that we want to commit, and we always have to
  334. work through the index file, so the first thing we need to do is to
  335. update the index cache:
  336. ------------------------------------------------
  337. $ git update-index hello
  338. ------------------------------------------------
  339. (note how we didn't need the `--add` flag this time, since Git knew
  340. about the file already).
  341. Note what happens to the different 'git diff-{asterisk}' versions here.
  342. After we've updated `hello` in the index, `git diff-files -p` now shows no
  343. differences, but `git diff-index -p HEAD` still *does* show that the
  344. current state is different from the state we committed. In fact, now
  345. 'git diff-index' shows the same difference whether we use the `--cached`
  346. flag or not, since now the index is coherent with the working tree.
  347. Now, since we've updated `hello` in the index, we can commit the new
  348. version. We could do it by writing the tree by hand again, and
  349. committing the tree (this time we'd have to use the `-p HEAD` flag to
  350. tell commit that the HEAD was the *parent* of the new commit, and that
  351. this wasn't an initial commit any more), but you've done that once
  352. already, so let's just use the helpful script this time:
  353. ------------------------------------------------
  354. $ git commit
  355. ------------------------------------------------
  356. which starts an editor for you to write the commit message and tells you
  357. a bit about what you have done.
  358. Write whatever message you want, and all the lines that start with '#'
  359. will be pruned out, and the rest will be used as the commit message for
  360. the change. If you decide you don't want to commit anything after all at
  361. this point (you can continue to edit things and update the index), you
  362. can just leave an empty message. Otherwise `git commit` will commit
  363. the change for you.
  364. You've now made your first real Git commit. And if you're interested in
  365. looking at what `git commit` really does, feel free to investigate:
  366. it's a few very simple shell scripts to generate the helpful (?) commit
  367. message headers, and a few one-liners that actually do the
  368. commit itself ('git commit').
  369. Inspecting Changes
  370. ------------------
  371. While creating changes is useful, it's even more useful if you can tell
  372. later what changed. The most useful command for this is another of the
  373. 'diff' family, namely 'git diff-tree'.
  374. 'git diff-tree' can be given two arbitrary trees, and it will tell you the
  375. differences between them. Perhaps even more commonly, though, you can
  376. give it just a single commit object, and it will figure out the parent
  377. of that commit itself, and show the difference directly. Thus, to get
  378. the same diff that we've already seen several times, we can now do
  379. ----------------
  380. $ git diff-tree -p HEAD
  381. ----------------
  382. (again, `-p` means to show the difference as a human-readable patch),
  383. and it will show what the last commit (in `HEAD`) actually changed.
  384. [NOTE]
  385. ============
  386. Here is an ASCII art by Jon Loeliger that illustrates how
  387. various 'diff-{asterisk}' commands compare things.
  388. diff-tree
  389. +----+
  390. | |
  391. | |
  392. V V
  393. +-----------+
  394. | Object DB |
  395. | Backing |
  396. | Store |
  397. +-----------+
  398. ^ ^
  399. | |
  400. | | diff-index --cached
  401. | |
  402. diff-index | V
  403. | +-----------+
  404. | | Index |
  405. | | "cache" |
  406. | +-----------+
  407. | ^
  408. | |
  409. | | diff-files
  410. | |
  411. V V
  412. +-----------+
  413. | Working |
  414. | Directory |
  415. +-----------+
  416. ============
  417. More interestingly, you can also give 'git diff-tree' the `--pretty` flag,
  418. which tells it to also show the commit message and author and date of the
  419. commit, and you can tell it to show a whole series of diffs.
  420. Alternatively, you can tell it to be "silent", and not show the diffs at
  421. all, but just show the actual commit message.
  422. In fact, together with the 'git rev-list' program (which generates a
  423. list of revisions), 'git diff-tree' ends up being a veritable fount of
  424. changes. You can emulate `git log`, `git log -p`, etc. with a trivial
  425. script that pipes the output of `git rev-list` to `git diff-tree --stdin`,
  426. which was exactly how early versions of `git log` were implemented.
  427. Tagging a version
  428. -----------------
  429. In Git, there are two kinds of tags, a "light" one, and an "annotated tag".
  430. A "light" tag is technically nothing more than a branch, except we put
  431. it in the `.git/refs/tags/` subdirectory instead of calling it a `head`.
  432. So the simplest form of tag involves nothing more than
  433. ------------------------------------------------
  434. $ git tag my-first-tag
  435. ------------------------------------------------
  436. which just writes the current `HEAD` into the `.git/refs/tags/my-first-tag`
  437. file, after which point you can then use this symbolic name for that
  438. particular state. You can, for example, do
  439. ----------------
  440. $ git diff my-first-tag
  441. ----------------
  442. to diff your current state against that tag which at this point will
  443. obviously be an empty diff, but if you continue to develop and commit
  444. stuff, you can use your tag as an "anchor-point" to see what has changed
  445. since you tagged it.
  446. An "annotated tag" is actually a real Git object, and contains not only a
  447. pointer to the state you want to tag, but also a small tag name and
  448. message, along with optionally a PGP signature that says that yes,
  449. you really did
  450. that tag. You create these annotated tags with either the `-a` or
  451. `-s` flag to 'git tag':
  452. ----------------
  453. $ git tag -s <tagname>
  454. ----------------
  455. which will sign the current `HEAD` (but you can also give it another
  456. argument that specifies the thing to tag, e.g., you could have tagged the
  457. current `mybranch` point by using `git tag <tagname> mybranch`).
  458. You normally only do signed tags for major releases or things
  459. like that, while the light-weight tags are useful for any marking you
  460. want to do -- any time you decide that you want to remember a certain
  461. point, just create a private tag for it, and you have a nice symbolic
  462. name for the state at that point.
  463. Copying repositories
  464. --------------------
  465. Git repositories are normally totally self-sufficient and relocatable.
  466. Unlike CVS, for example, there is no separate notion of
  467. "repository" and "working tree". A Git repository normally *is* the
  468. working tree, with the local Git information hidden in the `.git`
  469. subdirectory. There is nothing else. What you see is what you got.
  470. [NOTE]
  471. You can tell Git to split the Git internal information from
  472. the directory that it tracks, but we'll ignore that for now: it's not
  473. how normal projects work, and it's really only meant for special uses.
  474. So the mental model of "the Git information is always tied directly to
  475. the working tree that it describes" may not be technically 100%
  476. accurate, but it's a good model for all normal use.
  477. This has two implications:
  478. - if you grow bored with the tutorial repository you created (or you've
  479. made a mistake and want to start all over), you can just do simple
  480. +
  481. ----------------
  482. $ rm -rf git-tutorial
  483. ----------------
  484. +
  485. and it will be gone. There's no external repository, and there's no
  486. history outside the project you created.
  487. - if you want to move or duplicate a Git repository, you can do so. There
  488. is 'git clone' command, but if all you want to do is just to
  489. create a copy of your repository (with all the full history that
  490. went along with it), you can do so with a regular
  491. `cp -a git-tutorial new-git-tutorial`.
  492. +
  493. Note that when you've moved or copied a Git repository, your Git index
  494. file (which caches various information, notably some of the "stat"
  495. information for the files involved) will likely need to be refreshed.
  496. So after you do a `cp -a` to create a new copy, you'll want to do
  497. +
  498. ----------------
  499. $ git update-index --refresh
  500. ----------------
  501. +
  502. in the new repository to make sure that the index file is up to date.
  503. Note that the second point is true even across machines. You can
  504. duplicate a remote Git repository with *any* regular copy mechanism, be it
  505. 'scp', 'rsync' or 'wget'.
  506. When copying a remote repository, you'll want to at a minimum update the
  507. index cache when you do this, and especially with other peoples'
  508. repositories you often want to make sure that the index cache is in some
  509. known state (you don't know *what* they've done and not yet checked in),
  510. so usually you'll precede the 'git update-index' with a
  511. ----------------
  512. $ git read-tree --reset HEAD
  513. $ git update-index --refresh
  514. ----------------
  515. which will force a total index re-build from the tree pointed to by `HEAD`.
  516. It resets the index contents to `HEAD`, and then the 'git update-index'
  517. makes sure to match up all index entries with the checked-out files.
  518. If the original repository had uncommitted changes in its
  519. working tree, `git update-index --refresh` notices them and
  520. tells you they need to be updated.
  521. The above can also be written as simply
  522. ----------------
  523. $ git reset
  524. ----------------
  525. and in fact a lot of the common Git command combinations can be scripted
  526. with the `git xyz` interfaces. You can learn things by just looking
  527. at what the various git scripts do. For example, `git reset` used to be
  528. the above two lines implemented in 'git reset', but some things like
  529. 'git status' and 'git commit' are slightly more complex scripts around
  530. the basic Git commands.
  531. Many (most?) public remote repositories will not contain any of
  532. the checked out files or even an index file, and will *only* contain the
  533. actual core Git files. Such a repository usually doesn't even have the
  534. `.git` subdirectory, but has all the Git files directly in the
  535. repository.
  536. To create your own local live copy of such a "raw" Git repository, you'd
  537. first create your own subdirectory for the project, and then copy the
  538. raw repository contents into the `.git` directory. For example, to
  539. create your own copy of the Git repository, you'd do the following
  540. ----------------
  541. $ mkdir my-git
  542. $ cd my-git
  543. $ rsync -rL rsync://rsync.kernel.org/pub/scm/git/git.git/ .git
  544. ----------------
  545. followed by
  546. ----------------
  547. $ git read-tree HEAD
  548. ----------------
  549. to populate the index. However, now you have populated the index, and
  550. you have all the Git internal files, but you will notice that you don't
  551. actually have any of the working tree files to work on. To get
  552. those, you'd check them out with
  553. ----------------
  554. $ git checkout-index -u -a
  555. ----------------
  556. where the `-u` flag means that you want the checkout to keep the index
  557. up to date (so that you don't have to refresh it afterward), and the
  558. `-a` flag means "check out all files" (if you have a stale copy or an
  559. older version of a checked out tree you may also need to add the `-f`
  560. flag first, to tell 'git checkout-index' to *force* overwriting of any old
  561. files).
  562. Again, this can all be simplified with
  563. ----------------
  564. $ git clone git://git.kernel.org/pub/scm/git/git.git/ my-git
  565. $ cd my-git
  566. $ git checkout
  567. ----------------
  568. which will end up doing all of the above for you.
  569. You have now successfully copied somebody else's (mine) remote
  570. repository, and checked it out.
  571. Creating a new branch
  572. ---------------------
  573. Branches in Git are really nothing more than pointers into the Git
  574. object database from within the `.git/refs/` subdirectory, and as we
  575. already discussed, the `HEAD` branch is nothing but a symlink to one of
  576. these object pointers.
  577. You can at any time create a new branch by just picking an arbitrary
  578. point in the project history, and just writing the SHA-1 name of that
  579. object into a file under `.git/refs/heads/`. You can use any filename you
  580. want (and indeed, subdirectories), but the convention is that the
  581. "normal" branch is called `master`. That's just a convention, though,
  582. and nothing enforces it.
  583. To show that as an example, let's go back to the git-tutorial repository we
  584. used earlier, and create a branch in it. You do that by simply just
  585. saying that you want to check out a new branch:
  586. ------------
  587. $ git switch -c mybranch
  588. ------------
  589. will create a new branch based at the current `HEAD` position, and switch
  590. to it.
  591. [NOTE]
  592. ================================================
  593. If you make the decision to start your new branch at some
  594. other point in the history than the current `HEAD`, you can do so by
  595. just telling 'git switch' what the base of the checkout would be.
  596. In other words, if you have an earlier tag or branch, you'd just do
  597. ------------
  598. $ git switch -c mybranch earlier-commit
  599. ------------
  600. and it would create the new branch `mybranch` at the earlier commit,
  601. and check out the state at that time.
  602. ================================================
  603. You can always just jump back to your original `master` branch by doing
  604. ------------
  605. $ git switch master
  606. ------------
  607. (or any other branch-name, for that matter) and if you forget which
  608. branch you happen to be on, a simple
  609. ------------
  610. $ cat .git/HEAD
  611. ------------
  612. will tell you where it's pointing. To get the list of branches
  613. you have, you can say
  614. ------------
  615. $ git branch
  616. ------------
  617. which used to be nothing more than a simple script around `ls .git/refs/heads`.
  618. There will be an asterisk in front of the branch you are currently on.
  619. Sometimes you may wish to create a new branch _without_ actually
  620. checking it out and switching to it. If so, just use the command
  621. ------------
  622. $ git branch <branchname> [startingpoint]
  623. ------------
  624. which will simply _create_ the branch, but will not do anything further.
  625. You can then later -- once you decide that you want to actually develop
  626. on that branch -- switch to that branch with a regular 'git switch'
  627. with the branchname as the argument.
  628. Merging two branches
  629. --------------------
  630. One of the ideas of having a branch is that you do some (possibly
  631. experimental) work in it, and eventually merge it back to the main
  632. branch. So assuming you created the above `mybranch` that started out
  633. being the same as the original `master` branch, let's make sure we're in
  634. that branch, and do some work there.
  635. ------------------------------------------------
  636. $ git switch mybranch
  637. $ echo "Work, work, work" >>hello
  638. $ git commit -m "Some work." -i hello
  639. ------------------------------------------------
  640. Here, we just added another line to `hello`, and we used a shorthand for
  641. doing both `git update-index hello` and `git commit` by just giving the
  642. filename directly to `git commit`, with an `-i` flag (it tells
  643. Git to 'include' that file in addition to what you have done to
  644. the index file so far when making the commit). The `-m` flag is to give the
  645. commit log message from the command line.
  646. Now, to make it a bit more interesting, let's assume that somebody else
  647. does some work in the original branch, and simulate that by going back
  648. to the master branch, and editing the same file differently there:
  649. ------------
  650. $ git switch master
  651. ------------
  652. Here, take a moment to look at the contents of `hello`, and notice how they
  653. don't contain the work we just did in `mybranch` -- because that work
  654. hasn't happened in the `master` branch at all. Then do
  655. ------------
  656. $ echo "Play, play, play" >>hello
  657. $ echo "Lots of fun" >>example
  658. $ git commit -m "Some fun." -i hello example
  659. ------------
  660. since the master branch is obviously in a much better mood.
  661. Now, you've got two branches, and you decide that you want to merge the
  662. work done. Before we do that, let's introduce a cool graphical tool that
  663. helps you view what's going on:
  664. ----------------
  665. $ gitk --all
  666. ----------------
  667. will show you graphically both of your branches (that's what the `--all`
  668. means: normally it will just show you your current `HEAD`) and their
  669. histories. You can also see exactly how they came to be from a common
  670. source.
  671. Anyway, let's exit 'gitk' (`^Q` or the File menu), and decide that we want
  672. to merge the work we did on the `mybranch` branch into the `master`
  673. branch (which is currently our `HEAD` too). To do that, there's a nice
  674. script called 'git merge', which wants to know which branches you want
  675. to resolve and what the merge is all about:
  676. ------------
  677. $ git merge -m "Merge work in mybranch" mybranch
  678. ------------
  679. where the first argument is going to be used as the commit message if
  680. the merge can be resolved automatically.
  681. Now, in this case we've intentionally created a situation where the
  682. merge will need to be fixed up by hand, though, so Git will do as much
  683. of it as it can automatically (which in this case is just merge the `example`
  684. file, which had no differences in the `mybranch` branch), and say:
  685. ----------------
  686. Auto-merging hello
  687. CONFLICT (content): Merge conflict in hello
  688. Automatic merge failed; fix conflicts and then commit the result.
  689. ----------------
  690. It tells you that it did an "Automatic merge", which
  691. failed due to conflicts in `hello`.
  692. Not to worry. It left the (trivial) conflict in `hello` in the same form you
  693. should already be well used to if you've ever used CVS, so let's just
  694. open `hello` in our editor (whatever that may be), and fix it up somehow.
  695. I'd suggest just making it so that `hello` contains all four lines:
  696. ------------
  697. Hello World
  698. It's a new day for git
  699. Play, play, play
  700. Work, work, work
  701. ------------
  702. and once you're happy with your manual merge, just do a
  703. ------------
  704. $ git commit -i hello
  705. ------------
  706. which will very loudly warn you that you're now committing a merge
  707. (which is correct, so never mind), and you can write a small merge
  708. message about your adventures in 'git merge'-land.
  709. After you're done, start up `gitk --all` to see graphically what the
  710. history looks like. Notice that `mybranch` still exists, and you can
  711. switch to it, and continue to work with it if you want to. The
  712. `mybranch` branch will not contain the merge, but next time you merge it
  713. from the `master` branch, Git will know how you merged it, so you'll not
  714. have to do _that_ merge again.
  715. Another useful tool, especially if you do not always work in X-Window
  716. environment, is `git show-branch`.
  717. ------------------------------------------------
  718. $ git show-branch --topo-order --more=1 master mybranch
  719. * [master] Merge work in mybranch
  720. ! [mybranch] Some work.
  721. --
  722. - [master] Merge work in mybranch
  723. *+ [mybranch] Some work.
  724. * [master^] Some fun.
  725. ------------------------------------------------
  726. The first two lines indicate that it is showing the two branches
  727. with the titles of their top-of-the-tree commits, you are currently on
  728. `master` branch (notice the asterisk `*` character), and the first
  729. column for the later output lines is used to show commits contained in the
  730. `master` branch, and the second column for the `mybranch`
  731. branch. Three commits are shown along with their titles.
  732. All of them have non blank characters in the first column (`*`
  733. shows an ordinary commit on the current branch, `-` is a merge commit), which
  734. means they are now part of the `master` branch. Only the "Some
  735. work" commit has the plus `+` character in the second column,
  736. because `mybranch` has not been merged to incorporate these
  737. commits from the master branch. The string inside brackets
  738. before the commit log message is a short name you can use to
  739. name the commit. In the above example, 'master' and 'mybranch'
  740. are branch heads. 'master^' is the first parent of 'master'
  741. branch head. Please see linkgit:gitrevisions[7] if you want to
  742. see more complex cases.
  743. [NOTE]
  744. Without the '--more=1' option, 'git show-branch' would not output the
  745. '[master^]' commit, as '[mybranch]' commit is a common ancestor of
  746. both 'master' and 'mybranch' tips. Please see linkgit:git-show-branch[1]
  747. for details.
  748. [NOTE]
  749. If there were more commits on the 'master' branch after the merge, the
  750. merge commit itself would not be shown by 'git show-branch' by
  751. default. You would need to provide `--sparse` option to make the
  752. merge commit visible in this case.
  753. Now, let's pretend you are the one who did all the work in
  754. `mybranch`, and the fruit of your hard work has finally been merged
  755. to the `master` branch. Let's go back to `mybranch`, and run
  756. 'git merge' to get the "upstream changes" back to your branch.
  757. ------------
  758. $ git switch mybranch
  759. $ git merge -m "Merge upstream changes." master
  760. ------------
  761. This outputs something like this (the actual commit object names
  762. would be different)
  763. ----------------
  764. Updating from ae3a2da... to a80b4aa....
  765. Fast-forward (no commit created; -m option ignored)
  766. example | 1 +
  767. hello | 1 +
  768. 2 files changed, 2 insertions(+)
  769. ----------------
  770. Because your branch did not contain anything more than what had
  771. already been merged into the `master` branch, the merge operation did
  772. not actually do a merge. Instead, it just updated the top of
  773. the tree of your branch to that of the `master` branch. This is
  774. often called 'fast-forward' merge.
  775. You can run `gitk --all` again to see how the commit ancestry
  776. looks like, or run 'show-branch', which tells you this.
  777. ------------------------------------------------
  778. $ git show-branch master mybranch
  779. ! [master] Merge work in mybranch
  780. * [mybranch] Merge work in mybranch
  781. --
  782. -- [master] Merge work in mybranch
  783. ------------------------------------------------
  784. Merging external work
  785. ---------------------
  786. It's usually much more common that you merge with somebody else than
  787. merging with your own branches, so it's worth pointing out that Git
  788. makes that very easy too, and in fact, it's not that different from
  789. doing a 'git merge'. In fact, a remote merge ends up being nothing
  790. more than "fetch the work from a remote repository into a temporary tag"
  791. followed by a 'git merge'.
  792. Fetching from a remote repository is done by, unsurprisingly,
  793. 'git fetch':
  794. ----------------
  795. $ git fetch <remote-repository>
  796. ----------------
  797. One of the following transports can be used to name the
  798. repository to download from:
  799. SSH::
  800. `remote.machine:/path/to/repo.git/` or
  801. +
  802. `ssh://remote.machine/path/to/repo.git/`
  803. +
  804. This transport can be used for both uploading and downloading,
  805. and requires you to have a log-in privilege over `ssh` to the
  806. remote machine. It finds out the set of objects the other side
  807. lacks by exchanging the head commits both ends have and
  808. transfers (close to) minimum set of objects. It is by far the
  809. most efficient way to exchange Git objects between repositories.
  810. Local directory::
  811. `/path/to/repo.git/`
  812. +
  813. This transport is the same as SSH transport but uses 'sh' to run
  814. both ends on the local machine instead of running other end on
  815. the remote machine via 'ssh'.
  816. Git Native::
  817. `git://remote.machine/path/to/repo.git/`
  818. +
  819. This transport was designed for anonymous downloading. Like SSH
  820. transport, it finds out the set of objects the downstream side
  821. lacks and transfers (close to) minimum set of objects.
  822. HTTP(S)::
  823. `http://remote.machine/path/to/repo.git/`
  824. +
  825. Downloader from http and https URL
  826. first obtains the topmost commit object name from the remote site
  827. by looking at the specified refname under `repo.git/refs/` directory,
  828. and then tries to obtain the
  829. commit object by downloading from `repo.git/objects/xx/xxx...`
  830. using the object name of that commit object. Then it reads the
  831. commit object to find out its parent commits and the associate
  832. tree object; it repeats this process until it gets all the
  833. necessary objects. Because of this behavior, they are
  834. sometimes also called 'commit walkers'.
  835. +
  836. The 'commit walkers' are sometimes also called 'dumb
  837. transports', because they do not require any Git aware smart
  838. server like Git Native transport does. Any stock HTTP server
  839. that does not even support directory index would suffice. But
  840. you must prepare your repository with 'git update-server-info'
  841. to help dumb transport downloaders.
  842. Once you fetch from the remote repository, you `merge` that
  843. with your current branch.
  844. However -- it's such a common thing to `fetch` and then
  845. immediately `merge`, that it's called `git pull`, and you can
  846. simply do
  847. ----------------
  848. $ git pull <remote-repository>
  849. ----------------
  850. and optionally give a branch-name for the remote end as a second
  851. argument.
  852. [NOTE]
  853. You could do without using any branches at all, by
  854. keeping as many local repositories as you would like to have
  855. branches, and merging between them with 'git pull', just like
  856. you merge between branches. The advantage of this approach is
  857. that it lets you keep a set of files for each `branch` checked
  858. out and you may find it easier to switch back and forth if you
  859. juggle multiple lines of development simultaneously. Of
  860. course, you will pay the price of more disk usage to hold
  861. multiple working trees, but disk space is cheap these days.
  862. It is likely that you will be pulling from the same remote
  863. repository from time to time. As a short hand, you can store
  864. the remote repository URL in the local repository's config file
  865. like this:
  866. ------------------------------------------------
  867. $ git config remote.linus.url http://www.kernel.org/pub/scm/git/git.git/
  868. ------------------------------------------------
  869. and use the "linus" keyword with 'git pull' instead of the full URL.
  870. Examples.
  871. . `git pull linus`
  872. . `git pull linus tag v0.99.1`
  873. the above are equivalent to:
  874. . `git pull http://www.kernel.org/pub/scm/git/git.git/ HEAD`
  875. . `git pull http://www.kernel.org/pub/scm/git/git.git/ tag v0.99.1`
  876. How does the merge work?
  877. ------------------------
  878. We said this tutorial shows what plumbing does to help you cope
  879. with the porcelain that isn't flushing, but we so far did not
  880. talk about how the merge really works. If you are following
  881. this tutorial the first time, I'd suggest to skip to "Publishing
  882. your work" section and come back here later.
  883. OK, still with me? To give us an example to look at, let's go
  884. back to the earlier repository with "hello" and "example" file,
  885. and bring ourselves back to the pre-merge state:
  886. ------------
  887. $ git show-branch --more=2 master mybranch
  888. ! [master] Merge work in mybranch
  889. * [mybranch] Merge work in mybranch
  890. --
  891. -- [master] Merge work in mybranch
  892. +* [master^2] Some work.
  893. +* [master^] Some fun.
  894. ------------
  895. Remember, before running 'git merge', our `master` head was at
  896. "Some fun." commit, while our `mybranch` head was at "Some
  897. work." commit.
  898. ------------
  899. $ git switch -C mybranch master^2
  900. $ git switch master
  901. $ git reset --hard master^
  902. ------------
  903. After rewinding, the commit structure should look like this:
  904. ------------
  905. $ git show-branch
  906. * [master] Some fun.
  907. ! [mybranch] Some work.
  908. --
  909. * [master] Some fun.
  910. + [mybranch] Some work.
  911. *+ [master^] Initial commit
  912. ------------
  913. Now we are ready to experiment with the merge by hand.
  914. `git merge` command, when merging two branches, uses 3-way merge
  915. algorithm. First, it finds the common ancestor between them.
  916. The command it uses is 'git merge-base':
  917. ------------
  918. $ mb=$(git merge-base HEAD mybranch)
  919. ------------
  920. The command writes the commit object name of the common ancestor
  921. to the standard output, so we captured its output to a variable,
  922. because we will be using it in the next step. By the way, the common
  923. ancestor commit is the "Initial commit" commit in this case. You can
  924. tell it by:
  925. ------------
  926. $ git name-rev --name-only --tags $mb
  927. my-first-tag
  928. ------------
  929. After finding out a common ancestor commit, the second step is
  930. this:
  931. ------------
  932. $ git read-tree -m -u $mb HEAD mybranch
  933. ------------
  934. This is the same 'git read-tree' command we have already seen,
  935. but it takes three trees, unlike previous examples. This reads
  936. the contents of each tree into different 'stage' in the index
  937. file (the first tree goes to stage 1, the second to stage 2,
  938. etc.). After reading three trees into three stages, the paths
  939. that are the same in all three stages are 'collapsed' into stage
  940. 0. Also paths that are the same in two of three stages are
  941. collapsed into stage 0, taking the SHA-1 from either stage 2 or
  942. stage 3, whichever is different from stage 1 (i.e. only one side
  943. changed from the common ancestor).
  944. After 'collapsing' operation, paths that are different in three
  945. trees are left in non-zero stages. At this point, you can
  946. inspect the index file with this command:
  947. ------------
  948. $ git ls-files --stage
  949. 100644 7f8b141b65fdcee47321e399a2598a235a032422 0 example
  950. 100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
  951. 100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
  952. 100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello
  953. ------------
  954. In our example of only two files, we did not have unchanged
  955. files so only 'example' resulted in collapsing. But in real-life
  956. large projects, when only a small number of files change in one commit,
  957. this 'collapsing' tends to trivially merge most of the paths
  958. fairly quickly, leaving only a handful of real changes in non-zero
  959. stages.
  960. To look at only non-zero stages, use `--unmerged` flag:
  961. ------------
  962. $ git ls-files --unmerged
  963. 100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
  964. 100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
  965. 100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello
  966. ------------
  967. The next step of merging is to merge these three versions of the
  968. file, using 3-way merge. This is done by giving
  969. 'git merge-one-file' command as one of the arguments to
  970. 'git merge-index' command:
  971. ------------
  972. $ git merge-index git-merge-one-file hello
  973. Auto-merging hello
  974. ERROR: Merge conflict in hello
  975. fatal: merge program failed
  976. ------------
  977. 'git merge-one-file' script is called with parameters to
  978. describe those three versions, and is responsible to leave the
  979. merge results in the working tree.
  980. It is a fairly straightforward shell script, and
  981. eventually calls 'merge' program from RCS suite to perform a
  982. file-level 3-way merge. In this case, 'merge' detects
  983. conflicts, and the merge result with conflict marks is left in
  984. the working tree.. This can be seen if you run `ls-files
  985. --stage` again at this point:
  986. ------------
  987. $ git ls-files --stage
  988. 100644 7f8b141b65fdcee47321e399a2598a235a032422 0 example
  989. 100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
  990. 100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
  991. 100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello
  992. ------------
  993. This is the state of the index file and the working file after
  994. 'git merge' returns control back to you, leaving the conflicting
  995. merge for you to resolve. Notice that the path `hello` is still
  996. unmerged, and what you see with 'git diff' at this point is
  997. differences since stage 2 (i.e. your version).
  998. Publishing your work
  999. --------------------
  1000. So, we can use somebody else's work from a remote repository, but
  1001. how can *you* prepare a repository to let other people pull from
  1002. it?
  1003. You do your real work in your working tree that has your
  1004. primary repository hanging under it as its `.git` subdirectory.
  1005. You *could* make that repository accessible remotely and ask
  1006. people to pull from it, but in practice that is not the way
  1007. things are usually done. A recommended way is to have a public
  1008. repository, make it reachable by other people, and when the
  1009. changes you made in your primary working tree are in good shape,
  1010. update the public repository from it. This is often called
  1011. 'pushing'.
  1012. [NOTE]
  1013. This public repository could further be mirrored, and that is
  1014. how Git repositories at `kernel.org` are managed.
  1015. Publishing the changes from your local (private) repository to
  1016. your remote (public) repository requires a write privilege on
  1017. the remote machine. You need to have an SSH account there to
  1018. run a single command, 'git-receive-pack'.
  1019. First, you need to create an empty repository on the remote
  1020. machine that will house your public repository. This empty
  1021. repository will be populated and be kept up to date by pushing
  1022. into it later. Obviously, this repository creation needs to be
  1023. done only once.
  1024. [NOTE]
  1025. 'git push' uses a pair of commands,
  1026. 'git send-pack' on your local machine, and 'git-receive-pack'
  1027. on the remote machine. The communication between the two over
  1028. the network internally uses an SSH connection.
  1029. Your private repository's Git directory is usually `.git`, but
  1030. your public repository is often named after the project name,
  1031. i.e. `<project>.git`. Let's create such a public repository for
  1032. project `my-git`. After logging into the remote machine, create
  1033. an empty directory:
  1034. ------------
  1035. $ mkdir my-git.git
  1036. ------------
  1037. Then, make that directory into a Git repository by running
  1038. 'git init', but this time, since its name is not the usual
  1039. `.git`, we do things slightly differently:
  1040. ------------
  1041. $ GIT_DIR=my-git.git git init
  1042. ------------
  1043. Make sure this directory is available for others you want your
  1044. changes to be pulled via the transport of your choice. Also
  1045. you need to make sure that you have the 'git-receive-pack'
  1046. program on the `$PATH`.
  1047. [NOTE]
  1048. Many installations of sshd do not invoke your shell as the login
  1049. shell when you directly run programs; what this means is that if
  1050. your login shell is 'bash', only `.bashrc` is read and not
  1051. `.bash_profile`. As a workaround, make sure `.bashrc` sets up
  1052. `$PATH` so that you can run 'git-receive-pack' program.
  1053. [NOTE]
  1054. If you plan to publish this repository to be accessed over http,
  1055. you should do `mv my-git.git/hooks/post-update.sample
  1056. my-git.git/hooks/post-update` at this point.
  1057. This makes sure that every time you push into this
  1058. repository, `git update-server-info` is run.
  1059. Your "public repository" is now ready to accept your changes.
  1060. Come back to the machine you have your private repository. From
  1061. there, run this command:
  1062. ------------
  1063. $ git push <public-host>:/path/to/my-git.git master
  1064. ------------
  1065. This synchronizes your public repository to match the named
  1066. branch head (i.e. `master` in this case) and objects reachable
  1067. from them in your current repository.
  1068. As a real example, this is how I update my public Git
  1069. repository. Kernel.org mirror network takes care of the
  1070. propagation to other publicly visible machines:
  1071. ------------
  1072. $ git push master.kernel.org:/pub/scm/git/git.git/
  1073. ------------
  1074. Packing your repository
  1075. -----------------------
  1076. Earlier, we saw that one file under `.git/objects/??/` directory
  1077. is stored for each Git object you create. This representation
  1078. is efficient to create atomically and safely, but
  1079. not so convenient to transport over the network. Since Git objects are
  1080. immutable once they are created, there is a way to optimize the
  1081. storage by "packing them together". The command
  1082. ------------
  1083. $ git repack
  1084. ------------
  1085. will do it for you. If you followed the tutorial examples, you
  1086. would have accumulated about 17 objects in `.git/objects/??/`
  1087. directories by now. 'git repack' tells you how many objects it
  1088. packed, and stores the packed file in the `.git/objects/pack`
  1089. directory.
  1090. [NOTE]
  1091. You will see two files, `pack-*.pack` and `pack-*.idx`,
  1092. in `.git/objects/pack` directory. They are closely related to
  1093. each other, and if you ever copy them by hand to a different
  1094. repository for whatever reason, you should make sure you copy
  1095. them together. The former holds all the data from the objects
  1096. in the pack, and the latter holds the index for random
  1097. access.
  1098. If you are paranoid, running 'git verify-pack' command would
  1099. detect if you have a corrupt pack, but do not worry too much.
  1100. Our programs are always perfect ;-).
  1101. Once you have packed objects, you do not need to leave the
  1102. unpacked objects that are contained in the pack file anymore.
  1103. ------------
  1104. $ git prune-packed
  1105. ------------
  1106. would remove them for you.
  1107. You can try running `find .git/objects -type f` before and after
  1108. you run `git prune-packed` if you are curious. Also `git
  1109. count-objects` would tell you how many unpacked objects are in
  1110. your repository and how much space they are consuming.
  1111. [NOTE]
  1112. `git pull` is slightly cumbersome for HTTP transport, as a
  1113. packed repository may contain relatively few objects in a
  1114. relatively large pack. If you expect many HTTP pulls from your
  1115. public repository you might want to repack & prune often, or
  1116. never.
  1117. If you run `git repack` again at this point, it will say
  1118. "Nothing new to pack.". Once you continue your development and
  1119. accumulate the changes, running `git repack` again will create a
  1120. new pack, that contains objects created since you packed your
  1121. repository the last time. We recommend that you pack your project
  1122. soon after the initial import (unless you are starting your
  1123. project from scratch), and then run `git repack` every once in a
  1124. while, depending on how active your project is.
  1125. When a repository is synchronized via `git push` and `git pull`
  1126. objects packed in the source repository are usually stored
  1127. unpacked in the destination.
  1128. While this allows you to use different packing strategies on
  1129. both ends, it also means you may need to repack both
  1130. repositories every once in a while.
  1131. Working with Others
  1132. -------------------
  1133. Although Git is a truly distributed system, it is often
  1134. convenient to organize your project with an informal hierarchy
  1135. of developers. Linux kernel development is run this way. There
  1136. is a nice illustration (page 17, "Merges to Mainline") in
  1137. https://web.archive.org/web/20120915203609/http://www.xenotime.net/linux/mentor/linux-mentoring-2006.pdf[Randy Dunlap's presentation].
  1138. It should be stressed that this hierarchy is purely *informal*.
  1139. There is nothing fundamental in Git that enforces the "chain of
  1140. patch flow" this hierarchy implies. You do not have to pull
  1141. from only one remote repository.
  1142. A recommended workflow for a "project lead" goes like this:
  1143. 1. Prepare your primary repository on your local machine. Your
  1144. work is done there.
  1145. 2. Prepare a public repository accessible to others.
  1146. +
  1147. If other people are pulling from your repository over dumb
  1148. transport protocols (HTTP), you need to keep this repository
  1149. 'dumb transport friendly'. After `git init`,
  1150. `$GIT_DIR/hooks/post-update.sample` copied from the standard templates
  1151. would contain a call to 'git update-server-info'
  1152. but you need to manually enable the hook with
  1153. `mv post-update.sample post-update`. This makes sure
  1154. 'git update-server-info' keeps the necessary files up to date.
  1155. 3. Push into the public repository from your primary
  1156. repository.
  1157. 4. 'git repack' the public repository. This establishes a big
  1158. pack that contains the initial set of objects as the
  1159. baseline, and possibly 'git prune' if the transport
  1160. used for pulling from your repository supports packed
  1161. repositories.
  1162. 5. Keep working in your primary repository. Your changes
  1163. include modifications of your own, patches you receive via
  1164. e-mails, and merges resulting from pulling the "public"
  1165. repositories of your "subsystem maintainers".
  1166. +
  1167. You can repack this private repository whenever you feel like.
  1168. 6. Push your changes to the public repository, and announce it
  1169. to the public.
  1170. 7. Every once in a while, 'git repack' the public repository.
  1171. Go back to step 5. and continue working.
  1172. A recommended work cycle for a "subsystem maintainer" who works
  1173. on that project and has an own "public repository" goes like this:
  1174. 1. Prepare your work repository, by running 'git clone' on the public
  1175. repository of the "project lead". The URL used for the
  1176. initial cloning is stored in the remote.origin.url
  1177. configuration variable.
  1178. 2. Prepare a public repository accessible to others, just like
  1179. the "project lead" person does.
  1180. 3. Copy over the packed files from "project lead" public
  1181. repository to your public repository, unless the "project
  1182. lead" repository lives on the same machine as yours. In the
  1183. latter case, you can use `objects/info/alternates` file to
  1184. point at the repository you are borrowing from.
  1185. 4. Push into the public repository from your primary
  1186. repository. Run 'git repack', and possibly 'git prune' if the
  1187. transport used for pulling from your repository supports
  1188. packed repositories.
  1189. 5. Keep working in your primary repository. Your changes
  1190. include modifications of your own, patches you receive via
  1191. e-mails, and merges resulting from pulling the "public"
  1192. repositories of your "project lead" and possibly your
  1193. "sub-subsystem maintainers".
  1194. +
  1195. You can repack this private repository whenever you feel
  1196. like.
  1197. 6. Push your changes to your public repository, and ask your
  1198. "project lead" and possibly your "sub-subsystem
  1199. maintainers" to pull from it.
  1200. 7. Every once in a while, 'git repack' the public repository.
  1201. Go back to step 5. and continue working.
  1202. A recommended work cycle for an "individual developer" who does
  1203. not have a "public" repository is somewhat different. It goes
  1204. like this:
  1205. 1. Prepare your work repository, by 'git clone' the public
  1206. repository of the "project lead" (or a "subsystem
  1207. maintainer", if you work on a subsystem). The URL used for
  1208. the initial cloning is stored in the remote.origin.url
  1209. configuration variable.
  1210. 2. Do your work in your repository on 'master' branch.
  1211. 3. Run `git fetch origin` from the public repository of your
  1212. upstream every once in a while. This does only the first
  1213. half of `git pull` but does not merge. The head of the
  1214. public repository is stored in `.git/refs/remotes/origin/master`.
  1215. 4. Use `git cherry origin` to see which ones of your patches
  1216. were accepted, and/or use `git rebase origin` to port your
  1217. unmerged changes forward to the updated upstream.
  1218. 5. Use `git format-patch origin` to prepare patches for e-mail
  1219. submission to your upstream and send it out. Go back to
  1220. step 2. and continue.
  1221. Working with Others, Shared Repository Style
  1222. --------------------------------------------
  1223. If you are coming from a CVS background, the style of cooperation
  1224. suggested in the previous section may be new to you. You do not
  1225. have to worry. Git supports the "shared public repository" style of
  1226. cooperation you are probably more familiar with as well.
  1227. See linkgit:gitcvs-migration[7] for the details.
  1228. Bundling your work together
  1229. ---------------------------
  1230. It is likely that you will be working on more than one thing at
  1231. a time. It is easy to manage those more-or-less independent tasks
  1232. using branches with Git.
  1233. We have already seen how branches work previously,
  1234. with "fun and work" example using two branches. The idea is the
  1235. same if there are more than two branches. Let's say you started
  1236. out from "master" head, and have some new code in the "master"
  1237. branch, and two independent fixes in the "commit-fix" and
  1238. "diff-fix" branches:
  1239. ------------
  1240. $ git show-branch
  1241. ! [commit-fix] Fix commit message normalization.
  1242. ! [diff-fix] Fix rename detection.
  1243. * [master] Release candidate #1
  1244. ---
  1245. + [diff-fix] Fix rename detection.
  1246. + [diff-fix~1] Better common substring algorithm.
  1247. + [commit-fix] Fix commit message normalization.
  1248. * [master] Release candidate #1
  1249. ++* [diff-fix~2] Pretty-print messages.
  1250. ------------
  1251. Both fixes are tested well, and at this point, you want to merge
  1252. in both of them. You could merge in 'diff-fix' first and then
  1253. 'commit-fix' next, like this:
  1254. ------------
  1255. $ git merge -m "Merge fix in diff-fix" diff-fix
  1256. $ git merge -m "Merge fix in commit-fix" commit-fix
  1257. ------------
  1258. Which would result in:
  1259. ------------
  1260. $ git show-branch
  1261. ! [commit-fix] Fix commit message normalization.
  1262. ! [diff-fix] Fix rename detection.
  1263. * [master] Merge fix in commit-fix
  1264. ---
  1265. - [master] Merge fix in commit-fix
  1266. + * [commit-fix] Fix commit message normalization.
  1267. - [master~1] Merge fix in diff-fix
  1268. +* [diff-fix] Fix rename detection.
  1269. +* [diff-fix~1] Better common substring algorithm.
  1270. * [master~2] Release candidate #1
  1271. ++* [master~3] Pretty-print messages.
  1272. ------------
  1273. However, there is no particular reason to merge in one branch
  1274. first and the other next, when what you have are a set of truly
  1275. independent changes (if the order mattered, then they are not
  1276. independent by definition). You could instead merge those two
  1277. branches into the current branch at once. First let's undo what
  1278. we just did and start over. We would want to get the master
  1279. branch before these two merges by resetting it to 'master~2':
  1280. ------------
  1281. $ git reset --hard master~2
  1282. ------------
  1283. You can make sure `git show-branch` matches the state before
  1284. those two 'git merge' you just did. Then, instead of running
  1285. two 'git merge' commands in a row, you would merge these two
  1286. branch heads (this is known as 'making an Octopus'):
  1287. ------------
  1288. $ git merge commit-fix diff-fix
  1289. $ git show-branch
  1290. ! [commit-fix] Fix commit message normalization.
  1291. ! [diff-fix] Fix rename detection.
  1292. * [master] Octopus merge of branches 'diff-fix' and 'commit-fix'
  1293. ---
  1294. - [master] Octopus merge of branches 'diff-fix' and 'commit-fix'
  1295. + * [commit-fix] Fix commit message normalization.
  1296. +* [diff-fix] Fix rename detection.
  1297. +* [diff-fix~1] Better common substring algorithm.
  1298. * [master~1] Release candidate #1
  1299. ++* [master~2] Pretty-print messages.
  1300. ------------
  1301. Note that you should not do Octopus just because you can. An octopus
  1302. is a valid thing to do and often makes it easier to view the
  1303. commit history if you are merging more than two independent
  1304. changes at the same time. However, if you have merge conflicts
  1305. with any of the branches you are merging in and need to hand
  1306. resolve, that is an indication that the development happened in
  1307. those branches were not independent after all, and you should
  1308. merge two at a time, documenting how you resolved the conflicts,
  1309. and the reason why you preferred changes made in one side over
  1310. the other. Otherwise it would make the project history harder
  1311. to follow, not easier.
  1312. SEE ALSO
  1313. --------
  1314. linkgit:gittutorial[7],
  1315. linkgit:gittutorial-2[7],
  1316. linkgit:gitcvs-migration[7],
  1317. linkgit:git-help[1],
  1318. linkgit:giteveryday[7],
  1319. link:user-manual.html[The Git User's Manual]
  1320. GIT
  1321. ---
  1322. Part of the linkgit:git[1] suite