1A short git tutorial 2==================== 3 4Introduction 5------------ 6 7This is trying to be a short tutorial on setting up and using a git 8repository, mainly because being hands-on and using explicit examples is 9often the best way of explaining what is going on. 10 11In normal life, most people wouldn't use the "core" git programs 12directly, but rather script around them to make them more palatable. 13Understanding the core git stuff may help some people get those scripts 14done, though, and it may also be instructive in helping people 15understand what it is that the higher-level helper scripts are actually 16doing. 17 18The core git is often called "plumbing", with the prettier user 19interfaces on top of it called "porcelain". You may not want to use the 20plumbing directly very often, but it can be good to know what the 21plumbing does for when the porcelain isn't flushing. 22 23The material presented here often goes deep describing how things 24work internally. If you are mostly interested in using git as a 25SCM, you can skip them during your first pass. 26 27[NOTE] 28And those "too deep" descriptions are often marked as Note. 29 30 31Creating a git repository 32------------------------- 33 34Creating a new git repository couldn't be easier: all git repositories start 35out empty, and the only thing you need to do is find yourself a 36subdirectory that you want to use as a working tree - either an empty 37one for a totally new project, or an existing working tree that you want 38to import into git. 39 40For our first example, we're going to start a totally new repository from 41scratch, with no pre-existing files, and we'll call it `git-tutorial`. 42To start up, create a subdirectory for it, change into that 43subdirectory, and initialize the git infrastructure with `git-init-db`: 44 45------------------------------------------------ 46$ mkdir git-tutorial 47$ cd git-tutorial 48$ git-init-db 49------------------------------------------------ 50 51to which git will reply 52 53---------------- 54defaulting to local storage area 55---------------- 56 57which is just git's way of saying that you haven't been doing anything 58strange, and that it will have created a local `.git` directory setup for 59your new project. You will now have a `.git` directory, and you can 60inspect that with `ls`. For your new empty project, it should show you 61three entries, among other things: 62 63 - a symlink called `HEAD`, pointing to `refs/heads/master` (if your 64 platform does not have native symlinks, it is a file containing the 65 line "ref: refs/heads/master") 66+ 67Don't worry about the fact that the file that the `HEAD` link points to 68doesn't even exist yet -- you haven't created the commit that will 69start your `HEAD` development branch yet. 70 71 - a subdirectory called `objects`, which will contain all the 72 objects of your project. You should never have any real reason to 73 look at the objects directly, but you might want to know that these 74 objects are what contains all the real 'data' in your repository. 75 76 - a subdirectory called `refs`, which contains references to objects. 77 78In particular, the `refs` subdirectory will contain two other 79subdirectories, named `heads` and `tags` respectively. They do 80exactly what their names imply: they contain references to any number 81of different 'heads' of development (aka 'branches'), and to any 82'tags' that you have created to name specific versions in your 83repository. 84 85One note: the special `master` head is the default branch, which is 86why the `.git/HEAD` file was created as a symlink to it even if it 87doesn't yet exist. Basically, the `HEAD` link is supposed to always 88point to the branch you are working on right now, and you always 89start out expecting to work on the `master` branch. 90 91However, this is only a convention, and you can name your branches 92anything you want, and don't have to ever even 'have' a `master` 93branch. A number of the git tools will assume that `.git/HEAD` is 94valid, though. 95 96[NOTE] 97An 'object' is identified by its 160-bit SHA1 hash, aka 'object name', 98and a reference to an object is always the 40-byte hex 99representation of that SHA1 name. The files in the `refs` 100subdirectory are expected to contain these hex references 101(usually with a final `\'\n\'` at the end), and you should thus 102expect to see a number of 41-byte files containing these 103references in these `refs` subdirectories when you actually start 104populating your tree. 105 106[NOTE] 107An advanced user may want to take a look at the 108link:repository-layout.html[repository layout] document 109after finishing this tutorial. 110 111You have now created your first git repository. Of course, since it's 112empty, that's not very useful, so let's start populating it with data. 113 114 115Populating a git repository 116--------------------------- 117 118We'll keep this simple and stupid, so we'll start off with populating a 119few trivial files just to get a feel for it. 120 121Start off with just creating any random files that you want to maintain 122in your git repository. We'll start off with a few bad examples, just to 123get a feel for how this works: 124 125------------------------------------------------ 126$ echo "Hello World" >hello 127$ echo "Silly example" >example 128------------------------------------------------ 129 130you have now created two files in your working tree (aka 'working directory'), but to 131actually check in your hard work, you will have to go through two steps: 132 133 - fill in the 'index' file (aka 'cache') with the information about your 134 working tree state. 135 136 - commit that index file as an object. 137 138The first step is trivial: when you want to tell git about any changes 139to your working tree, you use the `git-update-index` program. That 140program normally just takes a list of filenames you want to update, but 141to avoid trivial mistakes, it refuses to add new entries to the index 142(or remove existing ones) unless you explicitly tell it that you're 143adding a new entry with the `\--add` flag (or removing an entry with the 144`\--remove`) flag. 145 146So to populate the index with the two files you just created, you can do 147 148------------------------------------------------ 149$ git-update-index --add hello example 150------------------------------------------------ 151 152and you have now told git to track those two files. 153 154In fact, as you did that, if you now look into your object directory, 155you'll notice that git will have added two new objects to the object 156database. If you did exactly the steps above, you should now be able to do 157 158 159---------------- 160$ ls .git/objects/??/* 161---------------- 162 163and see two files: 164 165---------------- 166.git/objects/55/7db03de997c86a4a028e1ebd3a1ceb225be238 167.git/objects/f2/4c74a2e500f5ee1332c86b94199f52b1d1d962 168---------------- 169 170which correspond with the objects with names of 557db... and f24c7.. 171respectively. 172 173If you want to, you can use `git-cat-file` to look at those objects, but 174you'll have to use the object name, not the filename of the object: 175 176---------------- 177$ git-cat-file -t 557db03de997c86a4a028e1ebd3a1ceb225be238 178---------------- 179 180where the `-t` tells `git-cat-file` to tell you what the "type" of the 181object is. git will tell you that you have a "blob" object (ie just a 182regular file), and you can see the contents with 183 184---------------- 185$ git-cat-file "blob" 557db03 186---------------- 187 188which will print out "Hello World". The object 557db03 is nothing 189more than the contents of your file `hello`. 190 191[NOTE] 192Don't confuse that object with the file `hello` itself. The 193object is literally just those specific *contents* of the file, and 194however much you later change the contents in file `hello`, the object 195we just looked at will never change. Objects are immutable. 196 197[NOTE] 198The second example demonstrates that you can 199abbreviate the object name to only the first several 200hexadecimal digits in most places. 201 202Anyway, as we mentioned previously, you normally never actually take a 203look at the objects themselves, and typing long 40-character hex 204names is not something you'd normally want to do. The above digression 205was just to show that `git-update-index` did something magical, and 206actually saved away the contents of your files into the git object 207database. 208 209Updating the index did something else too: it created a `.git/index` 210file. This is the index that describes your current working tree, and 211something you should be very aware of. Again, you normally never worry 212about the index file itself, but you should be aware of the fact that 213you have not actually really "checked in" your files into git so far, 214you've only *told* git about them. 215 216However, since git knows about them, you can now start using some of the 217most basic git commands to manipulate the files or look at their status. 218 219In particular, let's not even check in the two files into git yet, we'll 220start off by adding another line to `hello` first: 221 222------------------------------------------------ 223$ echo "It's a new day for git" >>hello 224------------------------------------------------ 225 226and you can now, since you told git about the previous state of `hello`, ask 227git what has changed in the tree compared to your old index, using the 228`git-diff-files` command: 229 230------------ 231$ git-diff-files 232------------ 233 234Oops. That wasn't very readable. It just spit out its own internal 235version of a `diff`, but that internal version really just tells you 236that it has noticed that "hello" has been modified, and that the old object 237contents it had have been replaced with something else. 238 239To make it readable, we can tell git-diff-files to output the 240differences as a patch, using the `-p` flag: 241 242------------ 243$ git-diff-files -p 244diff --git a/hello b/hello 245index 557db03..263414f 100644 246--- a/hello 247+++ b/hello 248@@ -1 +1,2 @@ 249 Hello World 250+It's a new day for git 251---- 252 253i.e. the diff of the change we caused by adding another line to `hello`. 254 255In other words, `git-diff-files` always shows us the difference between 256what is recorded in the index, and what is currently in the working 257tree. That's very useful. 258 259A common shorthand for `git-diff-files -p` is to just write `git 260diff`, which will do the same thing. 261 262------------ 263$ git diff 264diff --git a/hello b/hello 265index 557db03..263414f 100644 266--- a/hello 267+++ b/hello 268@@ -1 +1,2 @@ 269 Hello World 270+It's a new day for git 271------------ 272 273 274Committing git state 275-------------------- 276 277Now, we want to go to the next stage in git, which is to take the files 278that git knows about in the index, and commit them as a real tree. We do 279that in two phases: creating a 'tree' object, and committing that 'tree' 280object as a 'commit' object together with an explanation of what the 281tree was all about, along with information of how we came to that state. 282 283Creating a tree object is trivial, and is done with `git-write-tree`. 284There are no options or other input: git-write-tree will take the 285current index state, and write an object that describes that whole 286index. In other words, we're now tying together all the different 287filenames with their contents (and their permissions), and we're 288creating the equivalent of a git "directory" object: 289 290------------------------------------------------ 291$ git-write-tree 292------------------------------------------------ 293 294and this will just output the name of the resulting tree, in this case 295(if you have done exactly as I've described) it should be 296 297---------------- 2988988da15d077d4829fc51d8544c097def6644dbb 299---------------- 300 301which is another incomprehensible object name. Again, if you want to, 302you can use `git-cat-file -t 8988d\...` to see that this time the object 303is not a "blob" object, but a "tree" object (you can also use 304`git-cat-file` to actually output the raw object contents, but you'll see 305mainly a binary mess, so that's less interesting). 306 307However -- normally you'd never use `git-write-tree` on its own, because 308normally you always commit a tree into a commit object using the 309`git-commit-tree` command. In fact, it's easier to not actually use 310`git-write-tree` on its own at all, but to just pass its result in as an 311argument to `git-commit-tree`. 312 313`git-commit-tree` normally takes several arguments -- it wants to know 314what the 'parent' of a commit was, but since this is the first commit 315ever in this new repository, and it has no parents, we only need to pass in 316the object name of the tree. However, `git-commit-tree` 317also wants to get a commit message 318on its standard input, and it will write out the resulting object name for the 319commit to its standard output. 320 321And this is where we create the `.git/refs/heads/master` file 322which is pointed at by `HEAD`. This file is supposed to contain 323the reference to the top-of-tree of the master branch, and since 324that's exactly what `git-commit-tree` spits out, we can do this 325all with a sequence of simple shell commands: 326 327------------------------------------------------ 328$ tree=$(git-write-tree) 329$ commit=$(echo 'Initial commit' | git-commit-tree $tree) 330$ git-update-ref HEAD $commit 331------------------------------------------------ 332 333which will say: 334 335---------------- 336Committing initial tree 8988da15d077d4829fc51d8544c097def6644dbb 337---------------- 338 339just to warn you about the fact that it created a totally new commit 340that is not related to anything else. Normally you do this only *once* 341for a project ever, and all later commits will be parented on top of an 342earlier commit, and you'll never see this "Committing initial tree" 343message ever again. 344 345Again, normally you'd never actually do this by hand. There is a 346helpful script called `git commit` that will do all of this for you. So 347you could have just written `git commit` 348instead, and it would have done the above magic scripting for you. 349 350 351Making a change 352--------------- 353 354Remember how we did the `git-update-index` on file `hello` and then we 355changed `hello` afterward, and could compare the new state of `hello` with the 356state we saved in the index file? 357 358Further, remember how I said that `git-write-tree` writes the contents 359of the *index* file to the tree, and thus what we just committed was in 360fact the *original* contents of the file `hello`, not the new ones. We did 361that on purpose, to show the difference between the index state, and the 362state in the working tree, and how they don't have to match, even 363when we commit things. 364 365As before, if we do `git-diff-files -p` in our git-tutorial project, 366we'll still see the same difference we saw last time: the index file 367hasn't changed by the act of committing anything. However, now that we 368have committed something, we can also learn to use a new command: 369`git-diff-index`. 370 371Unlike `git-diff-files`, which showed the difference between the index 372file and the working tree, `git-diff-index` shows the differences 373between a committed *tree* and either the index file or the working 374tree. In other words, `git-diff-index` wants a tree to be diffed 375against, and before we did the commit, we couldn't do that, because we 376didn't have anything to diff against. 377 378But now we can do 379 380---------------- 381$ git-diff-index -p HEAD 382---------------- 383 384(where `-p` has the same meaning as it did in `git-diff-files`), and it 385will show us the same difference, but for a totally different reason. 386Now we're comparing the working tree not against the index file, 387but against the tree we just wrote. It just so happens that those two 388are obviously the same, so we get the same result. 389 390Again, because this is a common operation, you can also just shorthand 391it with 392 393---------------- 394$ git diff HEAD 395---------------- 396 397which ends up doing the above for you. 398 399In other words, `git-diff-index` normally compares a tree against the 400working tree, but when given the `\--cached` flag, it is told to 401instead compare against just the index cache contents, and ignore the 402current working tree state entirely. Since we just wrote the index 403file to HEAD, doing `git-diff-index \--cached -p HEAD` should thus return 404an empty set of differences, and that's exactly what it does. 405 406[NOTE] 407================ 408`git-diff-index` really always uses the index for its 409comparisons, and saying that it compares a tree against the working 410tree is thus not strictly accurate. In particular, the list of 411files to compare (the "meta-data") *always* comes from the index file, 412regardless of whether the `\--cached` flag is used or not. The `\--cached` 413flag really only determines whether the file *contents* to be compared 414come from the working tree or not. 415 416This is not hard to understand, as soon as you realize that git simply 417never knows (or cares) about files that it is not told about 418explicitly. git will never go *looking* for files to compare, it 419expects you to tell it what the files are, and that's what the index 420is there for. 421================ 422 423However, our next step is to commit the *change* we did, and again, to 424understand what's going on, keep in mind the difference between "working 425tree contents", "index file" and "committed tree". We have changes 426in the working tree that we want to commit, and we always have to 427work through the index file, so the first thing we need to do is to 428update the index cache: 429 430------------------------------------------------ 431$ git-update-index hello 432------------------------------------------------ 433 434(note how we didn't need the `\--add` flag this time, since git knew 435about the file already). 436 437Note what happens to the different `git-diff-\*` versions here. After 438we've updated `hello` in the index, `git-diff-files -p` now shows no 439differences, but `git-diff-index -p HEAD` still *does* show that the 440current state is different from the state we committed. In fact, now 441`git-diff-index` shows the same difference whether we use the `--cached` 442flag or not, since now the index is coherent with the working tree. 443 444Now, since we've updated `hello` in the index, we can commit the new 445version. We could do it by writing the tree by hand again, and 446committing the tree (this time we'd have to use the `-p HEAD` flag to 447tell commit that the HEAD was the *parent* of the new commit, and that 448this wasn't an initial commit any more), but you've done that once 449already, so let's just use the helpful script this time: 450 451------------------------------------------------ 452$ git commit 453------------------------------------------------ 454 455which starts an editor for you to write the commit message and tells you 456a bit about what you have done. 457 458Write whatever message you want, and all the lines that start with '#' 459will be pruned out, and the rest will be used as the commit message for 460the change. If you decide you don't want to commit anything after all at 461this point (you can continue to edit things and update the index), you 462can just leave an empty message. Otherwise `git commit` will commit 463the change for you. 464 465You've now made your first real git commit. And if you're interested in 466looking at what `git commit` really does, feel free to investigate: 467it's a few very simple shell scripts to generate the helpful (?) commit 468message headers, and a few one-liners that actually do the 469commit itself (`git-commit`). 470 471 472Inspecting Changes 473------------------ 474 475While creating changes is useful, it's even more useful if you can tell 476later what changed. The most useful command for this is another of the 477`diff` family, namely `git-diff-tree`. 478 479`git-diff-tree` can be given two arbitrary trees, and it will tell you the 480differences between them. Perhaps even more commonly, though, you can 481give it just a single commit object, and it will figure out the parent 482of that commit itself, and show the difference directly. Thus, to get 483the same diff that we've already seen several times, we can now do 484 485---------------- 486$ git-diff-tree -p HEAD 487---------------- 488 489(again, `-p` means to show the difference as a human-readable patch), 490and it will show what the last commit (in `HEAD`) actually changed. 491 492[NOTE] 493============ 494Here is an ASCII art by Jon Loeliger that illustrates how 495various diff-\* commands compare things. 496 497 diff-tree 498 +----+ 499 | | 500 | | 501 V V 502 +-----------+ 503 | Object DB | 504 | Backing | 505 | Store | 506 +-----------+ 507 ^ ^ 508 | | 509 | | diff-index --cached 510 | | 511 diff-index | V 512 | +-----------+ 513 | | Index | 514 | | "cache" | 515 | +-----------+ 516 | ^ 517 | | 518 | | diff-files 519 | | 520 V V 521 +-----------+ 522 | Working | 523 | Directory | 524 +-----------+ 525============ 526 527More interestingly, you can also give `git-diff-tree` the `-v` flag, which 528tells it to also show the commit message and author and date of the 529commit, and you can tell it to show a whole series of diffs. 530Alternatively, you can tell it to be "silent", and not show the diffs at 531all, but just show the actual commit message. 532 533In fact, together with the `git-rev-list` program (which generates a 534list of revisions), `git-diff-tree` ends up being a veritable fount of 535changes. A trivial (but very useful) script called `git-whatchanged` is 536included with git which does exactly this, and shows a log of recent 537activities. 538 539To see the whole history of our pitiful little git-tutorial project, you 540can do 541 542---------------- 543$ git log 544---------------- 545 546which shows just the log messages, or if we want to see the log together 547with the associated patches use the more complex (and much more 548powerful) 549 550---------------- 551$ git-whatchanged -p --root 552---------------- 553 554and you will see exactly what has changed in the repository over its 555short history. 556 557[NOTE] 558The `\--root` flag is a flag to `git-diff-tree` to tell it to 559show the initial aka 'root' commit too. Normally you'd probably not 560want to see the initial import diff, but since the tutorial project 561was started from scratch and is so small, we use it to make the result 562a bit more interesting. 563 564With that, you should now be having some inkling of what git does, and 565can explore on your own. 566 567[NOTE] 568Most likely, you are not directly using the core 569git Plumbing commands, but using Porcelain like Cogito on top 570of it. Cogito works a bit differently and you usually do not 571have to run `git-update-index` yourself for changed files (you 572do tell underlying git about additions and removals via 573`cg-add` and `cg-rm` commands). Just before you make a commit 574with `cg-commit`, Cogito figures out which files you modified, 575and runs `git-update-index` on them for you. 576 577 578Tagging a version 579----------------- 580 581In git, there are two kinds of tags, a "light" one, and an "annotated tag". 582 583A "light" tag is technically nothing more than a branch, except we put 584it in the `.git/refs/tags/` subdirectory instead of calling it a `head`. 585So the simplest form of tag involves nothing more than 586 587------------------------------------------------ 588$ git tag my-first-tag 589------------------------------------------------ 590 591which just writes the current `HEAD` into the `.git/refs/tags/my-first-tag` 592file, after which point you can then use this symbolic name for that 593particular state. You can, for example, do 594 595---------------- 596$ git diff my-first-tag 597---------------- 598 599to diff your current state against that tag (which at this point will 600obviously be an empty diff, but if you continue to develop and commit 601stuff, you can use your tag as an "anchor-point" to see what has changed 602since you tagged it. 603 604An "annotated tag" is actually a real git object, and contains not only a 605pointer to the state you want to tag, but also a small tag name and 606message, along with optionally a PGP signature that says that yes, 607you really did 608that tag. You create these annotated tags with either the `-a` or 609`-s` flag to `git tag`: 610 611---------------- 612$ git tag -s <tagname> 613---------------- 614 615which will sign the current `HEAD` (but you can also give it another 616argument that specifies the thing to tag, ie you could have tagged the 617current `mybranch` point by using `git tag <tagname> mybranch`). 618 619You normally only do signed tags for major releases or things 620like that, while the light-weight tags are useful for any marking you 621want to do -- any time you decide that you want to remember a certain 622point, just create a private tag for it, and you have a nice symbolic 623name for the state at that point. 624 625 626Copying repositories 627-------------------- 628 629git repositories are normally totally self-sufficient and relocatable 630Unlike CVS, for example, there is no separate notion of 631"repository" and "working tree". A git repository normally *is* the 632working tree, with the local git information hidden in the `.git` 633subdirectory. There is nothing else. What you see is what you got. 634 635[NOTE] 636You can tell git to split the git internal information from 637the directory that it tracks, but we'll ignore that for now: it's not 638how normal projects work, and it's really only meant for special uses. 639So the mental model of "the git information is always tied directly to 640the working tree that it describes" may not be technically 100% 641accurate, but it's a good model for all normal use. 642 643This has two implications: 644 645 - if you grow bored with the tutorial repository you created (or you've 646 made a mistake and want to start all over), you can just do simple 647+ 648---------------- 649$ rm -rf git-tutorial 650---------------- 651+ 652and it will be gone. There's no external repository, and there's no 653history outside the project you created. 654 655 - if you want to move or duplicate a git repository, you can do so. There 656 is `git clone` command, but if all you want to do is just to 657 create a copy of your repository (with all the full history that 658 went along with it), you can do so with a regular 659 `cp -a git-tutorial new-git-tutorial`. 660+ 661Note that when you've moved or copied a git repository, your git index 662file (which caches various information, notably some of the "stat" 663information for the files involved) will likely need to be refreshed. 664So after you do a `cp -a` to create a new copy, you'll want to do 665+ 666---------------- 667$ git-update-index --refresh 668---------------- 669+ 670in the new repository to make sure that the index file is up-to-date. 671 672Note that the second point is true even across machines. You can 673duplicate a remote git repository with *any* regular copy mechanism, be it 674`scp`, `rsync` or `wget`. 675 676When copying a remote repository, you'll want to at a minimum update the 677index cache when you do this, and especially with other peoples' 678repositories you often want to make sure that the index cache is in some 679known state (you don't know *what* they've done and not yet checked in), 680so usually you'll precede the `git-update-index` with a 681 682---------------- 683$ git-read-tree --reset HEAD 684$ git-update-index --refresh 685---------------- 686 687which will force a total index re-build from the tree pointed to by `HEAD`. 688It resets the index contents to `HEAD`, and then the `git-update-index` 689makes sure to match up all index entries with the checked-out files. 690If the original repository had uncommitted changes in its 691working tree, `git-update-index --refresh` notices them and 692tells you they need to be updated. 693 694The above can also be written as simply 695 696---------------- 697$ git reset 698---------------- 699 700and in fact a lot of the common git command combinations can be scripted 701with the `git xyz` interfaces. You can learn things by just looking 702at what the various git scripts do. For example, `git reset` is the 703above two lines implemented in `git-reset`, but some things like 704`git status` and `git commit` are slightly more complex scripts around 705the basic git commands. 706 707Many (most?) public remote repositories will not contain any of 708the checked out files or even an index file, and will *only* contain the 709actual core git files. Such a repository usually doesn't even have the 710`.git` subdirectory, but has all the git files directly in the 711repository. 712 713To create your own local live copy of such a "raw" git repository, you'd 714first create your own subdirectory for the project, and then copy the 715raw repository contents into the `.git` directory. For example, to 716create your own copy of the git repository, you'd do the following 717 718---------------- 719$ mkdir my-git 720$ cd my-git 721$ rsync -rL rsync://rsync.kernel.org/pub/scm/git/git.git/ .git 722---------------- 723 724followed by 725 726---------------- 727$ git-read-tree HEAD 728---------------- 729 730to populate the index. However, now you have populated the index, and 731you have all the git internal files, but you will notice that you don't 732actually have any of the working tree files to work on. To get 733those, you'd check them out with 734 735---------------- 736$ git-checkout-index -u -a 737---------------- 738 739where the `-u` flag means that you want the checkout to keep the index 740up-to-date (so that you don't have to refresh it afterward), and the 741`-a` flag means "check out all files" (if you have a stale copy or an 742older version of a checked out tree you may also need to add the `-f` 743flag first, to tell git-checkout-index to *force* overwriting of any old 744files). 745 746Again, this can all be simplified with 747 748---------------- 749$ git clone rsync://rsync.kernel.org/pub/scm/git/git.git/ my-git 750$ cd my-git 751$ git checkout 752---------------- 753 754which will end up doing all of the above for you. 755 756You have now successfully copied somebody else's (mine) remote 757repository, and checked it out. 758 759 760Creating a new branch 761--------------------- 762 763Branches in git are really nothing more than pointers into the git 764object database from within the `.git/refs/` subdirectory, and as we 765already discussed, the `HEAD` branch is nothing but a symlink to one of 766these object pointers. 767 768You can at any time create a new branch by just picking an arbitrary 769point in the project history, and just writing the SHA1 name of that 770object into a file under `.git/refs/heads/`. You can use any filename you 771want (and indeed, subdirectories), but the convention is that the 772"normal" branch is called `master`. That's just a convention, though, 773and nothing enforces it. 774 775To show that as an example, let's go back to the git-tutorial repository we 776used earlier, and create a branch in it. You do that by simply just 777saying that you want to check out a new branch: 778 779------------ 780$ git checkout -b mybranch 781------------ 782 783will create a new branch based at the current `HEAD` position, and switch 784to it. 785 786[NOTE] 787================================================ 788If you make the decision to start your new branch at some 789other point in the history than the current `HEAD`, you can do so by 790just telling `git checkout` what the base of the checkout would be. 791In other words, if you have an earlier tag or branch, you'd just do 792 793------------ 794$ git checkout -b mybranch earlier-commit 795------------ 796 797and it would create the new branch `mybranch` at the earlier commit, 798and check out the state at that time. 799================================================ 800 801You can always just jump back to your original `master` branch by doing 802 803------------ 804$ git checkout master 805------------ 806 807(or any other branch-name, for that matter) and if you forget which 808branch you happen to be on, a simple 809 810------------ 811$ ls -l .git/HEAD 812------------ 813 814will tell you where it's pointing (Note that on platforms with bad or no 815symlink support, you have to execute 816 817------------ 818$ cat .git/HEAD 819------------ 820 821instead). To get the list of branches you have, you can say 822 823------------ 824$ git branch 825------------ 826 827which is nothing more than a simple script around `ls .git/refs/heads`. 828There will be asterisk in front of the branch you are currently on. 829 830Sometimes you may wish to create a new branch _without_ actually 831checking it out and switching to it. If so, just use the command 832 833------------ 834$ git branch <branchname> [startingpoint] 835------------ 836 837which will simply _create_ the branch, but will not do anything further. 838You can then later -- once you decide that you want to actually develop 839on that branch -- switch to that branch with a regular `git checkout` 840with the branchname as the argument. 841 842 843Merging two branches 844-------------------- 845 846One of the ideas of having a branch is that you do some (possibly 847experimental) work in it, and eventually merge it back to the main 848branch. So assuming you created the above `mybranch` that started out 849being the same as the original `master` branch, let's make sure we're in 850that branch, and do some work there. 851 852------------------------------------------------ 853$ git checkout mybranch 854$ echo "Work, work, work" >>hello 855$ git commit -m 'Some work.' hello 856------------------------------------------------ 857 858Here, we just added another line to `hello`, and we used a shorthand for 859doing both `git-update-index hello` and `git commit` by just giving the 860filename directly to `git commit`. The `-m` flag is to give the 861commit log message from the command line. 862 863Now, to make it a bit more interesting, let's assume that somebody else 864does some work in the original branch, and simulate that by going back 865to the master branch, and editing the same file differently there: 866 867------------ 868$ git checkout master 869------------ 870 871Here, take a moment to look at the contents of `hello`, and notice how they 872don't contain the work we just did in `mybranch` -- because that work 873hasn't happened in the `master` branch at all. Then do 874 875------------ 876$ echo "Play, play, play" >>hello 877$ echo "Lots of fun" >>example 878$ git commit -m 'Some fun.' hello example 879------------ 880 881since the master branch is obviously in a much better mood. 882 883Now, you've got two branches, and you decide that you want to merge the 884work done. Before we do that, let's introduce a cool graphical tool that 885helps you view what's going on: 886 887---------------- 888$ gitk --all 889---------------- 890 891will show you graphically both of your branches (that's what the `\--all` 892means: normally it will just show you your current `HEAD`) and their 893histories. You can also see exactly how they came to be from a common 894source. 895 896Anyway, let's exit `gitk` (`^Q` or the File menu), and decide that we want 897to merge the work we did on the `mybranch` branch into the `master` 898branch (which is currently our `HEAD` too). To do that, there's a nice 899script called `git merge`, which wants to know which branches you want 900to resolve and what the merge is all about: 901 902------------ 903$ git merge "Merge work in mybranch" HEAD mybranch 904------------ 905 906where the first argument is going to be used as the commit message if 907the merge can be resolved automatically. 908 909Now, in this case we've intentionally created a situation where the 910merge will need to be fixed up by hand, though, so git will do as much 911of it as it can automatically (which in this case is just merge the `example` 912file, which had no differences in the `mybranch` branch), and say: 913 914---------------- 915 Trying really trivial in-index merge... 916 fatal: Merge requires file-level merging 917 Nope. 918 ... 919 Auto-merging hello 920 CONFLICT (content): Merge conflict in hello 921 Automatic merge failed/prevented; fix up by hand 922---------------- 923 924which is way too verbose, but it basically tells you that it failed the 925really trivial merge ("Simple merge") and did an "Automatic merge" 926instead, but that too failed due to conflicts in `hello`. 927 928Not to worry. It left the (trivial) conflict in `hello` in the same form you 929should already be well used to if you've ever used CVS, so let's just 930open `hello` in our editor (whatever that may be), and fix it up somehow. 931I'd suggest just making it so that `hello` contains all four lines: 932 933------------ 934Hello World 935It's a new day for git 936Play, play, play 937Work, work, work 938------------ 939 940and once you're happy with your manual merge, just do a 941 942------------ 943$ git commit hello 944------------ 945 946which will very loudly warn you that you're now committing a merge 947(which is correct, so never mind), and you can write a small merge 948message about your adventures in git-merge-land. 949 950After you're done, start up `gitk \--all` to see graphically what the 951history looks like. Notice that `mybranch` still exists, and you can 952switch to it, and continue to work with it if you want to. The 953`mybranch` branch will not contain the merge, but next time you merge it 954from the `master` branch, git will know how you merged it, so you'll not 955have to do _that_ merge again. 956 957Another useful tool, especially if you do not always work in X-Window 958environment, is `git show-branch`. 959 960------------------------------------------------ 961$ git show-branch master mybranch 962* [master] Merge work in mybranch 963 ! [mybranch] Some work. 964-- 965+ [master] Merge work in mybranch 966++ [mybranch] Some work. 967------------------------------------------------ 968 969The first two lines indicate that it is showing the two branches 970and the first line of the commit log message from their 971top-of-the-tree commits, you are currently on `master` branch 972(notice the asterisk `*` character), and the first column for 973the later output lines is used to show commits contained in the 974`master` branch, and the second column for the `mybranch` 975branch. Three commits are shown along with their log messages. 976All of them have plus `+` characters in the first column, which 977means they are now part of the `master` branch. Only the "Some 978work" commit has the plus `+` character in the second column, 979because `mybranch` has not been merged to incorporate these 980commits from the master branch. The string inside brackets 981before the commit log message is a short name you can use to 982name the commit. In the above example, 'master' and 'mybranch' 983are branch heads. 'master~1' is the first parent of 'master' 984branch head. Please see 'git-rev-parse' documentation if you 985see more complex cases. 986 987Now, let's pretend you are the one who did all the work in 988`mybranch`, and the fruit of your hard work has finally been merged 989to the `master` branch. Let's go back to `mybranch`, and run 990resolve to get the "upstream changes" back to your branch. 991 992------------ 993$ git checkout mybranch 994$ git merge "Merge upstream changes." HEAD master 995------------ 996 997This outputs something like this (the actual commit object names 998would be different) 9991000----------------1001Updating from ae3a2da... to a80b4aa....1002 example | 1 +1003 hello | 1 +1004 2 files changed, 2 insertions(+), 0 deletions(-)1005----------------10061007Because your branch did not contain anything more than what are1008already merged into the `master` branch, the resolve operation did1009not actually do a merge. Instead, it just updated the top of1010the tree of your branch to that of the `master` branch. This is1011often called 'fast forward' merge.10121013You can run `gitk \--all` again to see how the commit ancestry1014looks like, or run `show-branch`, which tells you this.10151016------------------------------------------------1017$ git show-branch master mybranch1018! [master] Merge work in mybranch1019 * [mybranch] Merge work in mybranch1020--1021++ [master] Merge work in mybranch1022------------------------------------------------102310241025Merging external work1026---------------------10271028It's usually much more common that you merge with somebody else than1029merging with your own branches, so it's worth pointing out that git1030makes that very easy too, and in fact, it's not that different from1031doing a `git merge`. In fact, a remote merge ends up being nothing1032more than "fetch the work from a remote repository into a temporary tag"1033followed by a `git merge`.10341035Fetching from a remote repository is done by, unsurprisingly,1036`git fetch`:10371038----------------1039$ git fetch <remote-repository>1040----------------10411042One of the following transports can be used to name the1043repository to download from:10441045Rsync::1046 `rsync://remote.machine/path/to/repo.git/`1047+1048Rsync transport is usable for both uploading and downloading,1049but is completely unaware of what git does, and can produce1050unexpected results when you download from the public repository1051while the repository owner is uploading into it via `rsync`1052transport. Most notably, it could update the files under1053`refs/` which holds the object name of the topmost commits1054before uploading the files in `objects/` -- the downloader would1055obtain head commit object name while that object itself is still1056not available in the repository. For this reason, it is1057considered deprecated.10581059SSH::1060 `remote.machine:/path/to/repo.git/` or1061+1062`ssh://remote.machine/path/to/repo.git/`1063+1064This transport can be used for both uploading and downloading,1065and requires you to have a log-in privilege over `ssh` to the1066remote machine. It finds out the set of objects the other side1067lacks by exchanging the head commits both ends have and1068transfers (close to) minimum set of objects. It is by far the1069most efficient way to exchange git objects between repositories.10701071Local directory::1072 `/path/to/repo.git/`1073+1074This transport is the same as SSH transport but uses `sh` to run1075both ends on the local machine instead of running other end on1076the remote machine via `ssh`.10771078git Native::1079 `git://remote.machine/path/to/repo.git/`1080+1081This transport was designed for anonymous downloading. Like SSH1082transport, it finds out the set of objects the downstream side1083lacks and transfers (close to) minimum set of objects.10841085HTTP(S)::1086 `http://remote.machine/path/to/repo.git/`1087+1088HTTP and HTTPS transport are used only for downloading. They1089first obtain the topmost commit object name from the remote site1090by looking at `repo.git/info/refs` file, tries to obtain the1091commit object by downloading from `repo.git/objects/xx/xxx\...`1092using the object name of that commit object. Then it reads the1093commit object to find out its parent commits and the associate1094tree object; it repeats this process until it gets all the1095necessary objects. Because of this behaviour, they are1096sometimes also called 'commit walkers'.1097+1098The 'commit walkers' are sometimes also called 'dumb1099transports', because they do not require any git aware smart1100server like git Native transport does. Any stock HTTP server1101would suffice.1102+1103There are (confusingly enough) `git-ssh-fetch` and `git-ssh-upload`1104programs, which are 'commit walkers'; they outlived their1105usefulness when git Native and SSH transports were introduced,1106and not used by `git pull` or `git push` scripts.11071108Once you fetch from the remote repository, you `resolve` that1109with your current branch.11101111However -- it's such a common thing to `fetch` and then1112immediately `resolve`, that it's called `git pull`, and you can1113simply do11141115----------------1116$ git pull <remote-repository>1117----------------11181119and optionally give a branch-name for the remote end as a second1120argument.11211122[NOTE]1123You could do without using any branches at all, by1124keeping as many local repositories as you would like to have1125branches, and merging between them with `git pull`, just like1126you merge between branches. The advantage of this approach is1127that it lets you keep set of files for each `branch` checked1128out and you may find it easier to switch back and forth if you1129juggle multiple lines of development simultaneously. Of1130course, you will pay the price of more disk usage to hold1131multiple working trees, but disk space is cheap these days.11321133[NOTE]1134You could even pull from your own repository by1135giving '.' as <remote-repository> parameter to `git pull`. This1136is useful when you want to merge a local branch (or more, if you1137are making an Octopus) into the current branch.11381139It is likely that you will be pulling from the same remote1140repository from time to time. As a short hand, you can store1141the remote repository URL in a file under .git/remotes/1142directory, like this:11431144------------------------------------------------1145$ mkdir -p .git/remotes/1146$ cat >.git/remotes/linus <<\EOF1147URL: http://www.kernel.org/pub/scm/git/git.git/1148EOF1149------------------------------------------------11501151and use the filename to `git pull` instead of the full URL.1152The URL specified in such file can even be a prefix1153of a full URL, like this:11541155------------------------------------------------1156$ cat >.git/remotes/jgarzik <<\EOF1157URL: http://www.kernel.org/pub/scm/linux/git/jgarzik/1158EOF1159------------------------------------------------116011611162Examples.11631164. `git pull linus`1165. `git pull linus tag v0.99.1`1166. `git pull jgarzik/netdev-2.6.git/ e100`11671168the above are equivalent to:11691170. `git pull http://www.kernel.org/pub/scm/git/git.git/ HEAD`1171. `git pull http://www.kernel.org/pub/scm/git/git.git/ tag v0.99.1`1172. `git pull http://www.kernel.org/pub/.../jgarzik/netdev-2.6.git e100`117311741175How does the merge work?1176------------------------11771178We said this tutorial shows what plumbing does to help you cope1179with the porcelain that isn't flushing, but we so far did not1180talk about how the merge really works. If you are following1181this tutorial the first time, I'd suggest to skip to "Publishing1182your work" section and come back here later.11831184OK, still with me? To give us an example to look at, let's go1185back to the earlier repository with "hello" and "example" file,1186and bring ourselves back to the pre-merge state:11871188------------1189$ git show-branch --more=3 master mybranch1190! [master] Merge work in mybranch1191 * [mybranch] Merge work in mybranch1192--1193++ [master] Merge work in mybranch1194++ [master^2] Some work.1195++ [master^] Some fun.1196------------11971198Remember, before running `git merge`, our `master` head was at1199"Some fun." commit, while our `mybranch` head was at "Some1200work." commit.12011202------------1203$ git checkout mybranch1204$ git reset --hard master^21205$ git checkout master1206$ git reset --hard master^1207------------12081209After rewinding, the commit structure should look like this:12101211------------1212$ git show-branch1213* [master] Some fun.1214 ! [mybranch] Some work.1215--1216 + [mybranch] Some work.1217+ [master] Some fun.1218++ [mybranch^] New day.1219------------12201221Now we are ready to experiment with the merge by hand.12221223`git merge` command, when merging two branches, uses 3-way merge1224algorithm. First, it finds the common ancestor between them.1225The command it uses is `git-merge-base`:12261227------------1228$ mb=$(git-merge-base HEAD mybranch)1229------------12301231The command writes the commit object name of the common ancestor1232to the standard output, so we captured its output to a variable,1233because we will be using it in the next step. BTW, the common1234ancestor commit is the "New day." commit in this case. You can1235tell it by:12361237------------1238$ git-name-rev $mb1239my-first-tag1240------------12411242After finding out a common ancestor commit, the second step is1243this:12441245------------1246$ git-read-tree -m -u $mb HEAD mybranch1247------------12481249This is the same `git-read-tree` command we have already seen,1250but it takes three trees, unlike previous examples. This reads1251the contents of each tree into different 'stage' in the index1252file (the first tree goes to stage 1, the second stage 2,1253etc.). After reading three trees into three stages, the paths1254that are the same in all three stages are 'collapsed' into stage12550. Also paths that are the same in two of three stages are1256collapsed into stage 0, taking the SHA1 from either stage 2 or1257stage 3, whichever is different from stage 1 (i.e. only one side1258changed from the common ancestor).12591260After 'collapsing' operation, paths that are different in three1261trees are left in non-zero stages. At this point, you can1262inspect the index file with this command:12631264------------1265$ git-ls-files --stage1266100644 7f8b141b65fdcee47321e399a2598a235a032422 0 example1267100644 263414f423d0e4d70dae8fe53fa34614ff3e2860 1 hello1268100644 06fa6a24256dc7e560efa5687fa84b51f0263c3a 2 hello1269100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello1270------------12711272In our example of only two files, we did not have unchanged1273files so only 'example' resulted in collapsing, but in real-life1274large projects, only small number of files change in one commit,1275and this 'collapsing' tends to trivially merge most of the paths1276fairly quickly, leaving only a handful the real changes in non-zero1277stages.12781279To look at only non-zero stages, use `\--unmerged` flag:12801281------------1282$ git-ls-files --unmerged1283100644 263414f423d0e4d70dae8fe53fa34614ff3e2860 1 hello1284100644 06fa6a24256dc7e560efa5687fa84b51f0263c3a 2 hello1285100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello1286------------12871288The next step of merging is to merge these three versions of the1289file, using 3-way merge. This is done by giving1290`git-merge-one-file` command as one of the arguments to1291`git-merge-index` command:12921293------------1294$ git-merge-index git-merge-one-file hello1295Auto-merging hello.1296merge: warning: conflicts during merge1297ERROR: Merge conflict in hello.1298fatal: merge program failed1299------------13001301`git-merge-one-file` script is called with parameters to1302describe those three versions, and is responsible to leave the1303merge results in the working tree.1304It is a fairly straightforward shell script, and1305eventually calls `merge` program from RCS suite to perform a1306file-level 3-way merge. In this case, `merge` detects1307conflicts, and the merge result with conflict marks is left in1308the working tree.. This can be seen if you run `ls-files1309--stage` again at this point:13101311------------1312$ git-ls-files --stage1313100644 7f8b141b65fdcee47321e399a2598a235a032422 0 example1314100644 263414f423d0e4d70dae8fe53fa34614ff3e2860 1 hello1315100644 06fa6a24256dc7e560efa5687fa84b51f0263c3a 2 hello1316100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello1317------------13181319This is the state of the index file and the working file after1320`git merge` returns control back to you, leaving the conflicting1321merge for you to resolve. Notice that the path `hello` is still1322unmerged, and what you see with `git diff` at this point is1323differences since stage 2 (i.e. your version).132413251326Publishing your work1327--------------------13281329So we can use somebody else's work from a remote repository; but1330how can *you* prepare a repository to let other people pull from1331it?13321333Your do your real work in your working tree that has your1334primary repository hanging under it as its `.git` subdirectory.1335You *could* make that repository accessible remotely and ask1336people to pull from it, but in practice that is not the way1337things are usually done. A recommended way is to have a public1338repository, make it reachable by other people, and when the1339changes you made in your primary working tree are in good shape,1340update the public repository from it. This is often called1341'pushing'.13421343[NOTE]1344This public repository could further be mirrored, and that is1345how git repositories at `kernel.org` are managed.13461347Publishing the changes from your local (private) repository to1348your remote (public) repository requires a write privilege on1349the remote machine. You need to have an SSH account there to1350run a single command, `git-receive-pack`.13511352First, you need to create an empty repository on the remote1353machine that will house your public repository. This empty1354repository will be populated and be kept up-to-date by pushing1355into it later. Obviously, this repository creation needs to be1356done only once.13571358[NOTE]1359`git push` uses a pair of programs,1360`git-send-pack` on your local machine, and `git-receive-pack`1361on the remote machine. The communication between the two over1362the network internally uses an SSH connection.13631364Your private repository's git directory is usually `.git`, but1365your public repository is often named after the project name,1366i.e. `<project>.git`. Let's create such a public repository for1367project `my-git`. After logging into the remote machine, create1368an empty directory:13691370------------1371$ mkdir my-git.git1372------------13731374Then, make that directory into a git repository by running1375`git init-db`, but this time, since its name is not the usual1376`.git`, we do things slightly differently:13771378------------1379$ GIT_DIR=my-git.git git-init-db1380------------13811382Make sure this directory is available for others you want your1383changes to be pulled by via the transport of your choice. Also1384you need to make sure that you have the `git-receive-pack`1385program on the `$PATH`.13861387[NOTE]1388Many installations of sshd do not invoke your shell as the login1389shell when you directly run programs; what this means is that if1390your login shell is `bash`, only `.bashrc` is read and not1391`.bash_profile`. As a workaround, make sure `.bashrc` sets up1392`$PATH` so that you can run `git-receive-pack` program.13931394[NOTE]1395If you plan to publish this repository to be accessed over http,1396you should do `chmod +x my-git.git/hooks/post-update` at this1397point. This makes sure that every time you push into this1398repository, `git-update-server-info` is run.13991400Your "public repository" is now ready to accept your changes.1401Come back to the machine you have your private repository. From1402there, run this command:14031404------------1405$ git push <public-host>:/path/to/my-git.git master1406------------14071408This synchronizes your public repository to match the named1409branch head (i.e. `master` in this case) and objects reachable1410from them in your current repository.14111412As a real example, this is how I update my public git1413repository. Kernel.org mirror network takes care of the1414propagation to other publicly visible machines:14151416------------1417$ git push master.kernel.org:/pub/scm/git/git.git/ 1418------------141914201421Packing your repository1422-----------------------14231424Earlier, we saw that one file under `.git/objects/??/` directory1425is stored for each git object you create. This representation1426is efficient to create atomically and safely, but1427not so convenient to transport over the network. Since git objects are1428immutable once they are created, there is a way to optimize the1429storage by "packing them together". The command14301431------------1432$ git repack1433------------14341435will do it for you. If you followed the tutorial examples, you1436would have accumulated about 17 objects in `.git/objects/??/`1437directories by now. `git repack` tells you how many objects it1438packed, and stores the packed file in `.git/objects/pack`1439directory.14401441[NOTE]1442You will see two files, `pack-\*.pack` and `pack-\*.idx`,1443in `.git/objects/pack` directory. They are closely related to1444each other, and if you ever copy them by hand to a different1445repository for whatever reason, you should make sure you copy1446them together. The former holds all the data from the objects1447in the pack, and the latter holds the index for random1448access.14491450If you are paranoid, running `git-verify-pack` command would1451detect if you have a corrupt pack, but do not worry too much.1452Our programs are always perfect ;-).14531454Once you have packed objects, you do not need to leave the1455unpacked objects that are contained in the pack file anymore.14561457------------1458$ git prune-packed1459------------14601461would remove them for you.14621463You can try running `find .git/objects -type f` before and after1464you run `git prune-packed` if you are curious. Also `git1465count-objects` would tell you how many unpacked objects are in1466your repository and how much space they are consuming.14671468[NOTE]1469`git pull` is slightly cumbersome for HTTP transport, as a1470packed repository may contain relatively few objects in a1471relatively large pack. If you expect many HTTP pulls from your1472public repository you might want to repack & prune often, or1473never.14741475If you run `git repack` again at this point, it will say1476"Nothing to pack". Once you continue your development and1477accumulate the changes, running `git repack` again will create a1478new pack, that contains objects created since you packed your1479repository the last time. We recommend that you pack your project1480soon after the initial import (unless you are starting your1481project from scratch), and then run `git repack` every once in a1482while, depending on how active your project is.14831484When a repository is synchronized via `git push` and `git pull`1485objects packed in the source repository are usually stored1486unpacked in the destination, unless rsync transport is used.1487While this allows you to use different packing strategies on1488both ends, it also means you may need to repack both1489repositories every once in a while.149014911492Working with Others1493-------------------14941495Although git is a truly distributed system, it is often1496convenient to organize your project with an informal hierarchy1497of developers. Linux kernel development is run this way. There1498is a nice illustration (page 17, "Merges to Mainline") in Randy1499Dunlap's presentation (`http://tinyurl.com/a2jdg`).15001501It should be stressed that this hierarchy is purely *informal*.1502There is nothing fundamental in git that enforces the "chain of1503patch flow" this hierarchy implies. You do not have to pull1504from only one remote repository.15051506A recommended workflow for a "project lead" goes like this:150715081. Prepare your primary repository on your local machine. Your1509 work is done there.151015112. Prepare a public repository accessible to others.1512+1513If other people are pulling from your repository over dumb1514transport protocols, you need to keep this repository 'dumb1515transport friendly'. After `git init-db`,1516`$GIT_DIR/hooks/post-update` copied from the standard templates1517would contain a call to `git-update-server-info` but the1518`post-update` hook itself is disabled by default -- enable it1519with `chmod +x post-update`.152015213. Push into the public repository from your primary1522 repository.152315244. `git repack` the public repository. This establishes a big1525 pack that contains the initial set of objects as the1526 baseline, and possibly `git prune` if the transport1527 used for pulling from your repository supports packed1528 repositories.152915305. Keep working in your primary repository. Your changes1531 include modifications of your own, patches you receive via1532 e-mails, and merges resulting from pulling the "public"1533 repositories of your "subsystem maintainers".1534+1535You can repack this private repository whenever you feel like.153615376. Push your changes to the public repository, and announce it1538 to the public.153915407. Every once in a while, "git repack" the public repository.1541 Go back to step 5. and continue working.154215431544A recommended work cycle for a "subsystem maintainer" who works1545on that project and has an own "public repository" goes like this:154615471. Prepare your work repository, by `git clone` the public1548 repository of the "project lead". The URL used for the1549 initial cloning is stored in `.git/remotes/origin`.155015512. Prepare a public repository accessible to others, just like1552 the "project lead" person does.155315543. Copy over the packed files from "project lead" public1555 repository to your public repository, unless the "project1556 lead" repository lives on the same machine as yours. In the1557 latter case, you can use `objects/info/alternates` file to1558 point at the repository you are borrowing from.155915604. Push into the public repository from your primary1561 repository. Run `git repack`, and possibly `git prune` if the1562 transport used for pulling from your repository supports1563 packed repositories.156415655. Keep working in your primary repository. Your changes1566 include modifications of your own, patches you receive via1567 e-mails, and merges resulting from pulling the "public"1568 repositories of your "project lead" and possibly your1569 "sub-subsystem maintainers".1570+1571You can repack this private repository whenever you feel1572like.157315746. Push your changes to your public repository, and ask your1575 "project lead" and possibly your "sub-subsystem1576 maintainers" to pull from it.157715787. Every once in a while, `git repack` the public repository.1579 Go back to step 5. and continue working.158015811582A recommended work cycle for an "individual developer" who does1583not have a "public" repository is somewhat different. It goes1584like this:158515861. Prepare your work repository, by `git clone` the public1587 repository of the "project lead" (or a "subsystem1588 maintainer", if you work on a subsystem). The URL used for1589 the initial cloning is stored in `.git/remotes/origin`.159015912. Do your work in your repository on 'master' branch.159215933. Run `git fetch origin` from the public repository of your1594 upstream every once in a while. This does only the first1595 half of `git pull` but does not merge. The head of the1596 public repository is stored in `.git/refs/heads/origin`.159715984. Use `git cherry origin` to see which ones of your patches1599 were accepted, and/or use `git rebase origin` to port your1600 unmerged changes forward to the updated upstream.160116025. Use `git format-patch origin` to prepare patches for e-mail1603 submission to your upstream and send it out. Go back to1604 step 2. and continue.160516061607Working with Others, Shared Repository Style1608--------------------------------------------16091610If you are coming from CVS background, the style of cooperation1611suggested in the previous section may be new to you. You do not1612have to worry. git supports "shared public repository" style of1613cooperation you are probably more familiar with as well.16141615For this, set up a public repository on a machine that is1616reachable via SSH by people with "commit privileges". Put the1617committers in the same user group and make the repository1618writable by that group.16191620You, as an individual committer, then:16211622- First clone the shared repository to a local repository:1623------------------------------------------------1624$ git clone repo.shared.xz:/pub/scm/project.git/ my-project1625$ cd my-project1626$ hack away1627------------------------------------------------16281629- Merge the work others might have done while you were hacking1630 away:1631------------------------------------------------1632$ git pull origin1633$ test the merge result1634------------------------------------------------1635[NOTE]1636================================1637The first `git clone` would have placed the following in1638`my-project/.git/remotes/origin` file, and that's why this and1639the next step work.1640------------1641URL: repo.shared.xz:/pub/scm/project.git/ my-project1642Pull: master:origin1643------------1644================================16451646- push your work as the new head of the shared1647 repository.1648------------------------------------------------1649$ git push origin master1650------------------------------------------------1651If somebody else pushed into the same shared repository while1652you were working locally, `git push` in the last step would1653complain, telling you that the remote `master` head does not1654fast forward. You need to pull and merge those other changes1655back before you push your work when it happens.165616571658Advanced Shared Repository Management1659-------------------------------------16601661Being able to push into a shared repository means being able to1662write into it. If your developers are coming over the network,1663this means you, as the repository administrator, need to give1664each of them an SSH access to the shared repository machine.16651666In some cases, though, you may not want to give a normal shell1667account to them, but want to restrict them to be able to only1668do `git push` into the repository and nothing else.16691670You can achieve this by setting the login shell of your1671developers on the shared repository host to `git-shell` program.16721673[NOTE]1674Most likely you would also need to list `git-shell` program in1675`/etc/shells` file.16761677This restricts the set of commands that can be run from incoming1678SSH connection for these users to only `receive-pack` and1679`upload-pack`, so the only thing they can do are `git fetch` and1680`git push`.16811682You still need to create UNIX user accounts for each developer,1683and put them in the same group. Make sure that the repository1684shared among these developers is writable by that group.16851686You can implement finer grained branch policies using update1687hooks. There is a document ("control access to branches") in1688Documentation/howto by Carl Baldwin and JC outlining how to (1)1689limit access to branch per user, (2) forbid overwriting existing1690tags.169116921693Bundling your work together1694---------------------------16951696It is likely that you will be working on more than one thing at1697a time. It is easy to manage those more-or-less independent tasks1698using branches with git.16991700We have already seen how branches work previously,1701with "fun and work" example using two branches. The idea is the1702same if there are more than two branches. Let's say you started1703out from "master" head, and have some new code in the "master"1704branch, and two independent fixes in the "commit-fix" and1705"diff-fix" branches:17061707------------1708$ git show-branch1709! [commit-fix] Fix commit message normalization.1710 ! [diff-fix] Fix rename detection.1711 * [master] Release candidate #11712---1713 + [diff-fix] Fix rename detection.1714 + [diff-fix~1] Better common substring algorithm.1715+ [commit-fix] Fix commit message normalization.1716 + [master] Release candidate #11717+++ [diff-fix~2] Pretty-print messages.1718------------17191720Both fixes are tested well, and at this point, you want to merge1721in both of them. You could merge in 'diff-fix' first and then1722'commit-fix' next, like this:17231724------------1725$ git merge 'Merge fix in diff-fix' master diff-fix1726$ git merge 'Merge fix in commit-fix' master commit-fix1727------------17281729Which would result in:17301731------------1732$ git show-branch1733! [commit-fix] Fix commit message normalization.1734 ! [diff-fix] Fix rename detection.1735 * [master] Merge fix in commit-fix1736---1737 + [master] Merge fix in commit-fix1738+ + [commit-fix] Fix commit message normalization.1739 + [master~1] Merge fix in diff-fix1740 ++ [diff-fix] Fix rename detection.1741 ++ [diff-fix~1] Better common substring algorithm.1742 + [master~2] Release candidate #11743+++ [master~3] Pretty-print messages.1744------------17451746However, there is no particular reason to merge in one branch1747first and the other next, when what you have are a set of truly1748independent changes (if the order mattered, then they are not1749independent by definition). You could instead merge those two1750branches into the current branch at once. First let's undo what1751we just did and start over. We would want to get the master1752branch before these two merges by resetting it to 'master~2':17531754------------1755$ git reset --hard master~21756------------17571758You can make sure 'git show-branch' matches the state before1759those two 'git merge' you just did. Then, instead of running1760two 'git merge' commands in a row, you would pull these two1761branch heads (this is known as 'making an Octopus'):17621763------------1764$ git pull . commit-fix diff-fix1765$ git show-branch1766! [commit-fix] Fix commit message normalization.1767 ! [diff-fix] Fix rename detection.1768 * [master] Octopus merge of branches 'diff-fix' and 'commit-fix'1769---1770 + [master] Octopus merge of branches 'diff-fix' and 'commit-fix'1771+ + [commit-fix] Fix commit message normalization.1772 ++ [diff-fix] Fix rename detection.1773 ++ [diff-fix~1] Better common substring algorithm.1774 + [master~1] Release candidate #11775+++ [master~2] Pretty-print messages.1776------------17771778Note that you should not do Octopus because you can. An octopus1779is a valid thing to do and often makes it easier to view the1780commit history if you are pulling more than two independent1781changes at the same time. However, if you have merge conflicts1782with any of the branches you are merging in and need to hand1783resolve, that is an indication that the development happened in1784those branches were not independent after all, and you should1785merge two at a time, documenting how you resolved the conflicts,1786and the reason why you preferred changes made in one side over1787the other. Otherwise it would make the project history harder1788to follow, not easier.17891790[ to be continued.. cvsimports ]