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