1Git User's Manual (for version 1.5.1 or newer) 2______________________________________________ 3 4This manual is designed to be readable by someone with basic unix 5command-line skills, but no previous knowledge of git. 6 7<<repositories-and-branches>> and <<exploring-git-history>> explain how 8to fetch and study a project using git--read these chapters to learn how 9to build and test a particular version of a software project, search for 10regressions, and so on. 11 12People needing to do actual development will also want to read 13<<Developing-with-git>> and <<sharing-development>>. 14 15Further chapters cover more specialized topics. 16 17Comprehensive reference documentation is available through the man 18pages. For a command such as "git clone", just use 19 20------------------------------------------------ 21$ man git-clone 22------------------------------------------------ 23 24See also <<git-quick-start>> for a brief overview of git commands, 25without any explanation. 26 27Also, see <<todo>> for ways that you can help make this manual more 28complete. 29 30 31[[repositories-and-branches]] 32Repositories and Branches 33========================= 34 35[[how-to-get-a-git-repository]] 36How to get a git repository 37--------------------------- 38 39It will be useful to have a git repository to experiment with as you 40read this manual. 41 42The best way to get one is by using the gitlink:git-clone[1] command 43to download a copy of an existing repository for a project that you 44are interested in. If you don't already have a project in mind, here 45are some interesting examples: 46 47------------------------------------------------ 48 # git itself (approx. 10MB download): 49$ git clone git://git.kernel.org/pub/scm/git/git.git 50 # the linux kernel (approx. 150MB download): 51$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git 52------------------------------------------------ 53 54The initial clone may be time-consuming for a large project, but you 55will only need to clone once. 56 57The clone command creates a new directory named after the project 58("git" or "linux-2.6" in the examples above). After you cd into this 59directory, you will see that it contains a copy of the project files, 60together with a special top-level directory named ".git", which 61contains all the information about the history of the project. 62 63In most of the following, examples will be taken from one of the two 64repositories above. 65 66[[how-to-check-out]] 67How to check out a different version of a project 68------------------------------------------------- 69 70Git is best thought of as a tool for storing the history of a 71collection of files. It stores the history as a compressed 72collection of interrelated snapshots (versions) of the project's 73contents. 74 75A single git repository may contain multiple branches. It keeps track 76of them by keeping a list of <<def_head,heads>> which reference the 77latest version on each branch; the gitlink:git-branch[1] command shows 78you the list of branch heads: 79 80------------------------------------------------ 81$ git branch 82* master 83------------------------------------------------ 84 85A freshly cloned repository contains a single branch head, by default 86named "master", with the working directory initialized to the state of 87the project referred to by that branch head. 88 89Most projects also use <<def_tag,tags>>. Tags, like heads, are 90references into the project's history, and can be listed using the 91gitlink:git-tag[1] command: 92 93------------------------------------------------ 94$ git tag -l 95v2.6.11 96v2.6.11-tree 97v2.6.12 98v2.6.12-rc2 99v2.6.12-rc3 100v2.6.12-rc4 101v2.6.12-rc5 102v2.6.12-rc6 103v2.6.13 104... 105------------------------------------------------ 106 107Tags are expected to always point at the same version of a project, 108while heads are expected to advance as development progresses. 109 110Create a new branch head pointing to one of these versions and check it 111out using gitlink:git-checkout[1]: 112 113------------------------------------------------ 114$ git checkout -b new v2.6.13 115------------------------------------------------ 116 117The working directory then reflects the contents that the project had 118when it was tagged v2.6.13, and gitlink:git-branch[1] shows two 119branches, with an asterisk marking the currently checked-out branch: 120 121------------------------------------------------ 122$ git branch 123 master 124* new 125------------------------------------------------ 126 127If you decide that you'd rather see version 2.6.17, you can modify 128the current branch to point at v2.6.17 instead, with 129 130------------------------------------------------ 131$ git reset --hard v2.6.17 132------------------------------------------------ 133 134Note that if the current branch head was your only reference to a 135particular point in history, then resetting that branch may leave you 136with no way to find the history it used to point to; so use this command 137carefully. 138 139[[understanding-commits]] 140Understanding History: Commits 141------------------------------ 142 143Every change in the history of a project is represented by a commit. 144The gitlink:git-show[1] command shows the most recent commit on the 145current branch: 146 147------------------------------------------------ 148$ git show 149commit 2b5f6dcce5bf94b9b119e9ed8d537098ec61c3d2 150Author: Jamal Hadi Salim <hadi@cyberus.ca> 151Date: Sat Dec 2 22:22:25 2006 -0800 152 153 [XFRM]: Fix aevent structuring to be more complete. 154 155 aevents can not uniquely identify an SA. We break the ABI with this 156 patch, but consensus is that since it is not yet utilized by any 157 (known) application then it is fine (better do it now than later). 158 159 Signed-off-by: Jamal Hadi Salim <hadi@cyberus.ca> 160 Signed-off-by: David S. Miller <davem@davemloft.net> 161 162diff --git a/Documentation/networking/xfrm_sync.txt b/Documentation/networking/xfrm_sync.txt 163index 8be626f..d7aac9d 100644 164--- a/Documentation/networking/xfrm_sync.txt 165+++ b/Documentation/networking/xfrm_sync.txt 166@@ -47,10 +47,13 @@ aevent_id structure looks like: 167 168 struct xfrm_aevent_id { 169 struct xfrm_usersa_id sa_id; 170+ xfrm_address_t saddr; 171 __u32 flags; 172+ __u32 reqid; 173 }; 174... 175------------------------------------------------ 176 177As you can see, a commit shows who made the latest change, what they 178did, and why. 179 180Every commit has a 40-hexdigit id, sometimes called the "object name" or the 181"SHA1 id", shown on the first line of the "git show" output. You can usually 182refer to a commit by a shorter name, such as a tag or a branch name, but this 183longer name can also be useful. Most importantly, it is a globally unique 184name for this commit: so if you tell somebody else the object name (for 185example in email), then you are guaranteed that name will refer to the same 186commit in their repository that it does in yours (assuming their repository 187has that commit at all). Since the object name is computed as a hash over the 188contents of the commit, you are guaranteed that the commit can never change 189without its name also changing. 190 191In fact, in <<git-internals>> we shall see that everything stored in git 192history, including file data and directory contents, is stored in an object 193with a name that is a hash of its contents. 194 195[[understanding-reachability]] 196Understanding history: commits, parents, and reachability 197~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 198 199Every commit (except the very first commit in a project) also has a 200parent commit which shows what happened before this commit. 201Following the chain of parents will eventually take you back to the 202beginning of the project. 203 204However, the commits do not form a simple list; git allows lines of 205development to diverge and then reconverge, and the point where two 206lines of development reconverge is called a "merge". The commit 207representing a merge can therefore have more than one parent, with 208each parent representing the most recent commit on one of the lines 209of development leading to that point. 210 211The best way to see how this works is using the gitlink:gitk[1] 212command; running gitk now on a git repository and looking for merge 213commits will help understand how the git organizes history. 214 215In the following, we say that commit X is "reachable" from commit Y 216if commit X is an ancestor of commit Y. Equivalently, you could say 217that Y is a descendent of X, or that there is a chain of parents 218leading from commit Y to commit X. 219 220[[history-diagrams]] 221Understanding history: History diagrams 222~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 223 224We will sometimes represent git history using diagrams like the one 225below. Commits are shown as "o", and the links between them with 226lines drawn with - / and \. Time goes left to right: 227 228 229................................................ 230 o--o--o <-- Branch A 231 / 232 o--o--o <-- master 233 \ 234 o--o--o <-- Branch B 235................................................ 236 237If we need to talk about a particular commit, the character "o" may 238be replaced with another letter or number. 239 240[[what-is-a-branch]] 241Understanding history: What is a branch? 242~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 243 244When we need to be precise, we will use the word "branch" to mean a line 245of development, and "branch head" (or just "head") to mean a reference 246to the most recent commit on a branch. In the example above, the branch 247head named "A" is a pointer to one particular commit, but we refer to 248the line of three commits leading up to that point as all being part of 249"branch A". 250 251However, when no confusion will result, we often just use the term 252"branch" both for branches and for branch heads. 253 254[[manipulating-branches]] 255Manipulating branches 256--------------------- 257 258Creating, deleting, and modifying branches is quick and easy; here's 259a summary of the commands: 260 261git branch:: 262 list all branches 263git branch <branch>:: 264 create a new branch named <branch>, referencing the same 265 point in history as the current branch 266git branch <branch> <start-point>:: 267 create a new branch named <branch>, referencing 268 <start-point>, which may be specified any way you like, 269 including using a branch name or a tag name 270git branch -d <branch>:: 271 delete the branch <branch>; if the branch you are deleting 272 points to a commit which is not reachable from the current 273 branch, this command will fail with a warning. 274git branch -D <branch>:: 275 even if the branch points to a commit not reachable 276 from the current branch, you may know that that commit 277 is still reachable from some other branch or tag. In that 278 case it is safe to use this command to force git to delete 279 the branch. 280git checkout <branch>:: 281 make the current branch <branch>, updating the working 282 directory to reflect the version referenced by <branch> 283git checkout -b <new> <start-point>:: 284 create a new branch <new> referencing <start-point>, and 285 check it out. 286 287The special symbol "HEAD" can always be used to refer to the current 288branch. In fact, git uses a file named "HEAD" in the .git directory to 289remember which branch is current: 290 291------------------------------------------------ 292$ cat .git/HEAD 293ref: refs/heads/master 294------------------------------------------------ 295 296[[detached-head]] 297Examining an old version without creating a new branch 298------------------------------------------------------ 299 300The git-checkout command normally expects a branch head, but will also 301accept an arbitrary commit; for example, you can check out the commit 302referenced by a tag: 303 304------------------------------------------------ 305$ git checkout v2.6.17 306Note: moving to "v2.6.17" which isn't a local branch 307If you want to create a new branch from this checkout, you may do so 308(now or later) by using -b with the checkout command again. Example: 309 git checkout -b <new_branch_name> 310HEAD is now at 427abfa... Linux v2.6.17 311------------------------------------------------ 312 313The HEAD then refers to the SHA1 of the commit instead of to a branch, 314and git branch shows that you are no longer on a branch: 315 316------------------------------------------------ 317$ cat .git/HEAD 318427abfa28afedffadfca9dd8b067eb6d36bac53f 319$ git branch 320* (no branch) 321 master 322------------------------------------------------ 323 324In this case we say that the HEAD is "detached". 325 326This is an easy way to check out a particular version without having to 327make up a name for the new branch. You can still create a new branch 328(or tag) for this version later if you decide to. 329 330[[examining-remote-branches]] 331Examining branches from a remote repository 332------------------------------------------- 333 334The "master" branch that was created at the time you cloned is a copy 335of the HEAD in the repository that you cloned from. That repository 336may also have had other branches, though, and your local repository 337keeps branches which track each of those remote branches, which you 338can view using the "-r" option to gitlink:git-branch[1]: 339 340------------------------------------------------ 341$ git branch -r 342 origin/HEAD 343 origin/html 344 origin/maint 345 origin/man 346 origin/master 347 origin/next 348 origin/pu 349 origin/todo 350------------------------------------------------ 351 352You cannot check out these remote-tracking branches, but you can 353examine them on a branch of your own, just as you would a tag: 354 355------------------------------------------------ 356$ git checkout -b my-todo-copy origin/todo 357------------------------------------------------ 358 359Note that the name "origin" is just the name that git uses by default 360to refer to the repository that you cloned from. 361 362[[how-git-stores-references]] 363Naming branches, tags, and other references 364------------------------------------------- 365 366Branches, remote-tracking branches, and tags are all references to 367commits. All references are named with a slash-separated path name 368starting with "refs"; the names we've been using so far are actually 369shorthand: 370 371 - The branch "test" is short for "refs/heads/test". 372 - The tag "v2.6.18" is short for "refs/tags/v2.6.18". 373 - "origin/master" is short for "refs/remotes/origin/master". 374 375The full name is occasionally useful if, for example, there ever 376exists a tag and a branch with the same name. 377 378As another useful shortcut, the "HEAD" of a repository can be referred 379to just using the name of that repository. So, for example, "origin" 380is usually a shortcut for the HEAD branch in the repository "origin". 381 382For the complete list of paths which git checks for references, and 383the order it uses to decide which to choose when there are multiple 384references with the same shorthand name, see the "SPECIFYING 385REVISIONS" section of gitlink:git-rev-parse[1]. 386 387[[Updating-a-repository-with-git-fetch]] 388Updating a repository with git fetch 389------------------------------------ 390 391Eventually the developer cloned from will do additional work in her 392repository, creating new commits and advancing the branches to point 393at the new commits. 394 395The command "git fetch", with no arguments, will update all of the 396remote-tracking branches to the latest version found in her 397repository. It will not touch any of your own branches--not even the 398"master" branch that was created for you on clone. 399 400[[fetching-branches]] 401Fetching branches from other repositories 402----------------------------------------- 403 404You can also track branches from repositories other than the one you 405cloned from, using gitlink:git-remote[1]: 406 407------------------------------------------------- 408$ git remote add linux-nfs git://linux-nfs.org/pub/nfs-2.6.git 409$ git fetch linux-nfs 410* refs/remotes/linux-nfs/master: storing branch 'master' ... 411 commit: bf81b46 412------------------------------------------------- 413 414New remote-tracking branches will be stored under the shorthand name 415that you gave "git remote add", in this case linux-nfs: 416 417------------------------------------------------- 418$ git branch -r 419linux-nfs/master 420origin/master 421------------------------------------------------- 422 423If you run "git fetch <remote>" later, the tracking branches for the 424named <remote> will be updated. 425 426If you examine the file .git/config, you will see that git has added 427a new stanza: 428 429------------------------------------------------- 430$ cat .git/config 431... 432[remote "linux-nfs"] 433 url = git://linux-nfs.org/pub/nfs-2.6.git 434 fetch = +refs/heads/*:refs/remotes/linux-nfs/* 435... 436------------------------------------------------- 437 438This is what causes git to track the remote's branches; you may modify 439or delete these configuration options by editing .git/config with a 440text editor. (See the "CONFIGURATION FILE" section of 441gitlink:git-config[1] for details.) 442 443[[exploring-git-history]] 444Exploring git history 445===================== 446 447Git is best thought of as a tool for storing the history of a 448collection of files. It does this by storing compressed snapshots of 449the contents of a file heirarchy, together with "commits" which show 450the relationships between these snapshots. 451 452Git provides extremely flexible and fast tools for exploring the 453history of a project. 454 455We start with one specialized tool that is useful for finding the 456commit that introduced a bug into a project. 457 458[[using-bisect]] 459How to use bisect to find a regression 460-------------------------------------- 461 462Suppose version 2.6.18 of your project worked, but the version at 463"master" crashes. Sometimes the best way to find the cause of such a 464regression is to perform a brute-force search through the project's 465history to find the particular commit that caused the problem. The 466gitlink:git-bisect[1] command can help you do this: 467 468------------------------------------------------- 469$ git bisect start 470$ git bisect good v2.6.18 471$ git bisect bad master 472Bisecting: 3537 revisions left to test after this 473[65934a9a028b88e83e2b0f8b36618fe503349f8e] BLOCK: Make USB storage depend on SCSI rather than selecting it [try #6] 474------------------------------------------------- 475 476If you run "git branch" at this point, you'll see that git has 477temporarily moved you to a new branch named "bisect". This branch 478points to a commit (with commit id 65934...) that is reachable from 479v2.6.19 but not from v2.6.18. Compile and test it, and see whether 480it crashes. Assume it does crash. Then: 481 482------------------------------------------------- 483$ git bisect bad 484Bisecting: 1769 revisions left to test after this 485[7eff82c8b1511017ae605f0c99ac275a7e21b867] i2c-core: Drop useless bitmaskings 486------------------------------------------------- 487 488checks out an older version. Continue like this, telling git at each 489stage whether the version it gives you is good or bad, and notice 490that the number of revisions left to test is cut approximately in 491half each time. 492 493After about 13 tests (in this case), it will output the commit id of 494the guilty commit. You can then examine the commit with 495gitlink:git-show[1], find out who wrote it, and mail them your bug 496report with the commit id. Finally, run 497 498------------------------------------------------- 499$ git bisect reset 500------------------------------------------------- 501 502to return you to the branch you were on before and delete the 503temporary "bisect" branch. 504 505Note that the version which git-bisect checks out for you at each 506point is just a suggestion, and you're free to try a different 507version if you think it would be a good idea. For example, 508occasionally you may land on a commit that broke something unrelated; 509run 510 511------------------------------------------------- 512$ git bisect visualize 513------------------------------------------------- 514 515which will run gitk and label the commit it chose with a marker that 516says "bisect". Chose a safe-looking commit nearby, note its commit 517id, and check it out with: 518 519------------------------------------------------- 520$ git reset --hard fb47ddb2db... 521------------------------------------------------- 522 523then test, run "bisect good" or "bisect bad" as appropriate, and 524continue. 525 526[[naming-commits]] 527Naming commits 528-------------- 529 530We have seen several ways of naming commits already: 531 532 - 40-hexdigit object name 533 - branch name: refers to the commit at the head of the given 534 branch 535 - tag name: refers to the commit pointed to by the given tag 536 (we've seen branches and tags are special cases of 537 <<how-git-stores-references,references>>). 538 - HEAD: refers to the head of the current branch 539 540There are many more; see the "SPECIFYING REVISIONS" section of the 541gitlink:git-rev-parse[1] man page for the complete list of ways to 542name revisions. Some examples: 543 544------------------------------------------------- 545$ git show fb47ddb2 # the first few characters of the object name 546 # are usually enough to specify it uniquely 547$ git show HEAD^ # the parent of the HEAD commit 548$ git show HEAD^^ # the grandparent 549$ git show HEAD~4 # the great-great-grandparent 550------------------------------------------------- 551 552Recall that merge commits may have more than one parent; by default, 553^ and ~ follow the first parent listed in the commit, but you can 554also choose: 555 556------------------------------------------------- 557$ git show HEAD^1 # show the first parent of HEAD 558$ git show HEAD^2 # show the second parent of HEAD 559------------------------------------------------- 560 561In addition to HEAD, there are several other special names for 562commits: 563 564Merges (to be discussed later), as well as operations such as 565git-reset, which change the currently checked-out commit, generally 566set ORIG_HEAD to the value HEAD had before the current operation. 567 568The git-fetch operation always stores the head of the last fetched 569branch in FETCH_HEAD. For example, if you run git fetch without 570specifying a local branch as the target of the operation 571 572------------------------------------------------- 573$ git fetch git://example.com/proj.git theirbranch 574------------------------------------------------- 575 576the fetched commits will still be available from FETCH_HEAD. 577 578When we discuss merges we'll also see the special name MERGE_HEAD, 579which refers to the other branch that we're merging in to the current 580branch. 581 582The gitlink:git-rev-parse[1] command is a low-level command that is 583occasionally useful for translating some name for a commit to the object 584name for that commit: 585 586------------------------------------------------- 587$ git rev-parse origin 588e05db0fd4f31dde7005f075a84f96b360d05984b 589------------------------------------------------- 590 591[[creating-tags]] 592Creating tags 593------------- 594 595We can also create a tag to refer to a particular commit; after 596running 597 598------------------------------------------------- 599$ git tag stable-1 1b2e1d63ff 600------------------------------------------------- 601 602You can use stable-1 to refer to the commit 1b2e1d63ff. 603 604This creates a "lightweight" tag. If you would also like to include a 605comment with the tag, and possibly sign it cryptographically, then you 606should create a tag object instead; see the gitlink:git-tag[1] man page 607for details. 608 609[[browsing-revisions]] 610Browsing revisions 611------------------ 612 613The gitlink:git-log[1] command can show lists of commits. On its 614own, it shows all commits reachable from the parent commit; but you 615can also make more specific requests: 616 617------------------------------------------------- 618$ git log v2.5.. # commits since (not reachable from) v2.5 619$ git log test..master # commits reachable from master but not test 620$ git log master..test # ...reachable from test but not master 621$ git log master...test # ...reachable from either test or master, 622 # but not both 623$ git log --since="2 weeks ago" # commits from the last 2 weeks 624$ git log Makefile # commits which modify Makefile 625$ git log fs/ # ... which modify any file under fs/ 626$ git log -S'foo()' # commits which add or remove any file data 627 # matching the string 'foo()' 628------------------------------------------------- 629 630And of course you can combine all of these; the following finds 631commits since v2.5 which touch the Makefile or any file under fs: 632 633------------------------------------------------- 634$ git log v2.5.. Makefile fs/ 635------------------------------------------------- 636 637You can also ask git log to show patches: 638 639------------------------------------------------- 640$ git log -p 641------------------------------------------------- 642 643See the "--pretty" option in the gitlink:git-log[1] man page for more 644display options. 645 646Note that git log starts with the most recent commit and works 647backwards through the parents; however, since git history can contain 648multiple independent lines of development, the particular order that 649commits are listed in may be somewhat arbitrary. 650 651[[generating-diffs]] 652Generating diffs 653---------------- 654 655You can generate diffs between any two versions using 656gitlink:git-diff[1]: 657 658------------------------------------------------- 659$ git diff master..test 660------------------------------------------------- 661 662Sometimes what you want instead is a set of patches: 663 664------------------------------------------------- 665$ git format-patch master..test 666------------------------------------------------- 667 668will generate a file with a patch for each commit reachable from test 669but not from master. Note that if master also has commits which are 670not reachable from test, then the combined result of these patches 671will not be the same as the diff produced by the git-diff example. 672 673[[viewing-old-file-versions]] 674Viewing old file versions 675------------------------- 676 677You can always view an old version of a file by just checking out the 678correct revision first. But sometimes it is more convenient to be 679able to view an old version of a single file without checking 680anything out; this command does that: 681 682------------------------------------------------- 683$ git show v2.5:fs/locks.c 684------------------------------------------------- 685 686Before the colon may be anything that names a commit, and after it 687may be any path to a file tracked by git. 688 689[[history-examples]] 690Examples 691-------- 692 693[[checking-for-equal-branches]] 694Check whether two branches point at the same history 695~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 696 697Suppose you want to check whether two branches point at the same point 698in history. 699 700------------------------------------------------- 701$ git diff origin..master 702------------------------------------------------- 703 704will tell you whether the contents of the project are the same at the 705two branches; in theory, however, it's possible that the same project 706contents could have been arrived at by two different historical 707routes. You could compare the object names: 708 709------------------------------------------------- 710$ git rev-list origin 711e05db0fd4f31dde7005f075a84f96b360d05984b 712$ git rev-list master 713e05db0fd4f31dde7005f075a84f96b360d05984b 714------------------------------------------------- 715 716Or you could recall that the ... operator selects all commits 717contained reachable from either one reference or the other but not 718both: so 719 720------------------------------------------------- 721$ git log origin...master 722------------------------------------------------- 723 724will return no commits when the two branches are equal. 725 726[[finding-tagged-descendants]] 727Find first tagged version including a given fix 728~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 729 730Suppose you know that the commit e05db0fd fixed a certain problem. 731You'd like to find the earliest tagged release that contains that 732fix. 733 734Of course, there may be more than one answer--if the history branched 735after commit e05db0fd, then there could be multiple "earliest" tagged 736releases. 737 738You could just visually inspect the commits since e05db0fd: 739 740------------------------------------------------- 741$ gitk e05db0fd.. 742------------------------------------------------- 743 744Or you can use gitlink:git-name-rev[1], which will give the commit a 745name based on any tag it finds pointing to one of the commit's 746descendants: 747 748------------------------------------------------- 749$ git name-rev --tags e05db0fd 750e05db0fd tags/v1.5.0-rc1^0~23 751------------------------------------------------- 752 753The gitlink:git-describe[1] command does the opposite, naming the 754revision using a tag on which the given commit is based: 755 756------------------------------------------------- 757$ git describe e05db0fd 758v1.5.0-rc0-260-ge05db0f 759------------------------------------------------- 760 761but that may sometimes help you guess which tags might come after the 762given commit. 763 764If you just want to verify whether a given tagged version contains a 765given commit, you could use gitlink:git-merge-base[1]: 766 767------------------------------------------------- 768$ git merge-base e05db0fd v1.5.0-rc1 769e05db0fd4f31dde7005f075a84f96b360d05984b 770------------------------------------------------- 771 772The merge-base command finds a common ancestor of the given commits, 773and always returns one or the other in the case where one is a 774descendant of the other; so the above output shows that e05db0fd 775actually is an ancestor of v1.5.0-rc1. 776 777Alternatively, note that 778 779------------------------------------------------- 780$ git log v1.5.0-rc1..e05db0fd 781------------------------------------------------- 782 783will produce empty output if and only if v1.5.0-rc1 includes e05db0fd, 784because it outputs only commits that are not reachable from v1.5.0-rc1. 785 786As yet another alternative, the gitlink:git-show-branch[1] command lists 787the commits reachable from its arguments with a display on the left-hand 788side that indicates which arguments that commit is reachable from. So, 789you can run something like 790 791------------------------------------------------- 792$ git show-branch e05db0fd v1.5.0-rc0 v1.5.0-rc1 v1.5.0-rc2 793! [e05db0fd] Fix warnings in sha1_file.c - use C99 printf format if 794available 795 ! [v1.5.0-rc0] GIT v1.5.0 preview 796 ! [v1.5.0-rc1] GIT v1.5.0-rc1 797 ! [v1.5.0-rc2] GIT v1.5.0-rc2 798... 799------------------------------------------------- 800 801then search for a line that looks like 802 803------------------------------------------------- 804+ ++ [e05db0fd] Fix warnings in sha1_file.c - use C99 printf format if 805available 806------------------------------------------------- 807 808Which shows that e05db0fd is reachable from itself, from v1.5.0-rc1, and 809from v1.5.0-rc2, but not from v1.5.0-rc0. 810 811 812[[Developing-with-git]] 813Developing with git 814=================== 815 816[[telling-git-your-name]] 817Telling git your name 818--------------------- 819 820Before creating any commits, you should introduce yourself to git. The 821easiest way to do so is to make sure the following lines appear in a 822file named .gitconfig in your home directory: 823 824------------------------------------------------ 825[user] 826 name = Your Name Comes Here 827 email = you@yourdomain.example.com 828------------------------------------------------ 829 830(See the "CONFIGURATION FILE" section of gitlink:git-config[1] for 831details on the configuration file.) 832 833 834[[creating-a-new-repository]] 835Creating a new repository 836------------------------- 837 838Creating a new repository from scratch is very easy: 839 840------------------------------------------------- 841$ mkdir project 842$ cd project 843$ git init 844------------------------------------------------- 845 846If you have some initial content (say, a tarball): 847 848------------------------------------------------- 849$ tar -xzvf project.tar.gz 850$ cd project 851$ git init 852$ git add . # include everything below ./ in the first commit: 853$ git commit 854------------------------------------------------- 855 856[[how-to-make-a-commit]] 857How to make a commit 858-------------------- 859 860Creating a new commit takes three steps: 861 862 1. Making some changes to the working directory using your 863 favorite editor. 864 2. Telling git about your changes. 865 3. Creating the commit using the content you told git about 866 in step 2. 867 868In practice, you can interleave and repeat steps 1 and 2 as many 869times as you want: in order to keep track of what you want committed 870at step 3, git maintains a snapshot of the tree's contents in a 871special staging area called "the index." 872 873At the beginning, the content of the index will be identical to 874that of the HEAD. The command "git diff --cached", which shows 875the difference between the HEAD and the index, should therefore 876produce no output at that point. 877 878Modifying the index is easy: 879 880To update the index with the new contents of a modified file, use 881 882------------------------------------------------- 883$ git add path/to/file 884------------------------------------------------- 885 886To add the contents of a new file to the index, use 887 888------------------------------------------------- 889$ git add path/to/file 890------------------------------------------------- 891 892To remove a file from the index and from the working tree, 893 894------------------------------------------------- 895$ git rm path/to/file 896------------------------------------------------- 897 898After each step you can verify that 899 900------------------------------------------------- 901$ git diff --cached 902------------------------------------------------- 903 904always shows the difference between the HEAD and the index file--this 905is what you'd commit if you created the commit now--and that 906 907------------------------------------------------- 908$ git diff 909------------------------------------------------- 910 911shows the difference between the working tree and the index file. 912 913Note that "git add" always adds just the current contents of a file 914to the index; further changes to the same file will be ignored unless 915you run git-add on the file again. 916 917When you're ready, just run 918 919------------------------------------------------- 920$ git commit 921------------------------------------------------- 922 923and git will prompt you for a commit message and then create the new 924commit. Check to make sure it looks like what you expected with 925 926------------------------------------------------- 927$ git show 928------------------------------------------------- 929 930As a special shortcut, 931 932------------------------------------------------- 933$ git commit -a 934------------------------------------------------- 935 936will update the index with any files that you've modified or removed 937and create a commit, all in one step. 938 939A number of commands are useful for keeping track of what you're 940about to commit: 941 942------------------------------------------------- 943$ git diff --cached # difference between HEAD and the index; what 944 # would be commited if you ran "commit" now. 945$ git diff # difference between the index file and your 946 # working directory; changes that would not 947 # be included if you ran "commit" now. 948$ git diff HEAD # difference between HEAD and working tree; what 949 # would be committed if you ran "commit -a" now. 950$ git status # a brief per-file summary of the above. 951------------------------------------------------- 952 953[[creating-good-commit-messages]] 954Creating good commit messages 955----------------------------- 956 957Though not required, it's a good idea to begin the commit message 958with a single short (less than 50 character) line summarizing the 959change, followed by a blank line and then a more thorough 960description. Tools that turn commits into email, for example, use 961the first line on the Subject line and the rest of the commit in the 962body. 963 964[[how-to-merge]] 965How to merge 966------------ 967 968You can rejoin two diverging branches of development using 969gitlink:git-merge[1]: 970 971------------------------------------------------- 972$ git merge branchname 973------------------------------------------------- 974 975merges the development in the branch "branchname" into the current 976branch. If there are conflicts--for example, if the same file is 977modified in two different ways in the remote branch and the local 978branch--then you are warned; the output may look something like this: 979 980------------------------------------------------- 981$ git merge next 982 100% (4/4) done 983Auto-merged file.txt 984CONFLICT (content): Merge conflict in file.txt 985Automatic merge failed; fix conflicts and then commit the result. 986------------------------------------------------- 987 988Conflict markers are left in the problematic files, and after 989you resolve the conflicts manually, you can update the index 990with the contents and run git commit, as you normally would when 991creating a new file. 992 993If you examine the resulting commit using gitk, you will see that it 994has two parents, one pointing to the top of the current branch, and 995one to the top of the other branch. 996 997[[resolving-a-merge]] 998Resolving a merge 999-----------------10001001When a merge isn't resolved automatically, git leaves the index and1002the working tree in a special state that gives you all the1003information you need to help resolve the merge.10041005Files with conflicts are marked specially in the index, so until you1006resolve the problem and update the index, gitlink:git-commit[1] will1007fail:10081009-------------------------------------------------1010$ git commit1011file.txt: needs merge1012-------------------------------------------------10131014Also, gitlink:git-status[1] will list those files as "unmerged", and the1015files with conflicts will have conflict markers added, like this:10161017-------------------------------------------------1018<<<<<<< HEAD:file.txt1019Hello world1020=======1021Goodbye1022>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt1023-------------------------------------------------10241025All you need to do is edit the files to resolve the conflicts, and then10261027-------------------------------------------------1028$ git add file.txt1029$ git commit1030-------------------------------------------------10311032Note that the commit message will already be filled in for you with1033some information about the merge. Normally you can just use this1034default message unchanged, but you may add additional commentary of1035your own if desired.10361037The above is all you need to know to resolve a simple merge. But git1038also provides more information to help resolve conflicts:10391040[[conflict-resolution]]1041Getting conflict-resolution help during a merge1042~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~10431044All of the changes that git was able to merge automatically are1045already added to the index file, so gitlink:git-diff[1] shows only1046the conflicts. It uses an unusual syntax:10471048-------------------------------------------------1049$ git diff1050diff --cc file.txt1051index 802992c,2b60207..00000001052--- a/file.txt1053+++ b/file.txt1054@@@ -1,1 -1,1 +1,5 @@@1055++<<<<<<< HEAD:file.txt1056 +Hello world1057++=======1058+ Goodbye1059++>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt1060-------------------------------------------------10611062Recall that the commit which will be commited after we resolve this1063conflict will have two parents instead of the usual one: one parent1064will be HEAD, the tip of the current branch; the other will be the1065tip of the other branch, which is stored temporarily in MERGE_HEAD.10661067During the merge, the index holds three versions of each file. Each of1068these three "file stages" represents a different version of the file:10691070-------------------------------------------------1071$ git show :1:file.txt # the file in a common ancestor of both branches1072$ git show :2:file.txt # the version from HEAD, but including any1073 # nonconflicting changes from MERGE_HEAD1074$ git show :3:file.txt # the version from MERGE_HEAD, but including any1075 # nonconflicting changes from HEAD.1076-------------------------------------------------10771078Since the stage 2 and stage 3 versions have already been updated with1079nonconflicting changes, the only remaining differences between them are1080the important ones; thus gitlink:git-diff[1] can use the information in1081the index to show only those conflicts.10821083The diff above shows the differences between the working-tree version of1084file.txt and the stage 2 and stage 3 versions. So instead of preceding1085each line by a single "+" or "-", it now uses two columns: the first1086column is used for differences between the first parent and the working1087directory copy, and the second for differences between the second parent1088and the working directory copy. (See the "COMBINED DIFF FORMAT" section1089of gitlink:git-diff-files[1] for a details of the format.)10901091After resolving the conflict in the obvious way (but before updating the1092index), the diff will look like:10931094-------------------------------------------------1095$ git diff1096diff --cc file.txt1097index 802992c,2b60207..00000001098--- a/file.txt1099+++ b/file.txt1100@@@ -1,1 -1,1 +1,1 @@@1101- Hello world1102 -Goodbye1103++Goodbye world1104-------------------------------------------------11051106This shows that our resolved version deleted "Hello world" from the1107first parent, deleted "Goodbye" from the second parent, and added1108"Goodbye world", which was previously absent from both.11091110Some special diff options allow diffing the working directory against1111any of these stages:11121113-------------------------------------------------1114$ git diff -1 file.txt # diff against stage 11115$ git diff --base file.txt # same as the above1116$ git diff -2 file.txt # diff against stage 21117$ git diff --ours file.txt # same as the above1118$ git diff -3 file.txt # diff against stage 31119$ git diff --theirs file.txt # same as the above.1120-------------------------------------------------11211122The gitlink:git-log[1] and gitk[1] commands also provide special help1123for merges:11241125-------------------------------------------------1126$ git log --merge1127$ gitk --merge1128-------------------------------------------------11291130These will display all commits which exist only on HEAD or on1131MERGE_HEAD, and which touch an unmerged file.11321133You may also use gitlink:git-mergetool[1], which lets you merge the1134unmerged files using external tools such as emacs or kdiff3.11351136Each time you resolve the conflicts in a file and update the index:11371138-------------------------------------------------1139$ git add file.txt1140-------------------------------------------------11411142the different stages of that file will be "collapsed", after which1143git-diff will (by default) no longer show diffs for that file.11441145[[undoing-a-merge]]1146Undoing a merge1147---------------11481149If you get stuck and decide to just give up and throw the whole mess1150away, you can always return to the pre-merge state with11511152-------------------------------------------------1153$ git reset --hard HEAD1154-------------------------------------------------11551156Or, if you've already commited the merge that you want to throw away,11571158-------------------------------------------------1159$ git reset --hard ORIG_HEAD1160-------------------------------------------------11611162However, this last command can be dangerous in some cases--never1163throw away a commit you have already committed if that commit may1164itself have been merged into another branch, as doing so may confuse1165further merges.11661167[[fast-forwards]]1168Fast-forward merges1169-------------------11701171There is one special case not mentioned above, which is treated1172differently. Normally, a merge results in a merge commit, with two1173parents, one pointing at each of the two lines of development that1174were merged.11751176However, if the current branch is a descendant of the other--so every1177commit present in the one is already contained in the other--then git1178just performs a "fast forward"; the head of the current branch is moved1179forward to point at the head of the merged-in branch, without any new1180commits being created.11811182[[fixing-mistakes]]1183Fixing mistakes1184---------------11851186If you've messed up the working tree, but haven't yet committed your1187mistake, you can return the entire working tree to the last committed1188state with11891190-------------------------------------------------1191$ git reset --hard HEAD1192-------------------------------------------------11931194If you make a commit that you later wish you hadn't, there are two1195fundamentally different ways to fix the problem:11961197 1. You can create a new commit that undoes whatever was done1198 by the previous commit. This is the correct thing if your1199 mistake has already been made public.12001201 2. You can go back and modify the old commit. You should1202 never do this if you have already made the history public;1203 git does not normally expect the "history" of a project to1204 change, and cannot correctly perform repeated merges from1205 a branch that has had its history changed.12061207[[reverting-a-commit]]1208Fixing a mistake with a new commit1209~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~12101211Creating a new commit that reverts an earlier change is very easy;1212just pass the gitlink:git-revert[1] command a reference to the bad1213commit; for example, to revert the most recent commit:12141215-------------------------------------------------1216$ git revert HEAD1217-------------------------------------------------12181219This will create a new commit which undoes the change in HEAD. You1220will be given a chance to edit the commit message for the new commit.12211222You can also revert an earlier change, for example, the next-to-last:12231224-------------------------------------------------1225$ git revert HEAD^1226-------------------------------------------------12271228In this case git will attempt to undo the old change while leaving1229intact any changes made since then. If more recent changes overlap1230with the changes to be reverted, then you will be asked to fix1231conflicts manually, just as in the case of <<resolving-a-merge,1232resolving a merge>>.12331234[[fixing-a-mistake-by-editing-history]]1235Fixing a mistake by editing history1236~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~12371238If the problematic commit is the most recent commit, and you have not1239yet made that commit public, then you may just1240<<undoing-a-merge,destroy it using git-reset>>.12411242Alternatively, you1243can edit the working directory and update the index to fix your1244mistake, just as if you were going to <<how-to-make-a-commit,create a1245new commit>>, then run12461247-------------------------------------------------1248$ git commit --amend1249-------------------------------------------------12501251which will replace the old commit by a new commit incorporating your1252changes, giving you a chance to edit the old commit message first.12531254Again, you should never do this to a commit that may already have1255been merged into another branch; use gitlink:git-revert[1] instead in1256that case.12571258It is also possible to edit commits further back in the history, but1259this is an advanced topic to be left for1260<<cleaning-up-history,another chapter>>.12611262[[checkout-of-path]]1263Checking out an old version of a file1264~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~12651266In the process of undoing a previous bad change, you may find it1267useful to check out an older version of a particular file using1268gitlink:git-checkout[1]. We've used git checkout before to switch1269branches, but it has quite different behavior if it is given a path1270name: the command12711272-------------------------------------------------1273$ git checkout HEAD^ path/to/file1274-------------------------------------------------12751276replaces path/to/file by the contents it had in the commit HEAD^, and1277also updates the index to match. It does not change branches.12781279If you just want to look at an old version of the file, without1280modifying the working directory, you can do that with1281gitlink:git-show[1]:12821283-------------------------------------------------1284$ git show HEAD^:path/to/file1285-------------------------------------------------12861287which will display the given version of the file.12881289[[ensuring-good-performance]]1290Ensuring good performance1291-------------------------12921293On large repositories, git depends on compression to keep the history1294information from taking up to much space on disk or in memory.12951296This compression is not performed automatically. Therefore you1297should occasionally run gitlink:git-gc[1]:12981299-------------------------------------------------1300$ git gc1301-------------------------------------------------13021303to recompress the archive. This can be very time-consuming, so1304you may prefer to run git-gc when you are not doing other work.130513061307[[ensuring-reliability]]1308Ensuring reliability1309--------------------13101311[[checking-for-corruption]]1312Checking the repository for corruption1313~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~13141315The gitlink:git-fsck[1] command runs a number of self-consistency checks1316on the repository, and reports on any problems. This may take some1317time. The most common warning by far is about "dangling" objects:13181319-------------------------------------------------1320$ git fsck1321dangling commit 7281251ddd2a61e38657c827739c57015671a6b31322dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a631323dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b51324dangling blob 218761f9d90712d37a9c5e36f406f92202db07eb1325dangling commit bf093535a34a4d35731aa2bd90fe6b176302f14f1326dangling commit 8e4bec7f2ddaa268bef999853c25755452100f8e1327dangling tree d50bb86186bf27b681d25af89d3b5b68382e40851328dangling tree b24c2473f1fd3d91352a624795be026d64c8841f1329...1330-------------------------------------------------13311332Dangling objects are not a problem. At worst they may take up a little1333extra disk space. They can sometimes provide a last-resort method of1334recovery lost work--see <<dangling-objects>> for details. However, if1335you want, you may remove them with gitlink:git-prune[1] or the --prune1336option to gitlink:git-gc[1]:13371338-------------------------------------------------1339$ git gc --prune1340-------------------------------------------------13411342This may be time-consuming. Unlike most other git operations (including1343git-gc when run without any options), it is not safe to prune while1344other git operations are in progress in the same repository.13451346[[recovering-lost-changes]]1347Recovering lost changes1348~~~~~~~~~~~~~~~~~~~~~~~13491350[[reflogs]]1351Reflogs1352^^^^^^^13531354Say you modify a branch with gitlink:git-reset[1] --hard, and then1355realize that the branch was the only reference you had to that point in1356history.13571358Fortunately, git also keeps a log, called a "reflog", of all the1359previous values of each branch. So in this case you can still find the1360old history using, for example, 13611362-------------------------------------------------1363$ git log master@{1}1364-------------------------------------------------13651366This lists the commits reachable from the previous version of the head.1367This syntax can be used to with any git command that accepts a commit,1368not just with git log. Some other examples:13691370-------------------------------------------------1371$ git show master@{2} # See where the branch pointed 2,1372$ git show master@{3} # 3, ... changes ago.1373$ gitk master@{yesterday} # See where it pointed yesterday,1374$ gitk master@{"1 week ago"} # ... or last week1375$ git log --walk-reflogs master # show reflog entries for master1376-------------------------------------------------13771378A separate reflog is kept for the HEAD, so13791380-------------------------------------------------1381$ git show HEAD@{"1 week ago"}1382-------------------------------------------------13831384will show what HEAD pointed to one week ago, not what the current branch1385pointed to one week ago. This allows you to see the history of what1386you've checked out.13871388The reflogs are kept by default for 30 days, after which they may be1389pruned. See gitlink:git-reflog[1] and gitlink:git-gc[1] to learn1390how to control this pruning, and see the "SPECIFYING REVISIONS"1391section of gitlink:git-rev-parse[1] for details.13921393Note that the reflog history is very different from normal git history.1394While normal history is shared by every repository that works on the1395same project, the reflog history is not shared: it tells you only about1396how the branches in your local repository have changed over time.13971398[[dangling-object-recovery]]1399Examining dangling objects1400^^^^^^^^^^^^^^^^^^^^^^^^^^14011402In some situations the reflog may not be able to save you. For example,1403suppose you delete a branch, then realize you need the history it1404contained. The reflog is also deleted; however, if you have not yet1405pruned the repository, then you may still be able to find the lost1406commits in the dangling objects that git-fsck reports. See1407<<dangling-objects>> for the details.14081409-------------------------------------------------1410$ git fsck1411dangling commit 7281251ddd2a61e38657c827739c57015671a6b31412dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a631413dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b51414...1415-------------------------------------------------14161417You can examine1418one of those dangling commits with, for example,14191420------------------------------------------------1421$ gitk 7281251ddd --not --all1422------------------------------------------------14231424which does what it sounds like: it says that you want to see the commit1425history that is described by the dangling commit(s), but not the1426history that is described by all your existing branches and tags. Thus1427you get exactly the history reachable from that commit that is lost.1428(And notice that it might not be just one commit: we only report the1429"tip of the line" as being dangling, but there might be a whole deep1430and complex commit history that was dropped.)14311432If you decide you want the history back, you can always create a new1433reference pointing to it, for example, a new branch:14341435------------------------------------------------1436$ git branch recovered-branch 7281251ddd 1437------------------------------------------------14381439Other types of dangling objects (blobs and trees) are also possible, and1440dangling objects can arise in other situations.144114421443[[sharing-development]]1444Sharing development with others1445===============================14461447[[getting-updates-with-git-pull]]1448Getting updates with git pull1449-----------------------------14501451After you clone a repository and make a few changes of your own, you1452may wish to check the original repository for updates and merge them1453into your own work.14541455We have already seen <<Updating-a-repository-with-git-fetch,how to1456keep remote tracking branches up to date>> with gitlink:git-fetch[1],1457and how to merge two branches. So you can merge in changes from the1458original repository's master branch with:14591460-------------------------------------------------1461$ git fetch1462$ git merge origin/master1463-------------------------------------------------14641465However, the gitlink:git-pull[1] command provides a way to do this in1466one step:14671468-------------------------------------------------1469$ git pull origin master1470-------------------------------------------------14711472In fact, "origin" is normally the default repository to pull from,1473and the default branch is normally the HEAD of the remote repository,1474so often you can accomplish the above with just14751476-------------------------------------------------1477$ git pull1478-------------------------------------------------14791480See the descriptions of the branch.<name>.remote and branch.<name>.merge1481options in gitlink:git-config[1] to learn how to control these defaults1482depending on the current branch. Also note that the --track option to1483gitlink:git-branch[1] and gitlink:git-checkout[1] can be used to1484automatically set the default remote branch to pull from at the time1485that a branch is created:14861487-------------------------------------------------1488$ git checkout --track -b origin/maint maint1489-------------------------------------------------14901491In addition to saving you keystrokes, "git pull" also helps you by1492producing a default commit message documenting the branch and1493repository that you pulled from.14941495(But note that no such commit will be created in the case of a1496<<fast-forwards,fast forward>>; instead, your branch will just be1497updated to point to the latest commit from the upstream branch.)14981499The git-pull command can also be given "." as the "remote" repository,1500in which case it just merges in a branch from the current repository; so1501the commands15021503-------------------------------------------------1504$ git pull . branch1505$ git merge branch1506-------------------------------------------------15071508are roughly equivalent. The former is actually very commonly used.15091510[[submitting-patches]]1511Submitting patches to a project1512-------------------------------15131514If you just have a few changes, the simplest way to submit them may1515just be to send them as patches in email:15161517First, use gitlink:git-format-patch[1]; for example:15181519-------------------------------------------------1520$ git format-patch origin1521-------------------------------------------------15221523will produce a numbered series of files in the current directory, one1524for each patch in the current branch but not in origin/HEAD.15251526You can then import these into your mail client and send them by1527hand. However, if you have a lot to send at once, you may prefer to1528use the gitlink:git-send-email[1] script to automate the process.1529Consult the mailing list for your project first to determine how they1530prefer such patches be handled.15311532[[importing-patches]]1533Importing patches to a project1534------------------------------15351536Git also provides a tool called gitlink:git-am[1] (am stands for1537"apply mailbox"), for importing such an emailed series of patches.1538Just save all of the patch-containing messages, in order, into a1539single mailbox file, say "patches.mbox", then run15401541-------------------------------------------------1542$ git am -3 patches.mbox1543-------------------------------------------------15441545Git will apply each patch in order; if any conflicts are found, it1546will stop, and you can fix the conflicts as described in1547"<<resolving-a-merge,Resolving a merge>>". (The "-3" option tells1548git to perform a merge; if you would prefer it just to abort and1549leave your tree and index untouched, you may omit that option.)15501551Once the index is updated with the results of the conflict1552resolution, instead of creating a new commit, just run15531554-------------------------------------------------1555$ git am --resolved1556-------------------------------------------------15571558and git will create the commit for you and continue applying the1559remaining patches from the mailbox.15601561The final result will be a series of commits, one for each patch in1562the original mailbox, with authorship and commit log message each1563taken from the message containing each patch.15641565[[setting-up-a-public-repository]]1566Setting up a public repository1567------------------------------15681569Another way to submit changes to a project is to simply tell the1570maintainer of that project to pull from your repository, exactly as1571you did in the section "<<getting-updates-with-git-pull, Getting1572updates with git pull>>".15731574If you and maintainer both have accounts on the same machine, then1575then you can just pull changes from each other's repositories1576directly; note that all of the commands (gitlink:git-clone[1],1577git-fetch[1], git-pull[1], etc.) that accept a URL as an argument1578will also accept a local directory name; so, for example, you can1579use15801581-------------------------------------------------1582$ git clone /path/to/repository1583$ git pull /path/to/other/repository1584-------------------------------------------------15851586If this sort of setup is inconvenient or impossible, another (more1587common) option is to set up a public repository on a public server.1588This also allows you to cleanly separate private work in progress1589from publicly visible work.15901591You will continue to do your day-to-day work in your personal1592repository, but periodically "push" changes from your personal1593repository into your public repository, allowing other developers to1594pull from that repository. So the flow of changes, in a situation1595where there is one other developer with a public repository, looks1596like this:15971598 you push1599 your personal repo ------------------> your public repo1600 ^ |1601 | |1602 | you pull | they pull1603 | |1604 | |1605 | they push V1606 their public repo <------------------- their repo16071608Now, assume your personal repository is in the directory ~/proj. We1609first create a new clone of the repository:16101611-------------------------------------------------1612$ git clone --bare ~/proj proj.git1613-------------------------------------------------16141615The resulting directory proj.git contains a "bare" git repository--it is1616just the contents of the ".git" directory, without a checked-out copy of1617a working directory.16181619Next, copy proj.git to the server where you plan to host the1620public repository. You can use scp, rsync, or whatever is most1621convenient.16221623If somebody else maintains the public server, they may already have1624set up a git service for you, and you may skip to the section1625"<<pushing-changes-to-a-public-repository,Pushing changes to a public1626repository>>", below.16271628Otherwise, the following sections explain how to export your newly1629created public repository:16301631[[exporting-via-http]]1632Exporting a git repository via http1633-----------------------------------16341635The git protocol gives better performance and reliability, but on a1636host with a web server set up, http exports may be simpler to set up.16371638All you need to do is place the newly created bare git repository in1639a directory that is exported by the web server, and make some1640adjustments to give web clients some extra information they need:16411642-------------------------------------------------1643$ mv proj.git /home/you/public_html/proj.git1644$ cd proj.git1645$ git --bare update-server-info1646$ chmod a+x hooks/post-update1647-------------------------------------------------16481649(For an explanation of the last two lines, see1650gitlink:git-update-server-info[1], and the documentation1651link:hooks.txt[Hooks used by git].)16521653Advertise the url of proj.git. Anybody else should then be able to1654clone or pull from that url, for example with a commandline like:16551656-------------------------------------------------1657$ git clone http://yourserver.com/~you/proj.git1658-------------------------------------------------16591660(See also1661link:howto/setup-git-server-over-http.txt[setup-git-server-over-http]1662for a slightly more sophisticated setup using WebDAV which also1663allows pushing over http.)16641665[[exporting-via-git]]1666Exporting a git repository via the git protocol1667-----------------------------------------------16681669This is the preferred method.16701671For now, we refer you to the gitlink:git-daemon[1] man page for1672instructions. (See especially the examples section.)16731674[[pushing-changes-to-a-public-repository]]1675Pushing changes to a public repository1676--------------------------------------16771678Note that the two techniques outline above (exporting via1679<<exporting-via-http,http>> or <<exporting-via-git,git>>) allow other1680maintainers to fetch your latest changes, but they do not allow write1681access, which you will need to update the public repository with the1682latest changes created in your private repository.16831684The simplest way to do this is using gitlink:git-push[1] and ssh; to1685update the remote branch named "master" with the latest state of your1686branch named "master", run16871688-------------------------------------------------1689$ git push ssh://yourserver.com/~you/proj.git master:master1690-------------------------------------------------16911692or just16931694-------------------------------------------------1695$ git push ssh://yourserver.com/~you/proj.git master1696-------------------------------------------------16971698As with git-fetch, git-push will complain if this does not result in1699a <<fast-forwards,fast forward>>. Normally this is a sign of1700something wrong. However, if you are sure you know what you're1701doing, you may force git-push to perform the update anyway by1702proceeding the branch name by a plus sign:17031704-------------------------------------------------1705$ git push ssh://yourserver.com/~you/proj.git +master1706-------------------------------------------------17071708As with git-fetch, you may also set up configuration options to1709save typing; so, for example, after17101711-------------------------------------------------1712$ cat >>.git/config <<EOF1713[remote "public-repo"]1714 url = ssh://yourserver.com/~you/proj.git1715EOF1716-------------------------------------------------17171718you should be able to perform the above push with just17191720-------------------------------------------------1721$ git push public-repo master1722-------------------------------------------------17231724See the explanations of the remote.<name>.url, branch.<name>.remote,1725and remote.<name>.push options in gitlink:git-config[1] for1726details.17271728[[setting-up-a-shared-repository]]1729Setting up a shared repository1730------------------------------17311732Another way to collaborate is by using a model similar to that1733commonly used in CVS, where several developers with special rights1734all push to and pull from a single shared repository. See1735link:cvs-migration.txt[git for CVS users] for instructions on how to1736set this up.17371738[[setting-up-gitweb]]1739Allow web browsing of a repository1740----------------------------------17411742The gitweb cgi script provides users an easy way to browse your1743project's files and history without having to install git; see the file1744gitweb/INSTALL in the git source tree for instructions on setting it up.17451746[[sharing-development-examples]]1747Examples1748--------17491750TODO: topic branches, typical roles as in everyday.txt, ?175117521753[[cleaning-up-history]]1754Rewriting history and maintaining patch series1755==============================================17561757Normally commits are only added to a project, never taken away or1758replaced. Git is designed with this assumption, and violating it will1759cause git's merge machinery (for example) to do the wrong thing.17601761However, there is a situation in which it can be useful to violate this1762assumption.17631764[[patch-series]]1765Creating the perfect patch series1766---------------------------------17671768Suppose you are a contributor to a large project, and you want to add a1769complicated feature, and to present it to the other developers in a way1770that makes it easy for them to read your changes, verify that they are1771correct, and understand why you made each change.17721773If you present all of your changes as a single patch (or commit), they1774may find that it is too much to digest all at once.17751776If you present them with the entire history of your work, complete with1777mistakes, corrections, and dead ends, they may be overwhelmed.17781779So the ideal is usually to produce a series of patches such that:17801781 1. Each patch can be applied in order.17821783 2. Each patch includes a single logical change, together with a1784 message explaining the change.17851786 3. No patch introduces a regression: after applying any initial1787 part of the series, the resulting project still compiles and1788 works, and has no bugs that it didn't have before.17891790 4. The complete series produces the same end result as your own1791 (probably much messier!) development process did.17921793We will introduce some tools that can help you do this, explain how to1794use them, and then explain some of the problems that can arise because1795you are rewriting history.17961797[[using-git-rebase]]1798Keeping a patch series up to date using git-rebase1799--------------------------------------------------18001801Suppose that you create a branch "mywork" on a remote-tracking branch1802"origin", and create some commits on top of it:18031804-------------------------------------------------1805$ git checkout -b mywork origin1806$ vi file.txt1807$ git commit1808$ vi otherfile.txt1809$ git commit1810...1811-------------------------------------------------18121813You have performed no merges into mywork, so it is just a simple linear1814sequence of patches on top of "origin":18151816................................................1817 o--o--o <-- origin1818 \1819 o--o--o <-- mywork1820................................................18211822Some more interesting work has been done in the upstream project, and1823"origin" has advanced:18241825................................................1826 o--o--O--o--o--o <-- origin1827 \1828 a--b--c <-- mywork1829................................................18301831At this point, you could use "pull" to merge your changes back in;1832the result would create a new merge commit, like this:18331834................................................1835 o--o--O--o--o--o <-- origin1836 \ \1837 a--b--c--m <-- mywork1838................................................18391840However, if you prefer to keep the history in mywork a simple series of1841commits without any merges, you may instead choose to use1842gitlink:git-rebase[1]:18431844-------------------------------------------------1845$ git checkout mywork1846$ git rebase origin1847-------------------------------------------------18481849This will remove each of your commits from mywork, temporarily saving1850them as patches (in a directory named ".dotest"), update mywork to1851point at the latest version of origin, then apply each of the saved1852patches to the new mywork. The result will look like:185318541855................................................1856 o--o--O--o--o--o <-- origin1857 \1858 a'--b'--c' <-- mywork1859................................................18601861In the process, it may discover conflicts. In that case it will stop1862and allow you to fix the conflicts; after fixing conflicts, use "git1863add" to update the index with those contents, and then, instead of1864running git-commit, just run18651866-------------------------------------------------1867$ git rebase --continue1868-------------------------------------------------18691870and git will continue applying the rest of the patches.18711872At any point you may use the --abort option to abort this process and1873return mywork to the state it had before you started the rebase:18741875-------------------------------------------------1876$ git rebase --abort1877-------------------------------------------------18781879[[modifying-one-commit]]1880Modifying a single commit1881-------------------------18821883We saw in <<fixing-a-mistake-by-editing-history>> that you can replace the1884most recent commit using18851886-------------------------------------------------1887$ git commit --amend1888-------------------------------------------------18891890which will replace the old commit by a new commit incorporating your1891changes, giving you a chance to edit the old commit message first.18921893You can also use a combination of this and gitlink:git-rebase[1] to edit1894commits further back in your history. First, tag the problematic commit with18951896-------------------------------------------------1897$ git tag bad mywork~51898-------------------------------------------------18991900(Either gitk or git-log may be useful for finding the commit.)19011902Then check out that commit, edit it, and rebase the rest of the series1903on top of it (note that we could check out the commit on a temporary1904branch, but instead we're using a <<detached-head,detached head>>):19051906-------------------------------------------------1907$ git checkout bad1908$ # make changes here and update the index1909$ git commit --amend1910$ git rebase --onto HEAD bad mywork1911-------------------------------------------------19121913When you're done, you'll be left with mywork checked out, with the top1914patches on mywork reapplied on top of your modified commit. You can1915then clean up with19161917-------------------------------------------------1918$ git tag -d bad1919-------------------------------------------------19201921Note that the immutable nature of git history means that you haven't really1922"modified" existing commits; instead, you have replaced the old commits with1923new commits having new object names.19241925[[reordering-patch-series]]1926Reordering or selecting from a patch series1927-------------------------------------------19281929Given one existing commit, the gitlink:git-cherry-pick[1] command1930allows you to apply the change introduced by that commit and create a1931new commit that records it. So, for example, if "mywork" points to a1932series of patches on top of "origin", you might do something like:19331934-------------------------------------------------1935$ git checkout -b mywork-new origin1936$ gitk origin..mywork &1937-------------------------------------------------19381939And browse through the list of patches in the mywork branch using gitk,1940applying them (possibly in a different order) to mywork-new using1941cherry-pick, and possibly modifying them as you go using commit1942--amend.19431944Another technique is to use git-format-patch to create a series of1945patches, then reset the state to before the patches:19461947-------------------------------------------------1948$ git format-patch origin1949$ git reset --hard origin1950-------------------------------------------------19511952Then modify, reorder, or eliminate patches as preferred before applying1953them again with gitlink:git-am[1].19541955[[patch-series-tools]]1956Other tools1957-----------19581959There are numerous other tools, such as stgit, which exist for the1960purpose of maintaining a patch series. These are outside of the scope of1961this manual.19621963[[problems-with-rewriting-history]]1964Problems with rewriting history1965-------------------------------19661967The primary problem with rewriting the history of a branch has to do1968with merging. Suppose somebody fetches your branch and merges it into1969their branch, with a result something like this:19701971................................................1972 o--o--O--o--o--o <-- origin1973 \ \1974 t--t--t--m <-- their branch:1975................................................19761977Then suppose you modify the last three commits:19781979................................................1980 o--o--o <-- new head of origin1981 /1982 o--o--O--o--o--o <-- old head of origin1983................................................19841985If we examined all this history together in one repository, it will1986look like:19871988................................................1989 o--o--o <-- new head of origin1990 /1991 o--o--O--o--o--o <-- old head of origin1992 \ \1993 t--t--t--m <-- their branch:1994................................................19951996Git has no way of knowing that the new head is an updated version of1997the old head; it treats this situation exactly the same as it would if1998two developers had independently done the work on the old and new heads1999in parallel. At this point, if someone attempts to merge the new head2000in to their branch, git will attempt to merge together the two (old and2001new) lines of development, instead of trying to replace the old by the2002new. The results are likely to be unexpected.20032004You may still choose to publish branches whose history is rewritten,2005and it may be useful for others to be able to fetch those branches in2006order to examine or test them, but they should not attempt to pull such2007branches into their own work.20082009For true distributed development that supports proper merging,2010published branches should never be rewritten.20112012[[advanced-branch-management]]2013Advanced branch management2014==========================20152016[[fetching-individual-branches]]2017Fetching individual branches2018----------------------------20192020Instead of using gitlink:git-remote[1], you can also choose just2021to update one branch at a time, and to store it locally under an2022arbitrary name:20232024-------------------------------------------------2025$ git fetch origin todo:my-todo-work2026-------------------------------------------------20272028The first argument, "origin", just tells git to fetch from the2029repository you originally cloned from. The second argument tells git2030to fetch the branch named "todo" from the remote repository, and to2031store it locally under the name refs/heads/my-todo-work.20322033You can also fetch branches from other repositories; so20342035-------------------------------------------------2036$ git fetch git://example.com/proj.git master:example-master2037-------------------------------------------------20382039will create a new branch named "example-master" and store in it the2040branch named "master" from the repository at the given URL. If you2041already have a branch named example-master, it will attempt to2042<<fast-forwards,fast-forward>> to the commit given by example.com's2043master branch. In more detail:20442045[[fetch-fast-forwards]]2046git fetch and fast-forwards2047---------------------------20482049In the previous example, when updating an existing branch, "git2050fetch" checks to make sure that the most recent commit on the remote2051branch is a descendant of the most recent commit on your copy of the2052branch before updating your copy of the branch to point at the new2053commit. Git calls this process a <<fast-forwards,fast forward>>.20542055A fast forward looks something like this:20562057................................................2058 o--o--o--o <-- old head of the branch2059 \2060 o--o--o <-- new head of the branch2061................................................206220632064In some cases it is possible that the new head will *not* actually be2065a descendant of the old head. For example, the developer may have2066realized she made a serious mistake, and decided to backtrack,2067resulting in a situation like:20682069................................................2070 o--o--o--o--a--b <-- old head of the branch2071 \2072 o--o--o <-- new head of the branch2073................................................20742075In this case, "git fetch" will fail, and print out a warning.20762077In that case, you can still force git to update to the new head, as2078described in the following section. However, note that in the2079situation above this may mean losing the commits labeled "a" and "b",2080unless you've already created a reference of your own pointing to2081them.20822083[[forcing-fetch]]2084Forcing git fetch to do non-fast-forward updates2085------------------------------------------------20862087If git fetch fails because the new head of a branch is not a2088descendant of the old head, you may force the update with:20892090-------------------------------------------------2091$ git fetch git://example.com/proj.git +master:refs/remotes/example/master2092-------------------------------------------------20932094Note the addition of the "+" sign. Alternatively, you can use the "-f"2095flag to force updates of all the fetched branches, as in:20962097-------------------------------------------------2098$ git fetch -f origin2099-------------------------------------------------21002101Be aware that commits that the old version of example/master pointed at2102may be lost, as we saw in the previous section.21032104[[remote-branch-configuration]]2105Configuring remote branches2106---------------------------21072108We saw above that "origin" is just a shortcut to refer to the2109repository that you originally cloned from. This information is2110stored in git configuration variables, which you can see using2111gitlink:git-config[1]:21122113-------------------------------------------------2114$ git config -l2115core.repositoryformatversion=02116core.filemode=true2117core.logallrefupdates=true2118remote.origin.url=git://git.kernel.org/pub/scm/git/git.git2119remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*2120branch.master.remote=origin2121branch.master.merge=refs/heads/master2122-------------------------------------------------21232124If there are other repositories that you also use frequently, you can2125create similar configuration options to save typing; for example,2126after21272128-------------------------------------------------2129$ git config remote.example.url git://example.com/proj.git2130-------------------------------------------------21312132then the following two commands will do the same thing:21332134-------------------------------------------------2135$ git fetch git://example.com/proj.git master:refs/remotes/example/master2136$ git fetch example master:refs/remotes/example/master2137-------------------------------------------------21382139Even better, if you add one more option:21402141-------------------------------------------------2142$ git config remote.example.fetch master:refs/remotes/example/master2143-------------------------------------------------21442145then the following commands will all do the same thing:21462147-------------------------------------------------2148$ git fetch git://example.com/proj.git master:refs/remotes/example/master2149$ git fetch example master:refs/remotes/example/master2150$ git fetch example2151-------------------------------------------------21522153You can also add a "+" to force the update each time:21542155-------------------------------------------------2156$ git config remote.example.fetch +master:ref/remotes/example/master2157-------------------------------------------------21582159Don't do this unless you're sure you won't mind "git fetch" possibly2160throwing away commits on mybranch.21612162Also note that all of the above configuration can be performed by2163directly editing the file .git/config instead of using2164gitlink:git-config[1].21652166See gitlink:git-config[1] for more details on the configuration2167options mentioned above.216821692170[[git-internals]]2171Git internals2172=============21732174Git depends on two fundamental abstractions: the "object database", and2175the "current directory cache" aka "index".21762177[[the-object-database]]2178The Object Database2179-------------------21802181The object database is literally just a content-addressable collection2182of objects. All objects are named by their content, which is2183approximated by the SHA1 hash of the object itself. Objects may refer2184to other objects (by referencing their SHA1 hash), and so you can2185build up a hierarchy of objects.21862187All objects have a statically determined "type" which is2188determined at object creation time, and which identifies the format of2189the object (i.e. how it is used, and how it can refer to other2190objects). There are currently four different object types: "blob",2191"tree", "commit", and "tag".21922193A <<def_blob_object,"blob" object>> cannot refer to any other object,2194and is, as the name implies, a pure storage object containing some2195user data. It is used to actually store the file data, i.e. a blob2196object is associated with some particular version of some file.21972198A <<def_tree_object,"tree" object>> is an object that ties one or more2199"blob" objects into a directory structure. In addition, a tree object2200can refer to other tree objects, thus creating a directory hierarchy.22012202A <<def_commit_object,"commit" object>> ties such directory hierarchies2203together into a <<def_DAG,directed acyclic graph>> of revisions - each2204"commit" is associated with exactly one tree (the directory hierarchy at2205the time of the commit). In addition, a "commit" refers to one or more2206"parent" commit objects that describe the history of how we arrived at2207that directory hierarchy.22082209As a special case, a commit object with no parents is called the "root"2210commit, and is the point of an initial project commit. Each project2211must have at least one root, and while you can tie several different2212root objects together into one project by creating a commit object which2213has two or more separate roots as its ultimate parents, that's probably2214just going to confuse people. So aim for the notion of "one root object2215per project", even if git itself does not enforce that. 22162217A <<def_tag_object,"tag" object>> symbolically identifies and can be2218used to sign other objects. It contains the identifier and type of2219another object, a symbolic name (of course!) and, optionally, a2220signature.22212222Regardless of object type, all objects share the following2223characteristics: they are all deflated with zlib, and have a header2224that not only specifies their type, but also provides size information2225about the data in the object. It's worth noting that the SHA1 hash2226that is used to name the object is the hash of the original data2227plus this header, so `sha1sum` 'file' does not match the object name2228for 'file'.2229(Historical note: in the dawn of the age of git the hash2230was the sha1 of the 'compressed' object.)22312232As a result, the general consistency of an object can always be tested2233independently of the contents or the type of the object: all objects can2234be validated by verifying that (a) their hashes match the content of the2235file and (b) the object successfully inflates to a stream of bytes that2236forms a sequence of <ascii type without space> + <space> + <ascii decimal2237size> + <byte\0> + <binary object data>. 22382239The structured objects can further have their structure and2240connectivity to other objects verified. This is generally done with2241the `git-fsck` program, which generates a full dependency graph2242of all objects, and verifies their internal consistency (in addition2243to just verifying their superficial consistency through the hash).22442245The object types in some more detail:22462247[[blob-object]]2248Blob Object2249-----------22502251A "blob" object is nothing but a binary blob of data, and doesn't2252refer to anything else. There is no signature or any other2253verification of the data, so while the object is consistent (it 'is'2254indexed by its sha1 hash, so the data itself is certainly correct), it2255has absolutely no other attributes. No name associations, no2256permissions. It is purely a blob of data (i.e. normally "file2257contents").22582259In particular, since the blob is entirely defined by its data, if two2260files in a directory tree (or in multiple different versions of the2261repository) have the same contents, they will share the same blob2262object. The object is totally independent of its location in the2263directory tree, and renaming a file does not change the object that2264file is associated with in any way.22652266A blob is typically created when gitlink:git-update-index[1]2267is run, and its data can be accessed by gitlink:git-cat-file[1].22682269[[tree-object]]2270Tree Object2271-----------22722273The next hierarchical object type is the "tree" object. A tree object2274is a list of mode/name/blob data, sorted by name. Alternatively, the2275mode data may specify a directory mode, in which case instead of2276naming a blob, that name is associated with another TREE object.22772278Like the "blob" object, a tree object is uniquely determined by the2279set contents, and so two separate but identical trees will always2280share the exact same object. This is true at all levels, i.e. it's2281true for a "leaf" tree (which does not refer to any other trees, only2282blobs) as well as for a whole subdirectory.22832284For that reason a "tree" object is just a pure data abstraction: it2285has no history, no signatures, no verification of validity, except2286that since the contents are again protected by the hash itself, we can2287trust that the tree is immutable and its contents never change.22882289So you can trust the contents of a tree to be valid, the same way you2290can trust the contents of a blob, but you don't know where those2291contents 'came' from.22922293Side note on trees: since a "tree" object is a sorted list of2294"filename+content", you can create a diff between two trees without2295actually having to unpack two trees. Just ignore all common parts,2296and your diff will look right. In other words, you can effectively2297(and efficiently) tell the difference between any two random trees by2298O(n) where "n" is the size of the difference, rather than the size of2299the tree.23002301Side note 2 on trees: since the name of a "blob" depends entirely and2302exclusively on its contents (i.e. there are no names or permissions2303involved), you can see trivial renames or permission changes by2304noticing that the blob stayed the same. However, renames with data2305changes need a smarter "diff" implementation.23062307A tree is created with gitlink:git-write-tree[1] and2308its data can be accessed by gitlink:git-ls-tree[1].2309Two trees can be compared with gitlink:git-diff-tree[1].23102311[[commit-object]]2312Commit Object2313-------------23142315The "commit" object is an object that introduces the notion of2316history into the picture. In contrast to the other objects, it2317doesn't just describe the physical state of a tree, it describes how2318we got there, and why.23192320A "commit" is defined by the tree-object that it results in, the2321parent commits (zero, one or more) that led up to that point, and a2322comment on what happened. Again, a commit is not trusted per se:2323the contents are well-defined and "safe" due to the cryptographically2324strong signatures at all levels, but there is no reason to believe2325that the tree is "good" or that the merge information makes sense.2326The parents do not have to actually have any relationship with the2327result, for example.23282329Note on commits: unlike some SCM's, commits do not contain2330rename information or file mode change information. All of that is2331implicit in the trees involved (the result tree, and the result trees2332of the parents), and describing that makes no sense in this idiotic2333file manager.23342335A commit is created with gitlink:git-commit-tree[1] and2336its data can be accessed by gitlink:git-cat-file[1].23372338[[trust]]2339Trust2340-----23412342An aside on the notion of "trust". Trust is really outside the scope2343of "git", but it's worth noting a few things. First off, since2344everything is hashed with SHA1, you 'can' trust that an object is2345intact and has not been messed with by external sources. So the name2346of an object uniquely identifies a known state - just not a state that2347you may want to trust.23482349Furthermore, since the SHA1 signature of a commit refers to the2350SHA1 signatures of the tree it is associated with and the signatures2351of the parent, a single named commit specifies uniquely a whole set2352of history, with full contents. You can't later fake any step of the2353way once you have the name of a commit.23542355So to introduce some real trust in the system, the only thing you need2356to do is to digitally sign just 'one' special note, which includes the2357name of a top-level commit. Your digital signature shows others2358that you trust that commit, and the immutability of the history of2359commits tells others that they can trust the whole history.23602361In other words, you can easily validate a whole archive by just2362sending out a single email that tells the people the name (SHA1 hash)2363of the top commit, and digitally sign that email using something2364like GPG/PGP.23652366To assist in this, git also provides the tag object...23672368[[tag-object]]2369Tag Object2370----------23712372Git provides the "tag" object to simplify creating, managing and2373exchanging symbolic and signed tokens. The "tag" object at its2374simplest simply symbolically identifies another object by containing2375the sha1, type and symbolic name.23762377However it can optionally contain additional signature information2378(which git doesn't care about as long as there's less than 8k of2379it). This can then be verified externally to git.23802381Note that despite the tag features, "git" itself only handles content2382integrity; the trust framework (and signature provision and2383verification) has to come from outside.23842385A tag is created with gitlink:git-mktag[1],2386its data can be accessed by gitlink:git-cat-file[1],2387and the signature can be verified by2388gitlink:git-verify-tag[1].238923902391[[the-index]]2392The "index" aka "Current Directory Cache"2393-----------------------------------------23942395The index is a simple binary file, which contains an efficient2396representation of the contents of a virtual directory. It2397does so by a simple array that associates a set of names, dates,2398permissions and content (aka "blob") objects together. The cache is2399always kept ordered by name, and names are unique (with a few very2400specific rules) at any point in time, but the cache has no long-term2401meaning, and can be partially updated at any time.24022403In particular, the index certainly does not need to be consistent with2404the current directory contents (in fact, most operations will depend on2405different ways to make the index 'not' be consistent with the directory2406hierarchy), but it has three very important attributes:24072408'(a) it can re-generate the full state it caches (not just the2409directory structure: it contains pointers to the "blob" objects so2410that it can regenerate the data too)'24112412As a special case, there is a clear and unambiguous one-way mapping2413from a current directory cache to a "tree object", which can be2414efficiently created from just the current directory cache without2415actually looking at any other data. So a directory cache at any one2416time uniquely specifies one and only one "tree" object (but has2417additional data to make it easy to match up that tree object with what2418has happened in the directory)24192420'(b) it has efficient methods for finding inconsistencies between that2421cached state ("tree object waiting to be instantiated") and the2422current state.'24232424'(c) it can additionally efficiently represent information about merge2425conflicts between different tree objects, allowing each pathname to be2426associated with sufficient information about the trees involved that2427you can create a three-way merge between them.'24282429Those are the ONLY three things that the directory cache does. It's a2430cache, and the normal operation is to re-generate it completely from a2431known tree object, or update/compare it with a live tree that is being2432developed. If you blow the directory cache away entirely, you generally2433haven't lost any information as long as you have the name of the tree2434that it described. 24352436At the same time, the index is at the same time also the2437staging area for creating new trees, and creating a new tree always2438involves a controlled modification of the index file. In particular,2439the index file can have the representation of an intermediate tree that2440has not yet been instantiated. So the index can be thought of as a2441write-back cache, which can contain dirty information that has not yet2442been written back to the backing store.2443244424452446[[the-workflow]]2447The Workflow2448------------24492450Generally, all "git" operations work on the index file. Some operations2451work *purely* on the index file (showing the current state of the2452index), but most operations move data to and from the index file. Either2453from the database or from the working directory. Thus there are four2454main combinations: 24552456[[working-directory-to-index]]2457working directory -> index2458~~~~~~~~~~~~~~~~~~~~~~~~~~24592460You update the index with information from the working directory with2461the gitlink:git-update-index[1] command. You2462generally update the index information by just specifying the filename2463you want to update, like so:24642465-------------------------------------------------2466$ git-update-index filename2467-------------------------------------------------24682469but to avoid common mistakes with filename globbing etc, the command2470will not normally add totally new entries or remove old entries,2471i.e. it will normally just update existing cache entries.24722473To tell git that yes, you really do realize that certain files no2474longer exist, or that new files should be added, you2475should use the `--remove` and `--add` flags respectively.24762477NOTE! A `--remove` flag does 'not' mean that subsequent filenames will2478necessarily be removed: if the files still exist in your directory2479structure, the index will be updated with their new status, not2480removed. The only thing `--remove` means is that update-cache will be2481considering a removed file to be a valid thing, and if the file really2482does not exist any more, it will update the index accordingly.24832484As a special case, you can also do `git-update-index --refresh`, which2485will refresh the "stat" information of each index to match the current2486stat information. It will 'not' update the object status itself, and2487it will only update the fields that are used to quickly test whether2488an object still matches its old backing store object.24892490[[index-to-object-database]]2491index -> object database2492~~~~~~~~~~~~~~~~~~~~~~~~24932494You write your current index file to a "tree" object with the program24952496-------------------------------------------------2497$ git-write-tree2498-------------------------------------------------24992500that doesn't come with any options - it will just write out the2501current index into the set of tree objects that describe that state,2502and it will return the name of the resulting top-level tree. You can2503use that tree to re-generate the index at any time by going in the2504other direction:25052506[[object-database-to-index]]2507object database -> index2508~~~~~~~~~~~~~~~~~~~~~~~~25092510You read a "tree" file from the object database, and use that to2511populate (and overwrite - don't do this if your index contains any2512unsaved state that you might want to restore later!) your current2513index. Normal operation is just25142515-------------------------------------------------2516$ git-read-tree <sha1 of tree>2517-------------------------------------------------25182519and your index file will now be equivalent to the tree that you saved2520earlier. However, that is only your 'index' file: your working2521directory contents have not been modified.25222523[[index-to-working-directory]]2524index -> working directory2525~~~~~~~~~~~~~~~~~~~~~~~~~~25262527You update your working directory from the index by "checking out"2528files. This is not a very common operation, since normally you'd just2529keep your files updated, and rather than write to your working2530directory, you'd tell the index files about the changes in your2531working directory (i.e. `git-update-index`).25322533However, if you decide to jump to a new version, or check out somebody2534else's version, or just restore a previous tree, you'd populate your2535index file with read-tree, and then you need to check out the result2536with25372538-------------------------------------------------2539$ git-checkout-index filename2540-------------------------------------------------25412542or, if you want to check out all of the index, use `-a`.25432544NOTE! git-checkout-index normally refuses to overwrite old files, so2545if you have an old version of the tree already checked out, you will2546need to use the "-f" flag ('before' the "-a" flag or the filename) to2547'force' the checkout.254825492550Finally, there are a few odds and ends which are not purely moving2551from one representation to the other:25522553[[tying-it-all-together]]2554Tying it all together2555~~~~~~~~~~~~~~~~~~~~~25562557To commit a tree you have instantiated with "git-write-tree", you'd2558create a "commit" object that refers to that tree and the history2559behind it - most notably the "parent" commits that preceded it in2560history.25612562Normally a "commit" has one parent: the previous state of the tree2563before a certain change was made. However, sometimes it can have two2564or more parent commits, in which case we call it a "merge", due to the2565fact that such a commit brings together ("merges") two or more2566previous states represented by other commits.25672568In other words, while a "tree" represents a particular directory state2569of a working directory, a "commit" represents that state in "time",2570and explains how we got there.25712572You create a commit object by giving it the tree that describes the2573state at the time of the commit, and a list of parents:25742575-------------------------------------------------2576$ git-commit-tree <tree> -p <parent> [-p <parent2> ..]2577-------------------------------------------------25782579and then giving the reason for the commit on stdin (either through2580redirection from a pipe or file, or by just typing it at the tty).25812582git-commit-tree will return the name of the object that represents2583that commit, and you should save it away for later use. Normally,2584you'd commit a new `HEAD` state, and while git doesn't care where you2585save the note about that state, in practice we tend to just write the2586result to the file pointed at by `.git/HEAD`, so that we can always see2587what the last committed state was.25882589Here is an ASCII art by Jon Loeliger that illustrates how2590various pieces fit together.25912592------------25932594 commit-tree2595 commit obj2596 +----+2597 | |2598 | |2599 V V2600 +-----------+2601 | Object DB |2602 | Backing |2603 | Store |2604 +-----------+2605 ^2606 write-tree | |2607 tree obj | |2608 | | read-tree2609 | | tree obj2610 V2611 +-----------+2612 | Index |2613 | "cache" |2614 +-----------+2615 update-index ^2616 blob obj | |2617 | |2618 checkout-index -u | | checkout-index2619 stat | | blob obj2620 V2621 +-----------+2622 | Working |2623 | Directory |2624 +-----------+26252626------------262726282629[[examining-the-data]]2630Examining the data2631------------------26322633You can examine the data represented in the object database and the2634index with various helper tools. For every object, you can use2635gitlink:git-cat-file[1] to examine details about the2636object:26372638-------------------------------------------------2639$ git-cat-file -t <objectname>2640-------------------------------------------------26412642shows the type of the object, and once you have the type (which is2643usually implicit in where you find the object), you can use26442645-------------------------------------------------2646$ git-cat-file blob|tree|commit|tag <objectname>2647-------------------------------------------------26482649to show its contents. NOTE! Trees have binary content, and as a result2650there is a special helper for showing that content, called2651`git-ls-tree`, which turns the binary content into a more easily2652readable form.26532654It's especially instructive to look at "commit" objects, since those2655tend to be small and fairly self-explanatory. In particular, if you2656follow the convention of having the top commit name in `.git/HEAD`,2657you can do26582659-------------------------------------------------2660$ git-cat-file commit HEAD2661-------------------------------------------------26622663to see what the top commit was.26642665[[merging-multiple-trees]]2666Merging multiple trees2667----------------------26682669Git helps you do a three-way merge, which you can expand to n-way by2670repeating the merge procedure arbitrary times until you finally2671"commit" the state. The normal situation is that you'd only do one2672three-way merge (two parents), and commit it, but if you like to, you2673can do multiple parents in one go.26742675To do a three-way merge, you need the two sets of "commit" objects2676that you want to merge, use those to find the closest common parent (a2677third "commit" object), and then use those commit objects to find the2678state of the directory ("tree" object) at these points.26792680To get the "base" for the merge, you first look up the common parent2681of two commits with26822683-------------------------------------------------2684$ git-merge-base <commit1> <commit2>2685-------------------------------------------------26862687which will return you the commit they are both based on. You should2688now look up the "tree" objects of those commits, which you can easily2689do with (for example)26902691-------------------------------------------------2692$ git-cat-file commit <commitname> | head -12693-------------------------------------------------26942695since the tree object information is always the first line in a commit2696object.26972698Once you know the three trees you are going to merge (the one "original"2699tree, aka the common tree, and the two "result" trees, aka the branches2700you want to merge), you do a "merge" read into the index. This will2701complain if it has to throw away your old index contents, so you should2702make sure that you've committed those - in fact you would normally2703always do a merge against your last commit (which should thus match what2704you have in your current index anyway).27052706To do the merge, do27072708-------------------------------------------------2709$ git-read-tree -m -u <origtree> <yourtree> <targettree>2710-------------------------------------------------27112712which will do all trivial merge operations for you directly in the2713index file, and you can just write the result out with2714`git-write-tree`.271527162717[[merging-multiple-trees-2]]2718Merging multiple trees, continued2719---------------------------------27202721Sadly, many merges aren't trivial. If there are files that have2722been added.moved or removed, or if both branches have modified the2723same file, you will be left with an index tree that contains "merge2724entries" in it. Such an index tree can 'NOT' be written out to a tree2725object, and you will have to resolve any such merge clashes using2726other tools before you can write out the result.27272728You can examine such index state with `git-ls-files --unmerged`2729command. An example:27302731------------------------------------------------2732$ git-read-tree -m $orig HEAD $target2733$ git-ls-files --unmerged2734100644 263414f423d0e4d70dae8fe53fa34614ff3e2860 1 hello.c2735100644 06fa6a24256dc7e560efa5687fa84b51f0263c3a 2 hello.c2736100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello.c2737------------------------------------------------27382739Each line of the `git-ls-files --unmerged` output begins with2740the blob mode bits, blob SHA1, 'stage number', and the2741filename. The 'stage number' is git's way to say which tree it2742came from: stage 1 corresponds to `$orig` tree, stage 2 `HEAD`2743tree, and stage3 `$target` tree.27442745Earlier we said that trivial merges are done inside2746`git-read-tree -m`. For example, if the file did not change2747from `$orig` to `HEAD` nor `$target`, or if the file changed2748from `$orig` to `HEAD` and `$orig` to `$target` the same way,2749obviously the final outcome is what is in `HEAD`. What the2750above example shows is that file `hello.c` was changed from2751`$orig` to `HEAD` and `$orig` to `$target` in a different way.2752You could resolve this by running your favorite 3-way merge2753program, e.g. `diff3`, `merge`, or git's own merge-file, on2754the blob objects from these three stages yourself, like this:27552756------------------------------------------------2757$ git-cat-file blob 263414f... >hello.c~12758$ git-cat-file blob 06fa6a2... >hello.c~22759$ git-cat-file blob cc44c73... >hello.c~32760$ git merge-file hello.c~2 hello.c~1 hello.c~32761------------------------------------------------27622763This would leave the merge result in `hello.c~2` file, along2764with conflict markers if there are conflicts. After verifying2765the merge result makes sense, you can tell git what the final2766merge result for this file is by:27672768-------------------------------------------------2769$ mv -f hello.c~2 hello.c2770$ git-update-index hello.c2771-------------------------------------------------27722773When a path is in unmerged state, running `git-update-index` for2774that path tells git to mark the path resolved.27752776The above is the description of a git merge at the lowest level,2777to help you understand what conceptually happens under the hood.2778In practice, nobody, not even git itself, uses three `git-cat-file`2779for this. There is `git-merge-index` program that extracts the2780stages to temporary files and calls a "merge" script on it:27812782-------------------------------------------------2783$ git-merge-index git-merge-one-file hello.c2784-------------------------------------------------27852786and that is what higher level `git merge -s resolve` is implemented with.27872788[[pack-files]]2789How git stores objects efficiently: pack files2790----------------------------------------------27912792We've seen how git stores each object in a file named after the2793object's SHA1 hash.27942795Unfortunately this system becomes inefficient once a project has a2796lot of objects. Try this on an old project:27972798------------------------------------------------2799$ git count-objects28006930 objects, 47620 kilobytes2801------------------------------------------------28022803The first number is the number of objects which are kept in2804individual files. The second is the amount of space taken up by2805those "loose" objects.28062807You can save space and make git faster by moving these loose objects in2808to a "pack file", which stores a group of objects in an efficient2809compressed format; the details of how pack files are formatted can be2810found in link:technical/pack-format.txt[technical/pack-format.txt].28112812To put the loose objects into a pack, just run git repack:28132814------------------------------------------------2815$ git repack2816Generating pack...2817Done counting 6020 objects.2818Deltifying 6020 objects.2819 100% (6020/6020) done2820Writing 6020 objects.2821 100% (6020/6020) done2822Total 6020, written 6020 (delta 4070), reused 0 (delta 0)2823Pack pack-3e54ad29d5b2e05838c75df582c65257b8d08e1c created.2824------------------------------------------------28252826You can then run28272828------------------------------------------------2829$ git prune2830------------------------------------------------28312832to remove any of the "loose" objects that are now contained in the2833pack. This will also remove any unreferenced objects (which may be2834created when, for example, you use "git reset" to remove a commit).2835You can verify that the loose objects are gone by looking at the2836.git/objects directory or by running28372838------------------------------------------------2839$ git count-objects28400 objects, 0 kilobytes2841------------------------------------------------28422843Although the object files are gone, any commands that refer to those2844objects will work exactly as they did before.28452846The gitlink:git-gc[1] command performs packing, pruning, and more for2847you, so is normally the only high-level command you need.28482849[[dangling-objects]]2850Dangling objects2851----------------28522853The gitlink:git-fsck[1] command will sometimes complain about dangling2854objects. They are not a problem.28552856The most common cause of dangling objects is that you've rebased a2857branch, or you have pulled from somebody else who rebased a branch--see2858<<cleaning-up-history>>. In that case, the old head of the original2859branch still exists, as does everything it pointed to. The branch2860pointer itself just doesn't, since you replaced it with another one.28612862There are also other situations that cause dangling objects. For2863example, a "dangling blob" may arise because you did a "git add" of a2864file, but then, before you actually committed it and made it part of the2865bigger picture, you changed something else in that file and committed2866that *updated* thing - the old state that you added originally ends up2867not being pointed to by any commit or tree, so it's now a dangling blob2868object.28692870Similarly, when the "recursive" merge strategy runs, and finds that2871there are criss-cross merges and thus more than one merge base (which is2872fairly unusual, but it does happen), it will generate one temporary2873midway tree (or possibly even more, if you had lots of criss-crossing2874merges and more than two merge bases) as a temporary internal merge2875base, and again, those are real objects, but the end result will not end2876up pointing to them, so they end up "dangling" in your repository.28772878Generally, dangling objects aren't anything to worry about. They can2879even be very useful: if you screw something up, the dangling objects can2880be how you recover your old tree (say, you did a rebase, and realized2881that you really didn't want to - you can look at what dangling objects2882you have, and decide to reset your head to some old dangling state).28832884For commits, you can just use:28852886------------------------------------------------2887$ gitk <dangling-commit-sha-goes-here> --not --all2888------------------------------------------------28892890This asks for all the history reachable from the given commit but not2891from any branch, tag, or other reference. If you decide it's something2892you want, you can always create a new reference to it, e.g.,28932894------------------------------------------------2895$ git branch recovered-branch <dangling-commit-sha-goes-here>2896------------------------------------------------28972898For blobs and trees, you can't do the same, but you can still examine2899them. You can just do29002901------------------------------------------------2902$ git show <dangling-blob/tree-sha-goes-here>2903------------------------------------------------29042905to show what the contents of the blob were (or, for a tree, basically2906what the "ls" for that directory was), and that may give you some idea2907of what the operation was that left that dangling object.29082909Usually, dangling blobs and trees aren't very interesting. They're2910almost always the result of either being a half-way mergebase (the blob2911will often even have the conflict markers from a merge in it, if you2912have had conflicting merges that you fixed up by hand), or simply2913because you interrupted a "git fetch" with ^C or something like that,2914leaving _some_ of the new objects in the object database, but just2915dangling and useless.29162917Anyway, once you are sure that you're not interested in any dangling 2918state, you can just prune all unreachable objects:29192920------------------------------------------------2921$ git prune2922------------------------------------------------29232924and they'll be gone. But you should only run "git prune" on a quiescent2925repository - it's kind of like doing a filesystem fsck recovery: you2926don't want to do that while the filesystem is mounted.29272928(The same is true of "git-fsck" itself, btw - but since 2929git-fsck never actually *changes* the repository, it just reports 2930on what it found, git-fsck itself is never "dangerous" to run. 2931Running it while somebody is actually changing the repository can cause 2932confusing and scary messages, but it won't actually do anything bad. In 2933contrast, running "git prune" while somebody is actively changing the 2934repository is a *BAD* idea).29352936[[birdview-on-the-source-code]]2937A birds-eye view of Git's source code2938-------------------------------------29392940It is not always easy for new developers to find their way through Git's2941source code. This section gives you a little guidance to show where to2942start.29432944A good place to start is with the contents of the initial commit, with:29452946----------------------------------------------------2947$ git checkout e83c51632948----------------------------------------------------29492950The initial revision lays the foundation for almost everything git has2951today, but is small enough to read in one sitting.29522953Note that terminology has changed since that revision. For example, the2954README in that revision uses the word "changeset" to describe what we2955now call a <<def_commit_object,commit>>.29562957Also, we do not call it "cache" any more, but "index", however, the2958file is still called `cache.h`. Remark: Not much reason to change it now,2959especially since there is no good single name for it anyway, because it is2960basically _the_ header file which is included by _all_ of Git's C sources.29612962If you grasp the ideas in that initial commit, you should check out a2963more recent version and skim `cache.h`, `object.h` and `commit.h`.29642965In the early days, Git (in the tradition of UNIX) was a bunch of programs2966which were extremely simple, and which you used in scripts, piping the2967output of one into another. This turned out to be good for initial2968development, since it was easier to test new things. However, recently2969many of these parts have become builtins, and some of the core has been2970"libified", i.e. put into libgit.a for performance, portability reasons,2971and to avoid code duplication.29722973By now, you know what the index is (and find the corresponding data2974structures in `cache.h`), and that there are just a couple of object types2975(blobs, trees, commits and tags) which inherit their common structure from2976`struct object`, which is their first member (and thus, you can cast e.g.2977`(struct object *)commit` to achieve the _same_ as `&commit->object`, i.e.2978get at the object name and flags).29792980Now is a good point to take a break to let this information sink in.29812982Next step: get familiar with the object naming. Read <<naming-commits>>.2983There are quite a few ways to name an object (and not only revisions!).2984All of these are handled in `sha1_name.c`. Just have a quick look at2985the function `get_sha1()`. A lot of the special handling is done by2986functions like `get_sha1_basic()` or the likes.29872988This is just to get you into the groove for the most libified part of Git:2989the revision walker.29902991Basically, the initial version of `git log` was a shell script:29922993----------------------------------------------------------------2994$ git-rev-list --pretty $(git-rev-parse --default HEAD "$@") | \2995 LESS=-S ${PAGER:-less}2996----------------------------------------------------------------29972998What does this mean?29993000`git-rev-list` is the original version of the revision walker, which3001_always_ printed a list of revisions to stdout. It is still functional,3002and needs to, since most new Git programs start out as scripts using3003`git-rev-list`.30043005`git-rev-parse` is not as important any more; it was only used to filter out3006options that were relevant for the different plumbing commands that were3007called by the script.30083009Most of what `git-rev-list` did is contained in `revision.c` and3010`revision.h`. It wraps the options in a struct named `rev_info`, which3011controls how and what revisions are walked, and more.30123013The original job of `git-rev-parse` is now taken by the function3014`setup_revisions()`, which parses the revisions and the common command line3015options for the revision walker. This information is stored in the struct3016`rev_info` for later consumption. You can do your own command line option3017parsing after calling `setup_revisions()`. After that, you have to call3018`prepare_revision_walk()` for initialization, and then you can get the3019commits one by one with the function `get_revision()`.30203021If you are interested in more details of the revision walking process,3022just have a look at the first implementation of `cmd_log()`; call3023`git-show v1.3.0~155^2~4` and scroll down to that function (note that you3024no longer need to call `setup_pager()` directly).30253026Nowadays, `git log` is a builtin, which means that it is _contained_ in the3027command `git`. The source side of a builtin is30283029- a function called `cmd_<bla>`, typically defined in `builtin-<bla>.c`,3030 and declared in `builtin.h`,30313032- an entry in the `commands[]` array in `git.c`, and30333034- an entry in `BUILTIN_OBJECTS` in the `Makefile`.30353036Sometimes, more than one builtin is contained in one source file. For3037example, `cmd_whatchanged()` and `cmd_log()` both reside in `builtin-log.c`,3038since they share quite a bit of code. In that case, the commands which are3039_not_ named like the `.c` file in which they live have to be listed in3040`BUILT_INS` in the `Makefile`.30413042`git log` looks more complicated in C than it does in the original script,3043but that allows for a much greater flexibility and performance.30443045Here again it is a good point to take a pause.30463047Lesson three is: study the code. Really, it is the best way to learn about3048the organization of Git (after you know the basic concepts).30493050So, think about something which you are interested in, say, "how can I3051access a blob just knowing the object name of it?". The first step is to3052find a Git command with which you can do it. In this example, it is either3053`git show` or `git cat-file`.30543055For the sake of clarity, let's stay with `git cat-file`, because it30563057- is plumbing, and30583059- was around even in the initial commit (it literally went only through3060 some 20 revisions as `cat-file.c`, was renamed to `builtin-cat-file.c`3061 when made a builtin, and then saw less than 10 versions).30623063So, look into `builtin-cat-file.c`, search for `cmd_cat_file()` and look what3064it does.30653066------------------------------------------------------------------3067 git_config(git_default_config);3068 if (argc != 3)3069 usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");3070 if (get_sha1(argv[2], sha1))3071 die("Not a valid object name %s", argv[2]);3072------------------------------------------------------------------30733074Let's skip over the obvious details; the only really interesting part3075here is the call to `get_sha1()`. It tries to interpret `argv[2]` as an3076object name, and if it refers to an object which is present in the current3077repository, it writes the resulting SHA-1 into the variable `sha1`.30783079Two things are interesting here:30803081- `get_sha1()` returns 0 on _success_. This might surprise some new3082 Git hackers, but there is a long tradition in UNIX to return different3083 negative numbers in case of different errors -- and 0 on success.30843085- the variable `sha1` in the function signature of `get_sha1()` is `unsigned3086 char \*`, but is actually expected to be a pointer to `unsigned3087 char[20]`. This variable will contain the 160-bit SHA-1 of the given3088 commit. Note that whenever a SHA-1 is passed as `unsigned char \*`, it3089 is the binary representation, as opposed to the ASCII representation in3090 hex characters, which is passed as `char *`.30913092You will see both of these things throughout the code.30933094Now, for the meat:30953096-----------------------------------------------------------------------------3097 case 0:3098 buf = read_object_with_reference(sha1, argv[1], &size, NULL);3099-----------------------------------------------------------------------------31003101This is how you read a blob (actually, not only a blob, but any type of3102object). To know how the function `read_object_with_reference()` actually3103works, find the source code for it (something like `git grep3104read_object_with | grep ":[a-z]"` in the git repository), and read3105the source.31063107To find out how the result can be used, just read on in `cmd_cat_file()`:31083109-----------------------------------3110 write_or_die(1, buf, size);3111-----------------------------------31123113Sometimes, you do not know where to look for a feature. In many such cases,3114it helps to search through the output of `git log`, and then `git show` the3115corresponding commit.31163117Example: If you know that there was some test case for `git bundle`, but3118do not remember where it was (yes, you _could_ `git grep bundle t/`, but that3119does not illustrate the point!):31203121------------------------3122$ git log --no-merges t/3123------------------------31243125In the pager (`less`), just search for "bundle", go a few lines back,3126and see that it is in commit 18449ab0... Now just copy this object name,3127and paste it into the command line31283129-------------------3130$ git show 18449ab03131-------------------31323133Voila.31343135Another example: Find out what to do in order to make some script a3136builtin:31373138-------------------------------------------------3139$ git log --no-merges --diff-filter=A builtin-*.c3140-------------------------------------------------31413142You see, Git is actually the best tool to find out about the source of Git3143itself!31443145[[glossary]]3146include::glossary.txt[]31473148[[git-quick-start]]3149Appendix A: Git Quick Start3150===========================31513152This is a quick summary of the major commands; the following chapters3153will explain how these work in more detail.31543155[[quick-creating-a-new-repository]]3156Creating a new repository3157-------------------------31583159From a tarball:31603161-----------------------------------------------3162$ tar xzf project.tar.gz3163$ cd project3164$ git init3165Initialized empty Git repository in .git/3166$ git add .3167$ git commit3168-----------------------------------------------31693170From a remote repository:31713172-----------------------------------------------3173$ git clone git://example.com/pub/project.git3174$ cd project3175-----------------------------------------------31763177[[managing-branches]]3178Managing branches3179-----------------31803181-----------------------------------------------3182$ git branch # list all local branches in this repo3183$ git checkout test # switch working directory to branch "test"3184$ git branch new # create branch "new" starting at current HEAD3185$ git branch -d new # delete branch "new"3186-----------------------------------------------31873188Instead of basing new branch on current HEAD (the default), use:31893190-----------------------------------------------3191$ git branch new test # branch named "test"3192$ git branch new v2.6.15 # tag named v2.6.153193$ git branch new HEAD^ # commit before the most recent3194$ git branch new HEAD^^ # commit before that3195$ git branch new test~10 # ten commits before tip of branch "test"3196-----------------------------------------------31973198Create and switch to a new branch at the same time:31993200-----------------------------------------------3201$ git checkout -b new v2.6.153202-----------------------------------------------32033204Update and examine branches from the repository you cloned from:32053206-----------------------------------------------3207$ git fetch # update3208$ git branch -r # list3209 origin/master3210 origin/next3211 ...3212$ git checkout -b masterwork origin/master3213-----------------------------------------------32143215Fetch a branch from a different repository, and give it a new3216name in your repository:32173218-----------------------------------------------3219$ git fetch git://example.com/project.git theirbranch:mybranch3220$ git fetch git://example.com/project.git v2.6.15:mybranch3221-----------------------------------------------32223223Keep a list of repositories you work with regularly:32243225-----------------------------------------------3226$ git remote add example git://example.com/project.git3227$ git remote # list remote repositories3228example3229origin3230$ git remote show example # get details3231* remote example3232 URL: git://example.com/project.git3233 Tracked remote branches3234 master next ...3235$ git fetch example # update branches from example3236$ git branch -r # list all remote branches3237-----------------------------------------------323832393240[[exploring-history]]3241Exploring history3242-----------------32433244-----------------------------------------------3245$ gitk # visualize and browse history3246$ git log # list all commits3247$ git log src/ # ...modifying src/3248$ git log v2.6.15..v2.6.16 # ...in v2.6.16, not in v2.6.153249$ git log master..test # ...in branch test, not in branch master3250$ git log test..master # ...in branch master, but not in test3251$ git log test...master # ...in one branch, not in both3252$ git log -S'foo()' # ...where difference contain "foo()"3253$ git log --since="2 weeks ago"3254$ git log -p # show patches as well3255$ git show # most recent commit3256$ git diff v2.6.15..v2.6.16 # diff between two tagged versions3257$ git diff v2.6.15..HEAD # diff with current head3258$ git grep "foo()" # search working directory for "foo()"3259$ git grep v2.6.15 "foo()" # search old tree for "foo()"3260$ git show v2.6.15:a.txt # look at old version of a.txt3261-----------------------------------------------32623263Search for regressions:32643265-----------------------------------------------3266$ git bisect start3267$ git bisect bad # current version is bad3268$ git bisect good v2.6.13-rc2 # last known good revision3269Bisecting: 675 revisions left to test after this3270 # test here, then:3271$ git bisect good # if this revision is good, or3272$ git bisect bad # if this revision is bad.3273 # repeat until done.3274-----------------------------------------------32753276[[making-changes]]3277Making changes3278--------------32793280Make sure git knows who to blame:32813282------------------------------------------------3283$ cat >>~/.gitconfig <<\EOF3284[user]3285 name = Your Name Comes Here3286 email = you@yourdomain.example.com3287EOF3288------------------------------------------------32893290Select file contents to include in the next commit, then make the3291commit:32923293-----------------------------------------------3294$ git add a.txt # updated file3295$ git add b.txt # new file3296$ git rm c.txt # old file3297$ git commit3298-----------------------------------------------32993300Or, prepare and create the commit in one step:33013302-----------------------------------------------3303$ git commit d.txt # use latest content only of d.txt3304$ git commit -a # use latest content of all tracked files3305-----------------------------------------------33063307[[merging]]3308Merging3309-------33103311-----------------------------------------------3312$ git merge test # merge branch "test" into the current branch3313$ git pull git://example.com/project.git master3314 # fetch and merge in remote branch3315$ git pull . test # equivalent to git merge test3316-----------------------------------------------33173318[[sharing-your-changes]]3319Sharing your changes3320--------------------33213322Importing or exporting patches:33233324-----------------------------------------------3325$ git format-patch origin..HEAD # format a patch for each commit3326 # in HEAD but not in origin3327$ git am mbox # import patches from the mailbox "mbox"3328-----------------------------------------------33293330Fetch a branch in a different git repository, then merge into the3331current branch:33323333-----------------------------------------------3334$ git pull git://example.com/project.git theirbranch3335-----------------------------------------------33363337Store the fetched branch into a local branch before merging into the3338current branch:33393340-----------------------------------------------3341$ git pull git://example.com/project.git theirbranch:mybranch3342-----------------------------------------------33433344After creating commits on a local branch, update the remote3345branch with your commits:33463347-----------------------------------------------3348$ git push ssh://example.com/project.git mybranch:theirbranch3349-----------------------------------------------33503351When remote and local branch are both named "test":33523353-----------------------------------------------3354$ git push ssh://example.com/project.git test3355-----------------------------------------------33563357Shortcut version for a frequently used remote repository:33583359-----------------------------------------------3360$ git remote add example ssh://example.com/project.git3361$ git push example test3362-----------------------------------------------33633364[[repository-maintenance]]3365Repository maintenance3366----------------------33673368Check for corruption:33693370-----------------------------------------------3371$ git fsck3372-----------------------------------------------33733374Recompress, remove unused cruft:33753376-----------------------------------------------3377$ git gc3378-----------------------------------------------337933803381[[todo]]3382Appendix B: Notes and todo list for this manual3383===============================================33843385This is a work in progress.33863387The basic requirements:3388 - It must be readable in order, from beginning to end, by3389 someone intelligent with a basic grasp of the unix3390 commandline, but without any special knowledge of git. If3391 necessary, any other prerequisites should be specifically3392 mentioned as they arise.3393 - Whenever possible, section headings should clearly describe3394 the task they explain how to do, in language that requires3395 no more knowledge than necessary: for example, "importing3396 patches into a project" rather than "the git-am command"33973398Think about how to create a clear chapter dependency graph that will3399allow people to get to important topics without necessarily reading3400everything in between.34013402Say something about .gitignore.34033404Scan Documentation/ for other stuff left out; in particular:3405 howto's3406 some of technical/?3407 hooks3408 list of commands in gitlink:git[1]34093410Scan email archives for other stuff left out34113412Scan man pages to see if any assume more background than this manual3413provides.34143415Simplify beginning by suggesting disconnected head instead of3416temporary branch creation?34173418Add more good examples. Entire sections of just cookbook examples3419might be a good idea; maybe make an "advanced examples" section a3420standard end-of-chapter section?34213422Include cross-references to the glossary, where appropriate.34233424Document shallow clones? See draft 1.5.0 release notes for some3425documentation.34263427Add a section on working with other version control systems, including3428CVS, Subversion, and just imports of series of release tarballs.34293430More details on gitweb?34313432Write a chapter on using plumbing and writing scripts.