1Git User Manual 2_______________ 3 4Git is a fast distributed revision control system. 5 6This manual is designed to be readable by someone with basic UNIX 7command-line skills, but no previous knowledge of Git. 8 9<<repositories-and-branches>> and <<exploring-git-history>> explain how 10to fetch and study a project using git--read these chapters to learn how 11to build and test a particular version of a software project, search for 12regressions, and so on. 13 14People needing to do actual development will also want to read 15<<Developing-With-git>> and <<sharing-development>>. 16 17Further chapters cover more specialized topics. 18 19Comprehensive reference documentation is available through the man 20pages, or linkgit:git-help[1] command. For example, for the command 21`git clone <repo>`, you can either use: 22 23------------------------------------------------ 24$ man git-clone 25------------------------------------------------ 26 27or: 28 29------------------------------------------------ 30$ git help clone 31------------------------------------------------ 32 33With the latter, you can use the manual viewer of your choice; see 34linkgit:git-help[1] for more information. 35 36See also <<git-quick-start>> for a brief overview of Git commands, 37without any explanation. 38 39Finally, see <<todo>> for ways that you can help make this manual more 40complete. 41 42 43[[repositories-and-branches]] 44Repositories and Branches 45========================= 46 47[[how-to-get-a-git-repository]] 48How to get a Git repository 49--------------------------- 50 51It will be useful to have a Git repository to experiment with as you 52read this manual. 53 54The best way to get one is by using the linkgit:git-clone[1] command to 55download a copy of an existing repository. If you don't already have a 56project in mind, here are some interesting examples: 57 58------------------------------------------------ 59 # Git itself (approx. 40MB download): 60$ git clone git://git.kernel.org/pub/scm/git/git.git 61 # the Linux kernel (approx. 640MB download): 62$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 63------------------------------------------------ 64 65The initial clone may be time-consuming for a large project, but you 66will only need to clone once. 67 68The clone command creates a new directory named after the project 69(`git` or `linux` in the examples above). After you cd into this 70directory, you will see that it contains a copy of the project files, 71called the <<def_working_tree,working tree>>, together with a special 72top-level directory named `.git`, which contains all the information 73about the history of the project. 74 75[[how-to-check-out]] 76How to check out a different version of a project 77------------------------------------------------- 78 79Git is best thought of as a tool for storing the history of a collection 80of files. It stores the history as a compressed collection of 81interrelated snapshots of the project's contents. In Git each such 82version is called a <<def_commit,commit>>. 83 84Those snapshots aren't necessarily all arranged in a single line from 85oldest to newest; instead, work may simultaneously proceed along 86parallel lines of development, called <<def_branch,branches>>, which may 87merge and diverge. 88 89A single Git repository can track development on multiple branches. It 90does this by keeping a list of <<def_head,heads>> which reference the 91latest commit on each branch; the linkgit:git-branch[1] command shows 92you the list of branch heads: 93 94------------------------------------------------ 95$ git branch 96* master 97------------------------------------------------ 98 99A freshly cloned repository contains a single branch head, by default 100named "master", with the working directory initialized to the state of 101the project referred to by that branch head. 102 103Most projects also use <<def_tag,tags>>. Tags, like heads, are 104references into the project's history, and can be listed using the 105linkgit:git-tag[1] command: 106 107------------------------------------------------ 108$ git tag -l 109v2.6.11 110v2.6.11-tree 111v2.6.12 112v2.6.12-rc2 113v2.6.12-rc3 114v2.6.12-rc4 115v2.6.12-rc5 116v2.6.12-rc6 117v2.6.13 118... 119------------------------------------------------ 120 121Tags are expected to always point at the same version of a project, 122while heads are expected to advance as development progresses. 123 124Create a new branch head pointing to one of these versions and check it 125out using linkgit:git-checkout[1]: 126 127------------------------------------------------ 128$ git checkout -b new v2.6.13 129------------------------------------------------ 130 131The working directory then reflects the contents that the project had 132when it was tagged v2.6.13, and linkgit:git-branch[1] shows two 133branches, with an asterisk marking the currently checked-out branch: 134 135------------------------------------------------ 136$ git branch 137 master 138* new 139------------------------------------------------ 140 141If you decide that you'd rather see version 2.6.17, you can modify 142the current branch to point at v2.6.17 instead, with 143 144------------------------------------------------ 145$ git reset --hard v2.6.17 146------------------------------------------------ 147 148Note that if the current branch head was your only reference to a 149particular point in history, then resetting that branch may leave you 150with no way to find the history it used to point to; so use this command 151carefully. 152 153[[understanding-commits]] 154Understanding History: Commits 155------------------------------ 156 157Every change in the history of a project is represented by a commit. 158The linkgit:git-show[1] command shows the most recent commit on the 159current branch: 160 161------------------------------------------------ 162$ git show 163commit 17cf781661e6d38f737f15f53ab552f1e95960d7 164Author: Linus Torvalds <torvalds@ppc970.osdl.org.(none)> 165Date: Tue Apr 19 14:11:06 2005 -0700 166 167 Remove duplicate getenv(DB_ENVIRONMENT) call 168 169 Noted by Tony Luck. 170 171diff --git a/init-db.c b/init-db.c 172index 65898fa..b002dc6 100644 173--- a/init-db.c 174+++ b/init-db.c 175@@ -7,7 +7,7 @@ 176 177 int main(int argc, char **argv) 178 { 179- char *sha1_dir = getenv(DB_ENVIRONMENT), *path; 180+ char *sha1_dir, *path; 181 int len, i; 182 183 if (mkdir(".git", 0755) < 0) { 184------------------------------------------------ 185 186As you can see, a commit shows who made the latest change, what they 187did, and why. 188 189Every commit has a 40-hexdigit id, sometimes called the "object name" or the 190"SHA-1 id", shown on the first line of the `git show` output. You can usually 191refer to a commit by a shorter name, such as a tag or a branch name, but this 192longer name can also be useful. Most importantly, it is a globally unique 193name for this commit: so if you tell somebody else the object name (for 194example in email), then you are guaranteed that name will refer to the same 195commit in their repository that it does in yours (assuming their repository 196has that commit at all). Since the object name is computed as a hash over the 197contents of the commit, you are guaranteed that the commit can never change 198without its name also changing. 199 200In fact, in <<git-concepts>> we shall see that everything stored in Git 201history, including file data and directory contents, is stored in an object 202with a name that is a hash of its contents. 203 204[[understanding-reachability]] 205Understanding history: commits, parents, and reachability 206~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 207 208Every commit (except the very first commit in a project) also has a 209parent commit which shows what happened before this commit. 210Following the chain of parents will eventually take you back to the 211beginning of the project. 212 213However, the commits do not form a simple list; Git allows lines of 214development to diverge and then reconverge, and the point where two 215lines of development reconverge is called a "merge". The commit 216representing a merge can therefore have more than one parent, with 217each parent representing the most recent commit on one of the lines 218of development leading to that point. 219 220The best way to see how this works is using the linkgit:gitk[1] 221command; running gitk now on a Git repository and looking for merge 222commits will help understand how Git organizes history. 223 224In the following, we say that commit X is "reachable" from commit Y 225if commit X is an ancestor of commit Y. Equivalently, you could say 226that Y is a descendant of X, or that there is a chain of parents 227leading from commit Y to commit X. 228 229[[history-diagrams]] 230Understanding history: History diagrams 231~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 232 233We will sometimes represent Git history using diagrams like the one 234below. Commits are shown as "o", and the links between them with 235lines drawn with - / and \. Time goes left to right: 236 237 238................................................ 239 o--o--o <-- Branch A 240 / 241 o--o--o <-- master 242 \ 243 o--o--o <-- Branch B 244................................................ 245 246If we need to talk about a particular commit, the character "o" may 247be replaced with another letter or number. 248 249[[what-is-a-branch]] 250Understanding history: What is a branch? 251~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 252 253When we need to be precise, we will use the word "branch" to mean a line 254of development, and "branch head" (or just "head") to mean a reference 255to the most recent commit on a branch. In the example above, the branch 256head named "A" is a pointer to one particular commit, but we refer to 257the line of three commits leading up to that point as all being part of 258"branch A". 259 260However, when no confusion will result, we often just use the term 261"branch" both for branches and for branch heads. 262 263[[manipulating-branches]] 264Manipulating branches 265--------------------- 266 267Creating, deleting, and modifying branches is quick and easy; here's 268a summary of the commands: 269 270`git branch`:: 271 list all branches 272`git branch <branch>`:: 273 create a new branch named `<branch>`, referencing the same 274 point in history as the current branch 275`git branch <branch> <start-point>`:: 276 create a new branch named `<branch>`, referencing 277 `<start-point>`, which may be specified any way you like, 278 including using a branch name or a tag name 279`git branch -d <branch>`:: 280 delete the branch `<branch>`; if the branch you are deleting 281 points to a commit which is not reachable from the current 282 branch, this command will fail with a warning. 283`git branch -D <branch>`:: 284 even if the branch points to a commit not reachable 285 from the current branch, you may know that that commit 286 is still reachable from some other branch or tag. In that 287 case it is safe to use this command to force Git to delete 288 the branch. 289`git checkout <branch>`:: 290 make the current branch `<branch>`, updating the working 291 directory to reflect the version referenced by `<branch>` 292`git checkout -b <new> <start-point>`:: 293 create a new branch `<new>` referencing `<start-point>`, and 294 check it out. 295 296The special symbol "HEAD" can always be used to refer to the current 297branch. In fact, Git uses a file named `HEAD` in the `.git` directory 298to remember which branch is current: 299 300------------------------------------------------ 301$ cat .git/HEAD 302ref: refs/heads/master 303------------------------------------------------ 304 305[[detached-head]] 306Examining an old version without creating a new branch 307------------------------------------------------------ 308 309The `git checkout` command normally expects a branch head, but will also 310accept an arbitrary commit; for example, you can check out the commit 311referenced by a tag: 312 313------------------------------------------------ 314$ git checkout v2.6.17 315Note: checking out 'v2.6.17'. 316 317You are in 'detached HEAD' state. You can look around, make experimental 318changes and commit them, and you can discard any commits you make in this 319state without impacting any branches by performing another checkout. 320 321If you want to create a new branch to retain commits you create, you may 322do so (now or later) by using -b with the checkout command again. Example: 323 324 git checkout -b new_branch_name 325 326HEAD is now at 427abfa... Linux v2.6.17 327------------------------------------------------ 328 329The HEAD then refers to the SHA-1 of the commit instead of to a branch, 330and git branch shows that you are no longer on a branch: 331 332------------------------------------------------ 333$ cat .git/HEAD 334427abfa28afedffadfca9dd8b067eb6d36bac53f 335$ git branch 336* (detached from v2.6.17) 337 master 338------------------------------------------------ 339 340In this case we say that the HEAD is "detached". 341 342This is an easy way to check out a particular version without having to 343make up a name for the new branch. You can still create a new branch 344(or tag) for this version later if you decide to. 345 346[[examining-remote-branches]] 347Examining branches from a remote repository 348------------------------------------------- 349 350The "master" branch that was created at the time you cloned is a copy 351of the HEAD in the repository that you cloned from. That repository 352may also have had other branches, though, and your local repository 353keeps branches which track each of those remote branches, called 354remote-tracking branches, which you 355can view using the `-r` option to linkgit:git-branch[1]: 356 357------------------------------------------------ 358$ git branch -r 359 origin/HEAD 360 origin/html 361 origin/maint 362 origin/man 363 origin/master 364 origin/next 365 origin/pu 366 origin/todo 367------------------------------------------------ 368 369In this example, "origin" is called a remote repository, or "remote" 370for short. The branches of this repository are called "remote 371branches" from our point of view. The remote-tracking branches listed 372above were created based on the remote branches at clone time and will 373be updated by `git fetch` (hence `git pull`) and `git push`. See 374<<Updating-a-repository-With-git-fetch>> for details. 375 376You might want to build on one of these remote-tracking branches 377on a branch of your own, just as you would for a tag: 378 379------------------------------------------------ 380$ git checkout -b my-todo-copy origin/todo 381------------------------------------------------ 382 383You can also check out `origin/todo` directly to examine it or 384write a one-off patch. See <<detached-head,detached head>>. 385 386Note that the name "origin" is just the name that Git uses by default 387to refer to the repository that you cloned from. 388 389[[how-git-stores-references]] 390Naming branches, tags, and other references 391------------------------------------------- 392 393Branches, remote-tracking branches, and tags are all references to 394commits. All references are named with a slash-separated path name 395starting with `refs`; the names we've been using so far are actually 396shorthand: 397 398 - The branch `test` is short for `refs/heads/test`. 399 - The tag `v2.6.18` is short for `refs/tags/v2.6.18`. 400 - `origin/master` is short for `refs/remotes/origin/master`. 401 402The full name is occasionally useful if, for example, there ever 403exists a tag and a branch with the same name. 404 405(Newly created refs are actually stored in the `.git/refs` directory, 406under the path given by their name. However, for efficiency reasons 407they may also be packed together in a single file; see 408linkgit:git-pack-refs[1]). 409 410As another useful shortcut, the "HEAD" of a repository can be referred 411to just using the name of that repository. So, for example, "origin" 412is usually a shortcut for the HEAD branch in the repository "origin". 413 414For the complete list of paths which Git checks for references, and 415the order it uses to decide which to choose when there are multiple 416references with the same shorthand name, see the "SPECIFYING 417REVISIONS" section of linkgit:gitrevisions[7]. 418 419[[Updating-a-repository-With-git-fetch]] 420Updating a repository with git fetch 421------------------------------------ 422 423Eventually the developer cloned from will do additional work in her 424repository, creating new commits and advancing the branches to point 425at the new commits. 426 427The command `git fetch`, with no arguments, will update all of the 428remote-tracking branches to the latest version found in her 429repository. It will not touch any of your own branches--not even the 430"master" branch that was created for you on clone. 431 432[[fetching-branches]] 433Fetching branches from other repositories 434----------------------------------------- 435 436You can also track branches from repositories other than the one you 437cloned from, using linkgit:git-remote[1]: 438 439------------------------------------------------- 440$ git remote add staging git://git.kernel.org/.../gregkh/staging.git 441$ git fetch staging 442... 443From git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging 444 * [new branch] master -> staging/master 445 * [new branch] staging-linus -> staging/staging-linus 446 * [new branch] staging-next -> staging/staging-next 447------------------------------------------------- 448 449New remote-tracking branches will be stored under the shorthand name 450that you gave `git remote add`, in this case `staging`: 451 452------------------------------------------------- 453$ git branch -r 454 origin/HEAD -> origin/master 455 origin/master 456 staging/master 457 staging/staging-linus 458 staging/staging-next 459------------------------------------------------- 460 461If you run `git fetch <remote>` later, the remote-tracking branches 462for the named `<remote>` will be updated. 463 464If you examine the file `.git/config`, you will see that Git has added 465a new stanza: 466 467------------------------------------------------- 468$ cat .git/config 469... 470[remote "staging"] 471 url = git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging.git 472 fetch = +refs/heads/*:refs/remotes/staging/* 473... 474------------------------------------------------- 475 476This is what causes Git to track the remote's branches; you may modify 477or delete these configuration options by editing `.git/config` with a 478text editor. (See the "CONFIGURATION FILE" section of 479linkgit:git-config[1] for details.) 480 481[[exploring-git-history]] 482Exploring Git history 483===================== 484 485Git is best thought of as a tool for storing the history of a 486collection of files. It does this by storing compressed snapshots of 487the contents of a file hierarchy, together with "commits" which show 488the relationships between these snapshots. 489 490Git provides extremely flexible and fast tools for exploring the 491history of a project. 492 493We start with one specialized tool that is useful for finding the 494commit that introduced a bug into a project. 495 496[[using-bisect]] 497How to use bisect to find a regression 498-------------------------------------- 499 500Suppose version 2.6.18 of your project worked, but the version at 501"master" crashes. Sometimes the best way to find the cause of such a 502regression is to perform a brute-force search through the project's 503history to find the particular commit that caused the problem. The 504linkgit:git-bisect[1] command can help you do this: 505 506------------------------------------------------- 507$ git bisect start 508$ git bisect good v2.6.18 509$ git bisect bad master 510Bisecting: 3537 revisions left to test after this 511[65934a9a028b88e83e2b0f8b36618fe503349f8e] BLOCK: Make USB storage depend on SCSI rather than selecting it [try #6] 512------------------------------------------------- 513 514If you run `git branch` at this point, you'll see that Git has 515temporarily moved you in "(no branch)". HEAD is now detached from any 516branch and points directly to a commit (with commit id 65934...) that 517is reachable from "master" but not from v2.6.18. Compile and test it, 518and see whether it crashes. Assume it does crash. Then: 519 520------------------------------------------------- 521$ git bisect bad 522Bisecting: 1769 revisions left to test after this 523[7eff82c8b1511017ae605f0c99ac275a7e21b867] i2c-core: Drop useless bitmaskings 524------------------------------------------------- 525 526checks out an older version. Continue like this, telling Git at each 527stage whether the version it gives you is good or bad, and notice 528that the number of revisions left to test is cut approximately in 529half each time. 530 531After about 13 tests (in this case), it will output the commit id of 532the guilty commit. You can then examine the commit with 533linkgit:git-show[1], find out who wrote it, and mail them your bug 534report with the commit id. Finally, run 535 536------------------------------------------------- 537$ git bisect reset 538------------------------------------------------- 539 540to return you to the branch you were on before. 541 542Note that the version which `git bisect` checks out for you at each 543point is just a suggestion, and you're free to try a different 544version if you think it would be a good idea. For example, 545occasionally you may land on a commit that broke something unrelated; 546run 547 548------------------------------------------------- 549$ git bisect visualize 550------------------------------------------------- 551 552which will run gitk and label the commit it chose with a marker that 553says "bisect". Choose a safe-looking commit nearby, note its commit 554id, and check it out with: 555 556------------------------------------------------- 557$ git reset --hard fb47ddb2db... 558------------------------------------------------- 559 560then test, run `bisect good` or `bisect bad` as appropriate, and 561continue. 562 563Instead of `git bisect visualize` and then `git reset --hard 564fb47ddb2db...`, you might just want to tell Git that you want to skip 565the current commit: 566 567------------------------------------------------- 568$ git bisect skip 569------------------------------------------------- 570 571In this case, though, Git may not eventually be able to tell the first 572bad one between some first skipped commits and a later bad commit. 573 574There are also ways to automate the bisecting process if you have a 575test script that can tell a good from a bad commit. See 576linkgit:git-bisect[1] for more information about this and other `git 577bisect` features. 578 579[[naming-commits]] 580Naming commits 581-------------- 582 583We have seen several ways of naming commits already: 584 585 - 40-hexdigit object name 586 - branch name: refers to the commit at the head of the given 587 branch 588 - tag name: refers to the commit pointed to by the given tag 589 (we've seen branches and tags are special cases of 590 <<how-git-stores-references,references>>). 591 - HEAD: refers to the head of the current branch 592 593There are many more; see the "SPECIFYING REVISIONS" section of the 594linkgit:gitrevisions[7] man page for the complete list of ways to 595name revisions. Some examples: 596 597------------------------------------------------- 598$ git show fb47ddb2 # the first few characters of the object name 599 # are usually enough to specify it uniquely 600$ git show HEAD^ # the parent of the HEAD commit 601$ git show HEAD^^ # the grandparent 602$ git show HEAD~4 # the great-great-grandparent 603------------------------------------------------- 604 605Recall that merge commits may have more than one parent; by default, 606`^` and `~` follow the first parent listed in the commit, but you can 607also choose: 608 609------------------------------------------------- 610$ git show HEAD^1 # show the first parent of HEAD 611$ git show HEAD^2 # show the second parent of HEAD 612------------------------------------------------- 613 614In addition to HEAD, there are several other special names for 615commits: 616 617Merges (to be discussed later), as well as operations such as 618`git reset`, which change the currently checked-out commit, generally 619set ORIG_HEAD to the value HEAD had before the current operation. 620 621The `git fetch` operation always stores the head of the last fetched 622branch in FETCH_HEAD. For example, if you run `git fetch` without 623specifying a local branch as the target of the operation 624 625------------------------------------------------- 626$ git fetch git://example.com/proj.git theirbranch 627------------------------------------------------- 628 629the fetched commits will still be available from FETCH_HEAD. 630 631When we discuss merges we'll also see the special name MERGE_HEAD, 632which refers to the other branch that we're merging in to the current 633branch. 634 635The linkgit:git-rev-parse[1] command is a low-level command that is 636occasionally useful for translating some name for a commit to the object 637name for that commit: 638 639------------------------------------------------- 640$ git rev-parse origin 641e05db0fd4f31dde7005f075a84f96b360d05984b 642------------------------------------------------- 643 644[[creating-tags]] 645Creating tags 646------------- 647 648We can also create a tag to refer to a particular commit; after 649running 650 651------------------------------------------------- 652$ git tag stable-1 1b2e1d63ff 653------------------------------------------------- 654 655You can use `stable-1` to refer to the commit 1b2e1d63ff. 656 657This creates a "lightweight" tag. If you would also like to include a 658comment with the tag, and possibly sign it cryptographically, then you 659should create a tag object instead; see the linkgit:git-tag[1] man page 660for details. 661 662[[browsing-revisions]] 663Browsing revisions 664------------------ 665 666The linkgit:git-log[1] command can show lists of commits. On its 667own, it shows all commits reachable from the parent commit; but you 668can also make more specific requests: 669 670------------------------------------------------- 671$ git log v2.5.. # commits since (not reachable from) v2.5 672$ git log test..master # commits reachable from master but not test 673$ git log master..test # ...reachable from test but not master 674$ git log master...test # ...reachable from either test or master, 675 # but not both 676$ git log --since="2 weeks ago" # commits from the last 2 weeks 677$ git log Makefile # commits which modify Makefile 678$ git log fs/ # ... which modify any file under fs/ 679$ git log -S'foo()' # commits which add or remove any file data 680 # matching the string 'foo()' 681------------------------------------------------- 682 683And of course you can combine all of these; the following finds 684commits since v2.5 which touch the `Makefile` or any file under `fs`: 685 686------------------------------------------------- 687$ git log v2.5.. Makefile fs/ 688------------------------------------------------- 689 690You can also ask git log to show patches: 691 692------------------------------------------------- 693$ git log -p 694------------------------------------------------- 695 696See the `--pretty` option in the linkgit:git-log[1] man page for more 697display options. 698 699Note that git log starts with the most recent commit and works 700backwards through the parents; however, since Git history can contain 701multiple independent lines of development, the particular order that 702commits are listed in may be somewhat arbitrary. 703 704[[generating-diffs]] 705Generating diffs 706---------------- 707 708You can generate diffs between any two versions using 709linkgit:git-diff[1]: 710 711------------------------------------------------- 712$ git diff master..test 713------------------------------------------------- 714 715That will produce the diff between the tips of the two branches. If 716you'd prefer to find the diff from their common ancestor to test, you 717can use three dots instead of two: 718 719------------------------------------------------- 720$ git diff master...test 721------------------------------------------------- 722 723Sometimes what you want instead is a set of patches; for this you can 724use linkgit:git-format-patch[1]: 725 726------------------------------------------------- 727$ git format-patch master..test 728------------------------------------------------- 729 730will generate a file with a patch for each commit reachable from test 731but not from master. 732 733[[viewing-old-file-versions]] 734Viewing old file versions 735------------------------- 736 737You can always view an old version of a file by just checking out the 738correct revision first. But sometimes it is more convenient to be 739able to view an old version of a single file without checking 740anything out; this command does that: 741 742------------------------------------------------- 743$ git show v2.5:fs/locks.c 744------------------------------------------------- 745 746Before the colon may be anything that names a commit, and after it 747may be any path to a file tracked by Git. 748 749[[history-examples]] 750Examples 751-------- 752 753[[counting-commits-on-a-branch]] 754Counting the number of commits on a branch 755~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 756 757Suppose you want to know how many commits you've made on `mybranch` 758since it diverged from `origin`: 759 760------------------------------------------------- 761$ git log --pretty=oneline origin..mybranch | wc -l 762------------------------------------------------- 763 764Alternatively, you may often see this sort of thing done with the 765lower-level command linkgit:git-rev-list[1], which just lists the SHA-1's 766of all the given commits: 767 768------------------------------------------------- 769$ git rev-list origin..mybranch | wc -l 770------------------------------------------------- 771 772[[checking-for-equal-branches]] 773Check whether two branches point at the same history 774~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 775 776Suppose you want to check whether two branches point at the same point 777in history. 778 779------------------------------------------------- 780$ git diff origin..master 781------------------------------------------------- 782 783will tell you whether the contents of the project are the same at the 784two branches; in theory, however, it's possible that the same project 785contents could have been arrived at by two different historical 786routes. You could compare the object names: 787 788------------------------------------------------- 789$ git rev-list origin 790e05db0fd4f31dde7005f075a84f96b360d05984b 791$ git rev-list master 792e05db0fd4f31dde7005f075a84f96b360d05984b 793------------------------------------------------- 794 795Or you could recall that the `...` operator selects all commits 796reachable from either one reference or the other but not 797both; so 798 799------------------------------------------------- 800$ git log origin...master 801------------------------------------------------- 802 803will return no commits when the two branches are equal. 804 805[[finding-tagged-descendants]] 806Find first tagged version including a given fix 807~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 808 809Suppose you know that the commit e05db0fd fixed a certain problem. 810You'd like to find the earliest tagged release that contains that 811fix. 812 813Of course, there may be more than one answer--if the history branched 814after commit e05db0fd, then there could be multiple "earliest" tagged 815releases. 816 817You could just visually inspect the commits since e05db0fd: 818 819------------------------------------------------- 820$ gitk e05db0fd.. 821------------------------------------------------- 822 823or you can use linkgit:git-name-rev[1], which will give the commit a 824name based on any tag it finds pointing to one of the commit's 825descendants: 826 827------------------------------------------------- 828$ git name-rev --tags e05db0fd 829e05db0fd tags/v1.5.0-rc1^0~23 830------------------------------------------------- 831 832The linkgit:git-describe[1] command does the opposite, naming the 833revision using a tag on which the given commit is based: 834 835------------------------------------------------- 836$ git describe e05db0fd 837v1.5.0-rc0-260-ge05db0f 838------------------------------------------------- 839 840but that may sometimes help you guess which tags might come after the 841given commit. 842 843If you just want to verify whether a given tagged version contains a 844given commit, you could use linkgit:git-merge-base[1]: 845 846------------------------------------------------- 847$ git merge-base e05db0fd v1.5.0-rc1 848e05db0fd4f31dde7005f075a84f96b360d05984b 849------------------------------------------------- 850 851The merge-base command finds a common ancestor of the given commits, 852and always returns one or the other in the case where one is a 853descendant of the other; so the above output shows that e05db0fd 854actually is an ancestor of v1.5.0-rc1. 855 856Alternatively, note that 857 858------------------------------------------------- 859$ git log v1.5.0-rc1..e05db0fd 860------------------------------------------------- 861 862will produce empty output if and only if v1.5.0-rc1 includes e05db0fd, 863because it outputs only commits that are not reachable from v1.5.0-rc1. 864 865As yet another alternative, the linkgit:git-show-branch[1] command lists 866the commits reachable from its arguments with a display on the left-hand 867side that indicates which arguments that commit is reachable from. 868So, if you run something like 869 870------------------------------------------------- 871$ git show-branch e05db0fd v1.5.0-rc0 v1.5.0-rc1 v1.5.0-rc2 872! [e05db0fd] Fix warnings in sha1_file.c - use C99 printf format if 873available 874 ! [v1.5.0-rc0] GIT v1.5.0 preview 875 ! [v1.5.0-rc1] GIT v1.5.0-rc1 876 ! [v1.5.0-rc2] GIT v1.5.0-rc2 877... 878------------------------------------------------- 879 880then a line like 881 882------------------------------------------------- 883+ ++ [e05db0fd] Fix warnings in sha1_file.c - use C99 printf format if 884available 885------------------------------------------------- 886 887shows that e05db0fd is reachable from itself, from v1.5.0-rc1, 888and from v1.5.0-rc2, and not from v1.5.0-rc0. 889 890[[showing-commits-unique-to-a-branch]] 891Showing commits unique to a given branch 892~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 893 894Suppose you would like to see all the commits reachable from the branch 895head named `master` but not from any other head in your repository. 896 897We can list all the heads in this repository with 898linkgit:git-show-ref[1]: 899 900------------------------------------------------- 901$ git show-ref --heads 902bf62196b5e363d73353a9dcf094c59595f3153b7 refs/heads/core-tutorial 903db768d5504c1bb46f63ee9d6e1772bd047e05bf9 refs/heads/maint 904a07157ac624b2524a059a3414e99f6f44bebc1e7 refs/heads/master 90524dbc180ea14dc1aebe09f14c8ecf32010690627 refs/heads/tutorial-2 9061e87486ae06626c2f31eaa63d26fc0fd646c8af2 refs/heads/tutorial-fixes 907------------------------------------------------- 908 909We can get just the branch-head names, and remove `master`, with 910the help of the standard utilities cut and grep: 911 912------------------------------------------------- 913$ git show-ref --heads | cut -d' ' -f2 | grep -v '^refs/heads/master' 914refs/heads/core-tutorial 915refs/heads/maint 916refs/heads/tutorial-2 917refs/heads/tutorial-fixes 918------------------------------------------------- 919 920And then we can ask to see all the commits reachable from master 921but not from these other heads: 922 923------------------------------------------------- 924$ gitk master --not $( git show-ref --heads | cut -d' ' -f2 | 925 grep -v '^refs/heads/master' ) 926------------------------------------------------- 927 928Obviously, endless variations are possible; for example, to see all 929commits reachable from some head but not from any tag in the repository: 930 931------------------------------------------------- 932$ gitk $( git show-ref --heads ) --not $( git show-ref --tags ) 933------------------------------------------------- 934 935(See linkgit:gitrevisions[7] for explanations of commit-selecting 936syntax such as `--not`.) 937 938[[making-a-release]] 939Creating a changelog and tarball for a software release 940~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 941 942The linkgit:git-archive[1] command can create a tar or zip archive from 943any version of a project; for example: 944 945------------------------------------------------- 946$ git archive -o latest.tar.gz --prefix=project/ HEAD 947------------------------------------------------- 948 949will use HEAD to produce a gzipped tar archive in which each filename 950is preceded by `project/`. The output file format is inferred from 951the output file extension if possible, see linkgit:git-archive[1] for 952details. 953 954Versions of Git older than 1.7.7 don't know about the `tar.gz` format, 955you'll need to use gzip explicitly: 956 957------------------------------------------------- 958$ git archive --format=tar --prefix=project/ HEAD | gzip >latest.tar.gz 959------------------------------------------------- 960 961If you're releasing a new version of a software project, you may want 962to simultaneously make a changelog to include in the release 963announcement. 964 965Linus Torvalds, for example, makes new kernel releases by tagging them, 966then running: 967 968------------------------------------------------- 969$ release-script 2.6.12 2.6.13-rc6 2.6.13-rc7 970------------------------------------------------- 971 972where release-script is a shell script that looks like: 973 974------------------------------------------------- 975#!/bin/sh 976stable="$1" 977last="$2" 978new="$3" 979echo "# git tag v$new" 980echo "git archive --prefix=linux-$new/ v$new | gzip -9 > ../linux-$new.tar.gz" 981echo "git diff v$stable v$new | gzip -9 > ../patch-$new.gz" 982echo "git log --no-merges v$new ^v$last > ../ChangeLog-$new" 983echo "git shortlog --no-merges v$new ^v$last > ../ShortLog" 984echo "git diff --stat --summary -M v$last v$new > ../diffstat-$new" 985------------------------------------------------- 986 987and then he just cut-and-pastes the output commands after verifying that 988they look OK. 989 990[[Finding-commits-With-given-Content]] 991Finding commits referencing a file with given content 992~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 993 994Somebody hands you a copy of a file, and asks which commits modified a 995file such that it contained the given content either before or after the 996commit. You can find out with this: 997 998------------------------------------------------- 999$ git log --raw --abbrev=40 --pretty=oneline |1000 grep -B 1 `git hash-object filename`1001-------------------------------------------------10021003Figuring out why this works is left as an exercise to the (advanced)1004student. The linkgit:git-log[1], linkgit:git-diff-tree[1], and1005linkgit:git-hash-object[1] man pages may prove helpful.10061007[[Developing-With-git]]1008Developing with Git1009===================10101011[[telling-git-your-name]]1012Telling Git your name1013---------------------10141015Before creating any commits, you should introduce yourself to Git.1016The easiest way to do so is to use linkgit:git-config[1]:10171018------------------------------------------------1019$ git config --global user.name 'Your Name Comes Here'1020$ git config --global user.email 'you@yourdomain.example.com'1021------------------------------------------------10221023Which will add the following to a file named `.gitconfig` in your1024home directory:10251026------------------------------------------------1027[user]1028 name = Your Name Comes Here1029 email = you@yourdomain.example.com1030------------------------------------------------10311032See the "CONFIGURATION FILE" section of linkgit:git-config[1] for1033details on the configuration file. The file is plain text, so you can1034also edit it with your favorite editor.103510361037[[creating-a-new-repository]]1038Creating a new repository1039-------------------------10401041Creating a new repository from scratch is very easy:10421043-------------------------------------------------1044$ mkdir project1045$ cd project1046$ git init1047-------------------------------------------------10481049If you have some initial content (say, a tarball):10501051-------------------------------------------------1052$ tar xzvf project.tar.gz1053$ cd project1054$ git init1055$ git add . # include everything below ./ in the first commit:1056$ git commit1057-------------------------------------------------10581059[[how-to-make-a-commit]]1060How to make a commit1061--------------------10621063Creating a new commit takes three steps:10641065 1. Making some changes to the working directory using your1066 favorite editor.1067 2. Telling Git about your changes.1068 3. Creating the commit using the content you told Git about1069 in step 2.10701071In practice, you can interleave and repeat steps 1 and 2 as many1072times as you want: in order to keep track of what you want committed1073at step 3, Git maintains a snapshot of the tree's contents in a1074special staging area called "the index."10751076At the beginning, the content of the index will be identical to1077that of the HEAD. The command `git diff --cached`, which shows1078the difference between the HEAD and the index, should therefore1079produce no output at that point.10801081Modifying the index is easy:10821083To update the index with the new contents of a modified file, use10841085-------------------------------------------------1086$ git add path/to/file1087-------------------------------------------------10881089To add the contents of a new file to the index, use10901091-------------------------------------------------1092$ git add path/to/file1093-------------------------------------------------10941095To remove a file from the index and from the working tree,10961097-------------------------------------------------1098$ git rm path/to/file1099-------------------------------------------------11001101After each step you can verify that11021103-------------------------------------------------1104$ git diff --cached1105-------------------------------------------------11061107always shows the difference between the HEAD and the index file--this1108is what you'd commit if you created the commit now--and that11091110-------------------------------------------------1111$ git diff1112-------------------------------------------------11131114shows the difference between the working tree and the index file.11151116Note that `git add` always adds just the current contents of a file1117to the index; further changes to the same file will be ignored unless1118you run `git add` on the file again.11191120When you're ready, just run11211122-------------------------------------------------1123$ git commit1124-------------------------------------------------11251126and Git will prompt you for a commit message and then create the new1127commit. Check to make sure it looks like what you expected with11281129-------------------------------------------------1130$ git show1131-------------------------------------------------11321133As a special shortcut,11341135-------------------------------------------------1136$ git commit -a1137-------------------------------------------------11381139will update the index with any files that you've modified or removed1140and create a commit, all in one step.11411142A number of commands are useful for keeping track of what you're1143about to commit:11441145-------------------------------------------------1146$ git diff --cached # difference between HEAD and the index; what1147 # would be committed if you ran "commit" now.1148$ git diff # difference between the index file and your1149 # working directory; changes that would not1150 # be included if you ran "commit" now.1151$ git diff HEAD # difference between HEAD and working tree; what1152 # would be committed if you ran "commit -a" now.1153$ git status # a brief per-file summary of the above.1154-------------------------------------------------11551156You can also use linkgit:git-gui[1] to create commits, view changes in1157the index and the working tree files, and individually select diff hunks1158for inclusion in the index (by right-clicking on the diff hunk and1159choosing "Stage Hunk For Commit").11601161[[creating-good-commit-messages]]1162Creating good commit messages1163-----------------------------11641165Though not required, it's a good idea to begin the commit message1166with a single short (less than 50 character) line summarizing the1167change, followed by a blank line and then a more thorough1168description. The text up to the first blank line in a commit1169message is treated as the commit title, and that title is used1170throughout Git. For example, linkgit:git-format-patch[1] turns a1171commit into email, and it uses the title on the Subject line and the1172rest of the commit in the body.117311741175[[ignoring-files]]1176Ignoring files1177--------------11781179A project will often generate files that you do 'not' want to track with Git.1180This typically includes files generated by a build process or temporary1181backup files made by your editor. Of course, 'not' tracking files with Git1182is just a matter of 'not' calling `git add` on them. But it quickly becomes1183annoying to have these untracked files lying around; e.g. they make1184`git add .` practically useless, and they keep showing up in the output of1185`git status`.11861187You can tell Git to ignore certain files by creating a file called1188`.gitignore` in the top level of your working directory, with contents1189such as:11901191-------------------------------------------------1192# Lines starting with '#' are considered comments.1193# Ignore any file named foo.txt.1194foo.txt1195# Ignore (generated) html files,1196*.html1197# except foo.html which is maintained by hand.1198!foo.html1199# Ignore objects and archives.1200*.[oa]1201-------------------------------------------------12021203See linkgit:gitignore[5] for a detailed explanation of the syntax. You can1204also place .gitignore files in other directories in your working tree, and they1205will apply to those directories and their subdirectories. The `.gitignore`1206files can be added to your repository like any other files (just run `git add1207.gitignore` and `git commit`, as usual), which is convenient when the exclude1208patterns (such as patterns matching build output files) would also make sense1209for other users who clone your repository.12101211If you wish the exclude patterns to affect only certain repositories1212(instead of every repository for a given project), you may instead put1213them in a file in your repository named `.git/info/exclude`, or in any1214file specified by the `core.excludesfile` configuration variable.1215Some Git commands can also take exclude patterns directly on the1216command line. See linkgit:gitignore[5] for the details.12171218[[how-to-merge]]1219How to merge1220------------12211222You can rejoin two diverging branches of development using1223linkgit:git-merge[1]:12241225-------------------------------------------------1226$ git merge branchname1227-------------------------------------------------12281229merges the development in the branch `branchname` into the current1230branch.12311232A merge is made by combining the changes made in `branchname` and the1233changes made up to the latest commit in your current branch since1234their histories forked. The work tree is overwritten by the result of1235the merge when this combining is done cleanly, or overwritten by a1236half-merged results when this combining results in conflicts.1237Therefore, if you have uncommitted changes touching the same files as1238the ones impacted by the merge, Git will refuse to proceed. Most of1239the time, you will want to commit your changes before you can merge,1240and if you don't, then linkgit:git-stash[1] can take these changes1241away while you're doing the merge, and reapply them afterwards.12421243If the changes are independent enough, Git will automatically complete1244the merge and commit the result (or reuse an existing commit in case1245of <<fast-forwards,fast-forward>>, see below). On the other hand,1246if there are conflicts--for example, if the same file is1247modified in two different ways in the remote branch and the local1248branch--then you are warned; the output may look something like this:12491250-------------------------------------------------1251$ git merge next1252 100% (4/4) done1253Auto-merged file.txt1254CONFLICT (content): Merge conflict in file.txt1255Automatic merge failed; fix conflicts and then commit the result.1256-------------------------------------------------12571258Conflict markers are left in the problematic files, and after1259you resolve the conflicts manually, you can update the index1260with the contents and run Git commit, as you normally would when1261creating a new file.12621263If you examine the resulting commit using gitk, you will see that it1264has two parents, one pointing to the top of the current branch, and1265one to the top of the other branch.12661267[[resolving-a-merge]]1268Resolving a merge1269-----------------12701271When a merge isn't resolved automatically, Git leaves the index and1272the working tree in a special state that gives you all the1273information you need to help resolve the merge.12741275Files with conflicts are marked specially in the index, so until you1276resolve the problem and update the index, linkgit:git-commit[1] will1277fail:12781279-------------------------------------------------1280$ git commit1281file.txt: needs merge1282-------------------------------------------------12831284Also, linkgit:git-status[1] will list those files as "unmerged", and the1285files with conflicts will have conflict markers added, like this:12861287-------------------------------------------------1288<<<<<<< HEAD:file.txt1289Hello world1290=======1291Goodbye1292>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt1293-------------------------------------------------12941295All you need to do is edit the files to resolve the conflicts, and then12961297-------------------------------------------------1298$ git add file.txt1299$ git commit1300-------------------------------------------------13011302Note that the commit message will already be filled in for you with1303some information about the merge. Normally you can just use this1304default message unchanged, but you may add additional commentary of1305your own if desired.13061307The above is all you need to know to resolve a simple merge. But Git1308also provides more information to help resolve conflicts:13091310[[conflict-resolution]]1311Getting conflict-resolution help during a merge1312~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~13131314All of the changes that Git was able to merge automatically are1315already added to the index file, so linkgit:git-diff[1] shows only1316the conflicts. It uses an unusual syntax:13171318-------------------------------------------------1319$ git diff1320diff --cc file.txt1321index 802992c,2b60207..00000001322--- a/file.txt1323+++ b/file.txt1324@@@ -1,1 -1,1 +1,5 @@@1325++<<<<<<< HEAD:file.txt1326 +Hello world1327++=======1328+ Goodbye1329++>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt1330-------------------------------------------------13311332Recall that the commit which will be committed after we resolve this1333conflict will have two parents instead of the usual one: one parent1334will be HEAD, the tip of the current branch; the other will be the1335tip of the other branch, which is stored temporarily in MERGE_HEAD.13361337During the merge, the index holds three versions of each file. Each of1338these three "file stages" represents a different version of the file:13391340-------------------------------------------------1341$ git show :1:file.txt # the file in a common ancestor of both branches1342$ git show :2:file.txt # the version from HEAD.1343$ git show :3:file.txt # the version from MERGE_HEAD.1344-------------------------------------------------13451346When you ask linkgit:git-diff[1] to show the conflicts, it runs a1347three-way diff between the conflicted merge results in the work tree with1348stages 2 and 3 to show only hunks whose contents come from both sides,1349mixed (in other words, when a hunk's merge results come only from stage 2,1350that part is not conflicting and is not shown. Same for stage 3).13511352The diff above shows the differences between the working-tree version of1353file.txt and the stage 2 and stage 3 versions. So instead of preceding1354each line by a single `+` or `-`, it now uses two columns: the first1355column is used for differences between the first parent and the working1356directory copy, and the second for differences between the second parent1357and the working directory copy. (See the "COMBINED DIFF FORMAT" section1358of linkgit:git-diff-files[1] for a details of the format.)13591360After resolving the conflict in the obvious way (but before updating the1361index), the diff will look like:13621363-------------------------------------------------1364$ git diff1365diff --cc file.txt1366index 802992c,2b60207..00000001367--- a/file.txt1368+++ b/file.txt1369@@@ -1,1 -1,1 +1,1 @@@1370- Hello world1371 -Goodbye1372++Goodbye world1373-------------------------------------------------13741375This shows that our resolved version deleted "Hello world" from the1376first parent, deleted "Goodbye" from the second parent, and added1377"Goodbye world", which was previously absent from both.13781379Some special diff options allow diffing the working directory against1380any of these stages:13811382-------------------------------------------------1383$ git diff -1 file.txt # diff against stage 11384$ git diff --base file.txt # same as the above1385$ git diff -2 file.txt # diff against stage 21386$ git diff --ours file.txt # same as the above1387$ git diff -3 file.txt # diff against stage 31388$ git diff --theirs file.txt # same as the above.1389-------------------------------------------------13901391The linkgit:git-log[1] and linkgit:gitk[1] commands also provide special help1392for merges:13931394-------------------------------------------------1395$ git log --merge1396$ gitk --merge1397-------------------------------------------------13981399These will display all commits which exist only on HEAD or on1400MERGE_HEAD, and which touch an unmerged file.14011402You may also use linkgit:git-mergetool[1], which lets you merge the1403unmerged files using external tools such as Emacs or kdiff3.14041405Each time you resolve the conflicts in a file and update the index:14061407-------------------------------------------------1408$ git add file.txt1409-------------------------------------------------14101411the different stages of that file will be "collapsed", after which1412`git diff` will (by default) no longer show diffs for that file.14131414[[undoing-a-merge]]1415Undoing a merge1416---------------14171418If you get stuck and decide to just give up and throw the whole mess1419away, you can always return to the pre-merge state with14201421-------------------------------------------------1422$ git reset --hard HEAD1423-------------------------------------------------14241425Or, if you've already committed the merge that you want to throw away,14261427-------------------------------------------------1428$ git reset --hard ORIG_HEAD1429-------------------------------------------------14301431However, this last command can be dangerous in some cases--never1432throw away a commit you have already committed if that commit may1433itself have been merged into another branch, as doing so may confuse1434further merges.14351436[[fast-forwards]]1437Fast-forward merges1438-------------------14391440There is one special case not mentioned above, which is treated1441differently. Normally, a merge results in a merge commit, with two1442parents, one pointing at each of the two lines of development that1443were merged.14441445However, if the current branch is a descendant of the other--so every1446commit present in the one is already contained in the other--then Git1447just performs a "fast-forward"; the head of the current branch is moved1448forward to point at the head of the merged-in branch, without any new1449commits being created.14501451[[fixing-mistakes]]1452Fixing mistakes1453---------------14541455If you've messed up the working tree, but haven't yet committed your1456mistake, you can return the entire working tree to the last committed1457state with14581459-------------------------------------------------1460$ git reset --hard HEAD1461-------------------------------------------------14621463If you make a commit that you later wish you hadn't, there are two1464fundamentally different ways to fix the problem:14651466 1. You can create a new commit that undoes whatever was done1467 by the old commit. This is the correct thing if your1468 mistake has already been made public.14691470 2. You can go back and modify the old commit. You should1471 never do this if you have already made the history public;1472 Git does not normally expect the "history" of a project to1473 change, and cannot correctly perform repeated merges from1474 a branch that has had its history changed.14751476[[reverting-a-commit]]1477Fixing a mistake with a new commit1478~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~14791480Creating a new commit that reverts an earlier change is very easy;1481just pass the linkgit:git-revert[1] command a reference to the bad1482commit; for example, to revert the most recent commit:14831484-------------------------------------------------1485$ git revert HEAD1486-------------------------------------------------14871488This will create a new commit which undoes the change in HEAD. You1489will be given a chance to edit the commit message for the new commit.14901491You can also revert an earlier change, for example, the next-to-last:14921493-------------------------------------------------1494$ git revert HEAD^1495-------------------------------------------------14961497In this case Git will attempt to undo the old change while leaving1498intact any changes made since then. If more recent changes overlap1499with the changes to be reverted, then you will be asked to fix1500conflicts manually, just as in the case of <<resolving-a-merge,1501resolving a merge>>.15021503[[fixing-a-mistake-by-rewriting-history]]1504Fixing a mistake by rewriting history1505~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~15061507If the problematic commit is the most recent commit, and you have not1508yet made that commit public, then you may just1509<<undoing-a-merge,destroy it using `git reset`>>.15101511Alternatively, you1512can edit the working directory and update the index to fix your1513mistake, just as if you were going to <<how-to-make-a-commit,create a1514new commit>>, then run15151516-------------------------------------------------1517$ git commit --amend1518-------------------------------------------------15191520which will replace the old commit by a new commit incorporating your1521changes, giving you a chance to edit the old commit message first.15221523Again, you should never do this to a commit that may already have1524been merged into another branch; use linkgit:git-revert[1] instead in1525that case.15261527It is also possible to replace commits further back in the history, but1528this is an advanced topic to be left for1529<<cleaning-up-history,another chapter>>.15301531[[checkout-of-path]]1532Checking out an old version of a file1533~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~15341535In the process of undoing a previous bad change, you may find it1536useful to check out an older version of a particular file using1537linkgit:git-checkout[1]. We've used `git checkout` before to switch1538branches, but it has quite different behavior if it is given a path1539name: the command15401541-------------------------------------------------1542$ git checkout HEAD^ path/to/file1543-------------------------------------------------15441545replaces path/to/file by the contents it had in the commit HEAD^, and1546also updates the index to match. It does not change branches.15471548If you just want to look at an old version of the file, without1549modifying the working directory, you can do that with1550linkgit:git-show[1]:15511552-------------------------------------------------1553$ git show HEAD^:path/to/file1554-------------------------------------------------15551556which will display the given version of the file.15571558[[interrupted-work]]1559Temporarily setting aside work in progress1560~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~15611562While you are in the middle of working on something complicated, you1563find an unrelated but obvious and trivial bug. You would like to fix it1564before continuing. You can use linkgit:git-stash[1] to save the current1565state of your work, and after fixing the bug (or, optionally after doing1566so on a different branch and then coming back), unstash the1567work-in-progress changes.15681569------------------------------------------------1570$ git stash save "work in progress for foo feature"1571------------------------------------------------15721573This command will save your changes away to the `stash`, and1574reset your working tree and the index to match the tip of your1575current branch. Then you can make your fix as usual.15761577------------------------------------------------1578... edit and test ...1579$ git commit -a -m "blorpl: typofix"1580------------------------------------------------15811582After that, you can go back to what you were working on with1583`git stash pop`:15841585------------------------------------------------1586$ git stash pop1587------------------------------------------------158815891590[[ensuring-good-performance]]1591Ensuring good performance1592-------------------------15931594On large repositories, Git depends on compression to keep the history1595information from taking up too much space on disk or in memory. Some1596Git commands may automatically run linkgit:git-gc[1], so you don't1597have to worry about running it manually. However, compressing a large1598repository may take a while, so you may want to call `gc` explicitly1599to avoid automatic compression kicking in when it is not convenient.160016011602[[ensuring-reliability]]1603Ensuring reliability1604--------------------16051606[[checking-for-corruption]]1607Checking the repository for corruption1608~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~16091610The linkgit:git-fsck[1] command runs a number of self-consistency checks1611on the repository, and reports on any problems. This may take some1612time.16131614-------------------------------------------------1615$ git fsck1616dangling commit 7281251ddd2a61e38657c827739c57015671a6b31617dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a631618dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b51619dangling blob 218761f9d90712d37a9c5e36f406f92202db07eb1620dangling commit bf093535a34a4d35731aa2bd90fe6b176302f14f1621dangling commit 8e4bec7f2ddaa268bef999853c25755452100f8e1622dangling tree d50bb86186bf27b681d25af89d3b5b68382e40851623dangling tree b24c2473f1fd3d91352a624795be026d64c8841f1624...1625-------------------------------------------------16261627You will see informational messages on dangling objects. They are objects1628that still exist in the repository but are no longer referenced by any of1629your branches, and can (and will) be removed after a while with `gc`.1630You can run `git fsck --no-dangling` to suppress these messages, and still1631view real errors.16321633[[recovering-lost-changes]]1634Recovering lost changes1635~~~~~~~~~~~~~~~~~~~~~~~16361637[[reflogs]]1638Reflogs1639^^^^^^^16401641Say you modify a branch with <<fixing-mistakes,`git reset --hard`>>,1642and then realize that the branch was the only reference you had to1643that point in history.16441645Fortunately, Git also keeps a log, called a "reflog", of all the1646previous values of each branch. So in this case you can still find the1647old history using, for example,16481649-------------------------------------------------1650$ git log master@{1}1651-------------------------------------------------16521653This lists the commits reachable from the previous version of the1654`master` branch head. This syntax can be used with any Git command1655that accepts a commit, not just with `git log`. Some other examples:16561657-------------------------------------------------1658$ git show master@{2} # See where the branch pointed 2,1659$ git show master@{3} # 3, ... changes ago.1660$ gitk master@{yesterday} # See where it pointed yesterday,1661$ gitk master@{"1 week ago"} # ... or last week1662$ git log --walk-reflogs master # show reflog entries for master1663-------------------------------------------------16641665A separate reflog is kept for the HEAD, so16661667-------------------------------------------------1668$ git show HEAD@{"1 week ago"}1669-------------------------------------------------16701671will show what HEAD pointed to one week ago, not what the current branch1672pointed to one week ago. This allows you to see the history of what1673you've checked out.16741675The reflogs are kept by default for 30 days, after which they may be1676pruned. See linkgit:git-reflog[1] and linkgit:git-gc[1] to learn1677how to control this pruning, and see the "SPECIFYING REVISIONS"1678section of linkgit:gitrevisions[7] for details.16791680Note that the reflog history is very different from normal Git history.1681While normal history is shared by every repository that works on the1682same project, the reflog history is not shared: it tells you only about1683how the branches in your local repository have changed over time.16841685[[dangling-object-recovery]]1686Examining dangling objects1687^^^^^^^^^^^^^^^^^^^^^^^^^^16881689In some situations the reflog may not be able to save you. For example,1690suppose you delete a branch, then realize you need the history it1691contained. The reflog is also deleted; however, if you have not yet1692pruned the repository, then you may still be able to find the lost1693commits in the dangling objects that `git fsck` reports. See1694<<dangling-objects>> for the details.16951696-------------------------------------------------1697$ git fsck1698dangling commit 7281251ddd2a61e38657c827739c57015671a6b31699dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a631700dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b51701...1702-------------------------------------------------17031704You can examine1705one of those dangling commits with, for example,17061707------------------------------------------------1708$ gitk 7281251ddd --not --all1709------------------------------------------------17101711which does what it sounds like: it says that you want to see the commit1712history that is described by the dangling commit(s), but not the1713history that is described by all your existing branches and tags. Thus1714you get exactly the history reachable from that commit that is lost.1715(And notice that it might not be just one commit: we only report the1716"tip of the line" as being dangling, but there might be a whole deep1717and complex commit history that was dropped.)17181719If you decide you want the history back, you can always create a new1720reference pointing to it, for example, a new branch:17211722------------------------------------------------1723$ git branch recovered-branch 7281251ddd1724------------------------------------------------17251726Other types of dangling objects (blobs and trees) are also possible, and1727dangling objects can arise in other situations.172817291730[[sharing-development]]1731Sharing development with others1732===============================17331734[[getting-updates-With-git-pull]]1735Getting updates with git pull1736-----------------------------17371738After you clone a repository and commit a few changes of your own, you1739may wish to check the original repository for updates and merge them1740into your own work.17411742We have already seen <<Updating-a-repository-With-git-fetch,how to1743keep remote-tracking branches up to date>> with linkgit:git-fetch[1],1744and how to merge two branches. So you can merge in changes from the1745original repository's master branch with:17461747-------------------------------------------------1748$ git fetch1749$ git merge origin/master1750-------------------------------------------------17511752However, the linkgit:git-pull[1] command provides a way to do this in1753one step:17541755-------------------------------------------------1756$ git pull origin master1757-------------------------------------------------17581759In fact, if you have `master` checked out, then this branch has been1760configured by `git clone` to get changes from the HEAD branch of the1761origin repository. So often you can1762accomplish the above with just a simple17631764-------------------------------------------------1765$ git pull1766-------------------------------------------------17671768This command will fetch changes from the remote branches to your1769remote-tracking branches `origin/*`, and merge the default branch into1770the current branch.17711772More generally, a branch that is created from a remote-tracking branch1773will pull1774by default from that branch. See the descriptions of the1775`branch.<name>.remote` and `branch.<name>.merge` options in1776linkgit:git-config[1], and the discussion of the `--track` option in1777linkgit:git-checkout[1], to learn how to control these defaults.17781779In addition to saving you keystrokes, `git pull` also helps you by1780producing a default commit message documenting the branch and1781repository that you pulled from.17821783(But note that no such commit will be created in the case of a1784<<fast-forwards,fast-forward>>; instead, your branch will just be1785updated to point to the latest commit from the upstream branch.)17861787The `git pull` command can also be given `.` as the "remote" repository,1788in which case it just merges in a branch from the current repository; so1789the commands17901791-------------------------------------------------1792$ git pull . branch1793$ git merge branch1794-------------------------------------------------17951796are roughly equivalent.17971798[[submitting-patches]]1799Submitting patches to a project1800-------------------------------18011802If you just have a few changes, the simplest way to submit them may1803just be to send them as patches in email:18041805First, use linkgit:git-format-patch[1]; for example:18061807-------------------------------------------------1808$ git format-patch origin1809-------------------------------------------------18101811will produce a numbered series of files in the current directory, one1812for each patch in the current branch but not in `origin/HEAD`.18131814`git format-patch` can include an initial "cover letter". You can insert1815commentary on individual patches after the three dash line which1816`format-patch` places after the commit message but before the patch1817itself. If you use `git notes` to track your cover letter material,1818`git format-patch --notes` will include the commit's notes in a similar1819manner.18201821You can then import these into your mail client and send them by1822hand. However, if you have a lot to send at once, you may prefer to1823use the linkgit:git-send-email[1] script to automate the process.1824Consult the mailing list for your project first to determine how they1825prefer such patches be handled.18261827[[importing-patches]]1828Importing patches to a project1829------------------------------18301831Git also provides a tool called linkgit:git-am[1] (am stands for1832"apply mailbox"), for importing such an emailed series of patches.1833Just save all of the patch-containing messages, in order, into a1834single mailbox file, say `patches.mbox`, then run18351836-------------------------------------------------1837$ git am -3 patches.mbox1838-------------------------------------------------18391840Git will apply each patch in order; if any conflicts are found, it1841will stop, and you can fix the conflicts as described in1842"<<resolving-a-merge,Resolving a merge>>". (The `-3` option tells1843Git to perform a merge; if you would prefer it just to abort and1844leave your tree and index untouched, you may omit that option.)18451846Once the index is updated with the results of the conflict1847resolution, instead of creating a new commit, just run18481849-------------------------------------------------1850$ git am --continue1851-------------------------------------------------18521853and Git will create the commit for you and continue applying the1854remaining patches from the mailbox.18551856The final result will be a series of commits, one for each patch in1857the original mailbox, with authorship and commit log message each1858taken from the message containing each patch.18591860[[public-repositories]]1861Public Git repositories1862-----------------------18631864Another way to submit changes to a project is to tell the maintainer1865of that project to pull the changes from your repository using1866linkgit:git-pull[1]. In the section "<<getting-updates-With-git-pull,1867Getting updates with `git pull`>>" we described this as a way to get1868updates from the "main" repository, but it works just as well in the1869other direction.18701871If you and the maintainer both have accounts on the same machine, then1872you can just pull changes from each other's repositories directly;1873commands that accept repository URLs as arguments will also accept a1874local directory name:18751876-------------------------------------------------1877$ git clone /path/to/repository1878$ git pull /path/to/other/repository1879-------------------------------------------------18801881or an ssh URL:18821883-------------------------------------------------1884$ git clone ssh://yourhost/~you/repository1885-------------------------------------------------18861887For projects with few developers, or for synchronizing a few private1888repositories, this may be all you need.18891890However, the more common way to do this is to maintain a separate public1891repository (usually on a different host) for others to pull changes1892from. This is usually more convenient, and allows you to cleanly1893separate private work in progress from publicly visible work.18941895You will continue to do your day-to-day work in your personal1896repository, but periodically "push" changes from your personal1897repository into your public repository, allowing other developers to1898pull from that repository. So the flow of changes, in a situation1899where there is one other developer with a public repository, looks1900like this:19011902 you push1903 your personal repo ------------------> your public repo1904 ^ |1905 | |1906 | you pull | they pull1907 | |1908 | |1909 | they push V1910 their public repo <------------------- their repo19111912We explain how to do this in the following sections.19131914[[setting-up-a-public-repository]]1915Setting up a public repository1916~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~19171918Assume your personal repository is in the directory `~/proj`. We1919first create a new clone of the repository and tell `git daemon` that it1920is meant to be public:19211922-------------------------------------------------1923$ git clone --bare ~/proj proj.git1924$ touch proj.git/git-daemon-export-ok1925-------------------------------------------------19261927The resulting directory proj.git contains a "bare" git repository--it is1928just the contents of the `.git` directory, without any files checked out1929around it.19301931Next, copy `proj.git` to the server where you plan to host the1932public repository. You can use scp, rsync, or whatever is most1933convenient.19341935[[exporting-via-git]]1936Exporting a Git repository via the Git protocol1937~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~19381939This is the preferred method.19401941If someone else administers the server, they should tell you what1942directory to put the repository in, and what `git://` URL it will1943appear at. You can then skip to the section1944"<<pushing-changes-to-a-public-repository,Pushing changes to a public1945repository>>", below.19461947Otherwise, all you need to do is start linkgit:git-daemon[1]; it will1948listen on port 9418. By default, it will allow access to any directory1949that looks like a Git directory and contains the magic file1950git-daemon-export-ok. Passing some directory paths as `git daemon`1951arguments will further restrict the exports to those paths.19521953You can also run `git daemon` as an inetd service; see the1954linkgit:git-daemon[1] man page for details. (See especially the1955examples section.)19561957[[exporting-via-http]]1958Exporting a git repository via HTTP1959~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~19601961The Git protocol gives better performance and reliability, but on a1962host with a web server set up, HTTP exports may be simpler to set up.19631964All you need to do is place the newly created bare Git repository in1965a directory that is exported by the web server, and make some1966adjustments to give web clients some extra information they need:19671968-------------------------------------------------1969$ mv proj.git /home/you/public_html/proj.git1970$ cd proj.git1971$ git --bare update-server-info1972$ mv hooks/post-update.sample hooks/post-update1973-------------------------------------------------19741975(For an explanation of the last two lines, see1976linkgit:git-update-server-info[1] and linkgit:githooks[5].)19771978Advertise the URL of `proj.git`. Anybody else should then be able to1979clone or pull from that URL, for example with a command line like:19801981-------------------------------------------------1982$ git clone http://yourserver.com/~you/proj.git1983-------------------------------------------------19841985(See also1986link:howto/setup-git-server-over-http.txt[setup-git-server-over-http]1987for a slightly more sophisticated setup using WebDAV which also1988allows pushing over HTTP.)19891990[[pushing-changes-to-a-public-repository]]1991Pushing changes to a public repository1992~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~19931994Note that the two techniques outlined above (exporting via1995<<exporting-via-http,http>> or <<exporting-via-git,git>>) allow other1996maintainers to fetch your latest changes, but they do not allow write1997access, which you will need to update the public repository with the1998latest changes created in your private repository.19992000The simplest way to do this is using linkgit:git-push[1] and ssh; to2001update the remote branch named `master` with the latest state of your2002branch named `master`, run20032004-------------------------------------------------2005$ git push ssh://yourserver.com/~you/proj.git master:master2006-------------------------------------------------20072008or just20092010-------------------------------------------------2011$ git push ssh://yourserver.com/~you/proj.git master2012-------------------------------------------------20132014As with `git fetch`, `git push` will complain if this does not result in a2015<<fast-forwards,fast-forward>>; see the following section for details on2016handling this case.20172018Note that the target of a `push` is normally a2019<<def_bare_repository,bare>> repository. You can also push to a2020repository that has a checked-out working tree, but a push to update the2021currently checked-out branch is denied by default to prevent confusion.2022See the description of the receive.denyCurrentBranch option2023in linkgit:git-config[1] for details.20242025As with `git fetch`, you may also set up configuration options to2026save typing; so, for example:20272028-------------------------------------------------2029$ git remote add public-repo ssh://yourserver.com/~you/proj.git2030-------------------------------------------------20312032adds the following to `.git/config`:20332034-------------------------------------------------2035[remote "public-repo"]2036 url = yourserver.com:proj.git2037 fetch = +refs/heads/*:refs/remotes/example/*2038-------------------------------------------------20392040which lets you do the same push with just20412042-------------------------------------------------2043$ git push public-repo master2044-------------------------------------------------20452046See the explanations of the `remote.<name>.url`,2047`branch.<name>.remote`, and `remote.<name>.push` options in2048linkgit:git-config[1] for details.20492050[[forcing-push]]2051What to do when a push fails2052~~~~~~~~~~~~~~~~~~~~~~~~~~~~20532054If a push would not result in a <<fast-forwards,fast-forward>> of the2055remote branch, then it will fail with an error like:20562057-------------------------------------------------2058error: remote 'refs/heads/master' is not an ancestor of2059 local 'refs/heads/master'.2060 Maybe you are not up-to-date and need to pull first?2061error: failed to push to 'ssh://yourserver.com/~you/proj.git'2062-------------------------------------------------20632064This can happen, for example, if you:20652066 - use `git reset --hard` to remove already-published commits, or2067 - use `git commit --amend` to replace already-published commits2068 (as in <<fixing-a-mistake-by-rewriting-history>>), or2069 - use `git rebase` to rebase any already-published commits (as2070 in <<using-git-rebase>>).20712072You may force `git push` to perform the update anyway by preceding the2073branch name with a plus sign:20742075-------------------------------------------------2076$ git push ssh://yourserver.com/~you/proj.git +master2077-------------------------------------------------20782079Note the addition of the `+` sign. Alternatively, you can use the2080`-f` flag to force the remote update, as in:20812082-------------------------------------------------2083$ git push -f ssh://yourserver.com/~you/proj.git master2084-------------------------------------------------20852086Normally whenever a branch head in a public repository is modified, it2087is modified to point to a descendant of the commit that it pointed to2088before. By forcing a push in this situation, you break that convention.2089(See <<problems-With-rewriting-history>>.)20902091Nevertheless, this is a common practice for people that need a simple2092way to publish a work-in-progress patch series, and it is an acceptable2093compromise as long as you warn other developers that this is how you2094intend to manage the branch.20952096It's also possible for a push to fail in this way when other people have2097the right to push to the same repository. In that case, the correct2098solution is to retry the push after first updating your work: either by a2099pull, or by a fetch followed by a rebase; see the2100<<setting-up-a-shared-repository,next section>> and2101linkgit:gitcvs-migration[7] for more.21022103[[setting-up-a-shared-repository]]2104Setting up a shared repository2105~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~21062107Another way to collaborate is by using a model similar to that2108commonly used in CVS, where several developers with special rights2109all push to and pull from a single shared repository. See2110linkgit:gitcvs-migration[7] for instructions on how to2111set this up.21122113However, while there is nothing wrong with Git's support for shared2114repositories, this mode of operation is not generally recommended,2115simply because the mode of collaboration that Git supports--by2116exchanging patches and pulling from public repositories--has so many2117advantages over the central shared repository:21182119 - Git's ability to quickly import and merge patches allows a2120 single maintainer to process incoming changes even at very2121 high rates. And when that becomes too much, `git pull` provides2122 an easy way for that maintainer to delegate this job to other2123 maintainers while still allowing optional review of incoming2124 changes.2125 - Since every developer's repository has the same complete copy2126 of the project history, no repository is special, and it is2127 trivial for another developer to take over maintenance of a2128 project, either by mutual agreement, or because a maintainer2129 becomes unresponsive or difficult to work with.2130 - The lack of a central group of "committers" means there is2131 less need for formal decisions about who is "in" and who is2132 "out".21332134[[setting-up-gitweb]]2135Allowing web browsing of a repository2136~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~21372138The gitweb cgi script provides users an easy way to browse your2139project's files and history without having to install Git; see the file2140gitweb/INSTALL in the Git source tree for instructions on setting it up.21412142[[sharing-development-examples]]2143Examples2144--------21452146[[maintaining-topic-branches]]2147Maintaining topic branches for a Linux subsystem maintainer2148~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~21492150This describes how Tony Luck uses Git in his role as maintainer of the2151IA64 architecture for the Linux kernel.21522153He uses two public branches:21542155 - A "test" tree into which patches are initially placed so that they2156 can get some exposure when integrated with other ongoing development.2157 This tree is available to Andrew for pulling into -mm whenever he2158 wants.21592160 - A "release" tree into which tested patches are moved for final sanity2161 checking, and as a vehicle to send them upstream to Linus (by sending2162 him a "please pull" request.)21632164He also uses a set of temporary branches ("topic branches"), each2165containing a logical grouping of patches.21662167To set this up, first create your work tree by cloning Linus's public2168tree:21692170-------------------------------------------------2171$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git work2172$ cd work2173-------------------------------------------------21742175Linus's tree will be stored in the remote-tracking branch named origin/master,2176and can be updated using linkgit:git-fetch[1]; you can track other2177public trees using linkgit:git-remote[1] to set up a "remote" and2178linkgit:git-fetch[1] to keep them up-to-date; see2179<<repositories-and-branches>>.21802181Now create the branches in which you are going to work; these start out2182at the current tip of origin/master branch, and should be set up (using2183the `--track` option to linkgit:git-branch[1]) to merge changes in from2184Linus by default.21852186-------------------------------------------------2187$ git branch --track test origin/master2188$ git branch --track release origin/master2189-------------------------------------------------21902191These can be easily kept up to date using linkgit:git-pull[1].21922193-------------------------------------------------2194$ git checkout test && git pull2195$ git checkout release && git pull2196-------------------------------------------------21972198Important note! If you have any local changes in these branches, then2199this merge will create a commit object in the history (with no local2200changes Git will simply do a "fast-forward" merge). Many people dislike2201the "noise" that this creates in the Linux history, so you should avoid2202doing this capriciously in the `release` branch, as these noisy commits2203will become part of the permanent history when you ask Linus to pull2204from the release branch.22052206A few configuration variables (see linkgit:git-config[1]) can2207make it easy to push both branches to your public tree. (See2208<<setting-up-a-public-repository>>.)22092210-------------------------------------------------2211$ cat >> .git/config <<EOF2212[remote "mytree"]2213 url = master.kernel.org:/pub/scm/linux/kernel/git/aegl/linux.git2214 push = release2215 push = test2216EOF2217-------------------------------------------------22182219Then you can push both the test and release trees using2220linkgit:git-push[1]:22212222-------------------------------------------------2223$ git push mytree2224-------------------------------------------------22252226or push just one of the test and release branches using:22272228-------------------------------------------------2229$ git push mytree test2230-------------------------------------------------22312232or22332234-------------------------------------------------2235$ git push mytree release2236-------------------------------------------------22372238Now to apply some patches from the community. Think of a short2239snappy name for a branch to hold this patch (or related group of2240patches), and create a new branch from a recent stable tag of2241Linus's branch. Picking a stable base for your branch will:22421) help you: by avoiding inclusion of unrelated and perhaps lightly2243tested changes22442) help future bug hunters that use `git bisect` to find problems22452246-------------------------------------------------2247$ git checkout -b speed-up-spinlocks v2.6.352248-------------------------------------------------22492250Now you apply the patch(es), run some tests, and commit the change(s). If2251the patch is a multi-part series, then you should apply each as a separate2252commit to this branch.22532254-------------------------------------------------2255$ ... patch ... test ... commit [ ... patch ... test ... commit ]*2256-------------------------------------------------22572258When you are happy with the state of this change, you can merge it into the2259"test" branch in preparation to make it public:22602261-------------------------------------------------2262$ git checkout test && git merge speed-up-spinlocks2263-------------------------------------------------22642265It is unlikely that you would have any conflicts here ... but you might if you2266spent a while on this step and had also pulled new versions from upstream.22672268Some time later when enough time has passed and testing done, you can pull the2269same branch into the `release` tree ready to go upstream. This is where you2270see the value of keeping each patch (or patch series) in its own branch. It2271means that the patches can be moved into the `release` tree in any order.22722273-------------------------------------------------2274$ git checkout release && git merge speed-up-spinlocks2275-------------------------------------------------22762277After a while, you will have a number of branches, and despite the2278well chosen names you picked for each of them, you may forget what2279they are for, or what status they are in. To get a reminder of what2280changes are in a specific branch, use:22812282-------------------------------------------------2283$ git log linux..branchname | git shortlog2284-------------------------------------------------22852286To see whether it has already been merged into the test or release branches,2287use:22882289-------------------------------------------------2290$ git log test..branchname2291-------------------------------------------------22922293or22942295-------------------------------------------------2296$ git log release..branchname2297-------------------------------------------------22982299(If this branch has not yet been merged, you will see some log entries.2300If it has been merged, then there will be no output.)23012302Once a patch completes the great cycle (moving from test to release,2303then pulled by Linus, and finally coming back into your local2304`origin/master` branch), the branch for this change is no longer needed.2305You detect this when the output from:23062307-------------------------------------------------2308$ git log origin..branchname2309-------------------------------------------------23102311is empty. At this point the branch can be deleted:23122313-------------------------------------------------2314$ git branch -d branchname2315-------------------------------------------------23162317Some changes are so trivial that it is not necessary to create a separate2318branch and then merge into each of the test and release branches. For2319these changes, just apply directly to the `release` branch, and then2320merge that into the `test` branch.23212322After pushing your work to `mytree`, you can use2323linkgit:git-request-pull[1] to prepare a "please pull" request message2324to send to Linus:23252326-------------------------------------------------2327$ git push mytree2328$ git request-pull origin mytree release2329-------------------------------------------------23302331Here are some of the scripts that simplify all this even further.23322333-------------------------------------------------2334==== update script ====2335# Update a branch in my Git tree. If the branch to be updated2336# is origin, then pull from kernel.org. Otherwise merge2337# origin/master branch into test|release branch23382339case "$1" in2340test|release)2341 git checkout $1 && git pull . origin2342 ;;2343origin)2344 before=$(git rev-parse refs/remotes/origin/master)2345 git fetch origin2346 after=$(git rev-parse refs/remotes/origin/master)2347 if [ $before != $after ]2348 then2349 git log $before..$after | git shortlog2350 fi2351 ;;2352*)2353 echo "usage: $0 origin|test|release" 1>&22354 exit 12355 ;;2356esac2357-------------------------------------------------23582359-------------------------------------------------2360==== merge script ====2361# Merge a branch into either the test or release branch23622363pname=$023642365usage()2366{2367 echo "usage: $pname branch test|release" 1>&22368 exit 12369}23702371git show-ref -q --verify -- refs/heads/"$1" || {2372 echo "Can't see branch <$1>" 1>&22373 usage2374}23752376case "$2" in2377test|release)2378 if [ $(git log $2..$1 | wc -c) -eq 0 ]2379 then2380 echo $1 already merged into $2 1>&22381 exit 12382 fi2383 git checkout $2 && git pull . $12384 ;;2385*)2386 usage2387 ;;2388esac2389-------------------------------------------------23902391-------------------------------------------------2392==== status script ====2393# report on status of my ia64 Git tree23942395gb=$(tput setab 2)2396rb=$(tput setab 1)2397restore=$(tput setab 9)23982399if [ `git rev-list test..release | wc -c` -gt 0 ]2400then2401 echo $rb Warning: commits in release that are not in test $restore2402 git log test..release2403fi24042405for branch in `git show-ref --heads | sed 's|^.*/||'`2406do2407 if [ $branch = test -o $branch = release ]2408 then2409 continue2410 fi24112412 echo -n $gb ======= $branch ====== $restore " "2413 status=2414 for ref in test release origin/master2415 do2416 if [ `git rev-list $ref..$branch | wc -c` -gt 0 ]2417 then2418 status=$status${ref:0:1}2419 fi2420 done2421 case $status in2422 trl)2423 echo $rb Need to pull into test $restore2424 ;;2425 rl)2426 echo "In test"2427 ;;2428 l)2429 echo "Waiting for linus"2430 ;;2431 "")2432 echo $rb All done $restore2433 ;;2434 *)2435 echo $rb "<$status>" $restore2436 ;;2437 esac2438 git log origin/master..$branch | git shortlog2439done2440-------------------------------------------------244124422443[[cleaning-up-history]]2444Rewriting history and maintaining patch series2445==============================================24462447Normally commits are only added to a project, never taken away or2448replaced. Git is designed with this assumption, and violating it will2449cause Git's merge machinery (for example) to do the wrong thing.24502451However, there is a situation in which it can be useful to violate this2452assumption.24532454[[patch-series]]2455Creating the perfect patch series2456---------------------------------24572458Suppose you are a contributor to a large project, and you want to add a2459complicated feature, and to present it to the other developers in a way2460that makes it easy for them to read your changes, verify that they are2461correct, and understand why you made each change.24622463If you present all of your changes as a single patch (or commit), they2464may find that it is too much to digest all at once.24652466If you present them with the entire history of your work, complete with2467mistakes, corrections, and dead ends, they may be overwhelmed.24682469So the ideal is usually to produce a series of patches such that:24702471 1. Each patch can be applied in order.24722473 2. Each patch includes a single logical change, together with a2474 message explaining the change.24752476 3. No patch introduces a regression: after applying any initial2477 part of the series, the resulting project still compiles and2478 works, and has no bugs that it didn't have before.24792480 4. The complete series produces the same end result as your own2481 (probably much messier!) development process did.24822483We will introduce some tools that can help you do this, explain how to2484use them, and then explain some of the problems that can arise because2485you are rewriting history.24862487[[using-git-rebase]]2488Keeping a patch series up to date using git rebase2489--------------------------------------------------24902491Suppose that you create a branch `mywork` on a remote-tracking branch2492`origin`, and create some commits on top of it:24932494-------------------------------------------------2495$ git checkout -b mywork origin2496$ vi file.txt2497$ git commit2498$ vi otherfile.txt2499$ git commit2500...2501-------------------------------------------------25022503You have performed no merges into mywork, so it is just a simple linear2504sequence of patches on top of `origin`:25052506................................................2507 o--o--O <-- origin2508 \2509 a--b--c <-- mywork2510................................................25112512Some more interesting work has been done in the upstream project, and2513`origin` has advanced:25142515................................................2516 o--o--O--o--o--o <-- origin2517 \2518 a--b--c <-- mywork2519................................................25202521At this point, you could use `pull` to merge your changes back in;2522the result would create a new merge commit, like this:25232524................................................2525 o--o--O--o--o--o <-- origin2526 \ \2527 a--b--c--m <-- mywork2528................................................25292530However, if you prefer to keep the history in mywork a simple series of2531commits without any merges, you may instead choose to use2532linkgit:git-rebase[1]:25332534-------------------------------------------------2535$ git checkout mywork2536$ git rebase origin2537-------------------------------------------------25382539This will remove each of your commits from mywork, temporarily saving2540them as patches (in a directory named `.git/rebase-apply`), update mywork to2541point at the latest version of origin, then apply each of the saved2542patches to the new mywork. The result will look like:254325442545................................................2546 o--o--O--o--o--o <-- origin2547 \2548 a'--b'--c' <-- mywork2549................................................25502551In the process, it may discover conflicts. In that case it will stop2552and allow you to fix the conflicts; after fixing conflicts, use `git add`2553to update the index with those contents, and then, instead of2554running `git commit`, just run25552556-------------------------------------------------2557$ git rebase --continue2558-------------------------------------------------25592560and Git will continue applying the rest of the patches.25612562At any point you may use the `--abort` option to abort this process and2563return mywork to the state it had before you started the rebase:25642565-------------------------------------------------2566$ git rebase --abort2567-------------------------------------------------25682569If you need to reorder or edit a number of commits in a branch, it may2570be easier to use `git rebase -i`, which allows you to reorder and2571squash commits, as well as marking them for individual editing during2572the rebase. See <<interactive-rebase>> for details, and2573<<reordering-patch-series>> for alternatives.25742575[[rewriting-one-commit]]2576Rewriting a single commit2577-------------------------25782579We saw in <<fixing-a-mistake-by-rewriting-history>> that you can replace the2580most recent commit using25812582-------------------------------------------------2583$ git commit --amend2584-------------------------------------------------25852586which will replace the old commit by a new commit incorporating your2587changes, giving you a chance to edit the old commit message first.2588This is useful for fixing typos in your last commit, or for adjusting2589the patch contents of a poorly staged commit.25902591If you need to amend commits from deeper in your history, you can2592use <<interactive-rebase,interactive rebase's `edit` instruction>>.25932594[[reordering-patch-series]]2595Reordering or selecting from a patch series2596-------------------------------------------25972598Sometimes you want to edit a commit deeper in your history. One2599approach is to use `git format-patch` to create a series of patches2600and then reset the state to before the patches:26012602-------------------------------------------------2603$ git format-patch origin2604$ git reset --hard origin2605-------------------------------------------------26062607Then modify, reorder, or eliminate patches as needed before applying2608them again with linkgit:git-am[1]:26092610-------------------------------------------------2611$ git am *.patch2612-------------------------------------------------26132614[[interactive-rebase]]2615Using interactive rebases2616-------------------------26172618You can also edit a patch series with an interactive rebase. This is2619the same as <<reordering-patch-series,reordering a patch series using2620`format-patch`>>, so use whichever interface you like best.26212622Rebase your current HEAD on the last commit you want to retain as-is.2623For example, if you want to reorder the last 5 commits, use:26242625-------------------------------------------------2626$ git rebase -i HEAD~52627-------------------------------------------------26282629This will open your editor with a list of steps to be taken to perform2630your rebase.26312632-------------------------------------------------2633pick deadbee The oneline of this commit2634pick fa1afe1 The oneline of the next commit2635...26362637# Rebase c0ffeee..deadbee onto c0ffeee2638#2639# Commands:2640# p, pick = use commit2641# r, reword = use commit, but edit the commit message2642# e, edit = use commit, but stop for amending2643# s, squash = use commit, but meld into previous commit2644# f, fixup = like "squash", but discard this commit's log message2645# x, exec = run command (the rest of the line) using shell2646#2647# These lines can be re-ordered; they are executed from top to bottom.2648#2649# If you remove a line here THAT COMMIT WILL BE LOST.2650#2651# However, if you remove everything, the rebase will be aborted.2652#2653# Note that empty commits are commented out2654-------------------------------------------------26552656As explained in the comments, you can reorder commits, squash them2657together, edit commit messages, etc. by editing the list. Once you2658are satisfied, save the list and close your editor, and the rebase2659will begin.26602661The rebase will stop where `pick` has been replaced with `edit` or2662when a step in the list fails to mechanically resolve conflicts and2663needs your help. When you are done editing and/or resolving conflicts2664you can continue with `git rebase --continue`. If you decide that2665things are getting too hairy, you can always bail out with `git rebase2666--abort`. Even after the rebase is complete, you can still recover2667the original branch by using the <<reflogs,reflog>>.26682669For a more detailed discussion of the procedure and additional tips,2670see the "INTERACTIVE MODE" section of linkgit:git-rebase[1].26712672[[patch-series-tools]]2673Other tools2674-----------26752676There are numerous other tools, such as StGit, which exist for the2677purpose of maintaining a patch series. These are outside of the scope of2678this manual.26792680[[problems-With-rewriting-history]]2681Problems with rewriting history2682-------------------------------26832684The primary problem with rewriting the history of a branch has to do2685with merging. Suppose somebody fetches your branch and merges it into2686their branch, with a result something like this:26872688................................................2689 o--o--O--o--o--o <-- origin2690 \ \2691 t--t--t--m <-- their branch:2692................................................26932694Then suppose you modify the last three commits:26952696................................................2697 o--o--o <-- new head of origin2698 /2699 o--o--O--o--o--o <-- old head of origin2700................................................27012702If we examined all this history together in one repository, it will2703look like:27042705................................................2706 o--o--o <-- new head of origin2707 /2708 o--o--O--o--o--o <-- old head of origin2709 \ \2710 t--t--t--m <-- their branch:2711................................................27122713Git has no way of knowing that the new head is an updated version of2714the old head; it treats this situation exactly the same as it would if2715two developers had independently done the work on the old and new heads2716in parallel. At this point, if someone attempts to merge the new head2717in to their branch, Git will attempt to merge together the two (old and2718new) lines of development, instead of trying to replace the old by the2719new. The results are likely to be unexpected.27202721You may still choose to publish branches whose history is rewritten,2722and it may be useful for others to be able to fetch those branches in2723order to examine or test them, but they should not attempt to pull such2724branches into their own work.27252726For true distributed development that supports proper merging,2727published branches should never be rewritten.27282729[[bisect-merges]]2730Why bisecting merge commits can be harder than bisecting linear history2731-----------------------------------------------------------------------27322733The linkgit:git-bisect[1] command correctly handles history that2734includes merge commits. However, when the commit that it finds is a2735merge commit, the user may need to work harder than usual to figure out2736why that commit introduced a problem.27372738Imagine this history:27392740................................................2741 ---Z---o---X---...---o---A---C---D2742 \ /2743 o---o---Y---...---o---B2744................................................27452746Suppose that on the upper line of development, the meaning of one2747of the functions that exists at Z is changed at commit X. The2748commits from Z leading to A change both the function's2749implementation and all calling sites that exist at Z, as well2750as new calling sites they add, to be consistent. There is no2751bug at A.27522753Suppose that in the meantime on the lower line of development somebody2754adds a new calling site for that function at commit Y. The2755commits from Z leading to B all assume the old semantics of that2756function and the callers and the callee are consistent with each2757other. There is no bug at B, either.27582759Suppose further that the two development lines merge cleanly at C,2760so no conflict resolution is required.27612762Nevertheless, the code at C is broken, because the callers added2763on the lower line of development have not been converted to the new2764semantics introduced on the upper line of development. So if all2765you know is that D is bad, that Z is good, and that2766linkgit:git-bisect[1] identifies C as the culprit, how will you2767figure out that the problem is due to this change in semantics?27682769When the result of a `git bisect` is a non-merge commit, you should2770normally be able to discover the problem by examining just that commit.2771Developers can make this easy by breaking their changes into small2772self-contained commits. That won't help in the case above, however,2773because the problem isn't obvious from examination of any single2774commit; instead, a global view of the development is required. To2775make matters worse, the change in semantics in the problematic2776function may be just one small part of the changes in the upper2777line of development.27782779On the other hand, if instead of merging at C you had rebased the2780history between Z to B on top of A, you would have gotten this2781linear history:27822783................................................................2784 ---Z---o---X--...---o---A---o---o---Y*--...---o---B*--D*2785................................................................27862787Bisecting between Z and D* would hit a single culprit commit Y*,2788and understanding why Y* was broken would probably be easier.27892790Partly for this reason, many experienced Git users, even when2791working on an otherwise merge-heavy project, keep the history2792linear by rebasing against the latest upstream version before2793publishing.27942795[[advanced-branch-management]]2796Advanced branch management2797==========================27982799[[fetching-individual-branches]]2800Fetching individual branches2801----------------------------28022803Instead of using linkgit:git-remote[1], you can also choose just2804to update one branch at a time, and to store it locally under an2805arbitrary name:28062807-------------------------------------------------2808$ git fetch origin todo:my-todo-work2809-------------------------------------------------28102811The first argument, `origin`, just tells Git to fetch from the2812repository you originally cloned from. The second argument tells Git2813to fetch the branch named `todo` from the remote repository, and to2814store it locally under the name `refs/heads/my-todo-work`.28152816You can also fetch branches from other repositories; so28172818-------------------------------------------------2819$ git fetch git://example.com/proj.git master:example-master2820-------------------------------------------------28212822will create a new branch named `example-master` and store in it the2823branch named `master` from the repository at the given URL. If you2824already have a branch named example-master, it will attempt to2825<<fast-forwards,fast-forward>> to the commit given by example.com's2826master branch. In more detail:28272828[[fetch-fast-forwards]]2829git fetch and fast-forwards2830---------------------------28312832In the previous example, when updating an existing branch, `git fetch`2833checks to make sure that the most recent commit on the remote2834branch is a descendant of the most recent commit on your copy of the2835branch before updating your copy of the branch to point at the new2836commit. Git calls this process a <<fast-forwards,fast-forward>>.28372838A fast-forward looks something like this:28392840................................................2841 o--o--o--o <-- old head of the branch2842 \2843 o--o--o <-- new head of the branch2844................................................284528462847In some cases it is possible that the new head will *not* actually be2848a descendant of the old head. For example, the developer may have2849realized she made a serious mistake, and decided to backtrack,2850resulting in a situation like:28512852................................................2853 o--o--o--o--a--b <-- old head of the branch2854 \2855 o--o--o <-- new head of the branch2856................................................28572858In this case, `git fetch` will fail, and print out a warning.28592860In that case, you can still force Git to update to the new head, as2861described in the following section. However, note that in the2862situation above this may mean losing the commits labeled `a` and `b`,2863unless you've already created a reference of your own pointing to2864them.28652866[[forcing-fetch]]2867Forcing git fetch to do non-fast-forward updates2868------------------------------------------------28692870If git fetch fails because the new head of a branch is not a2871descendant of the old head, you may force the update with:28722873-------------------------------------------------2874$ git fetch git://example.com/proj.git +master:refs/remotes/example/master2875-------------------------------------------------28762877Note the addition of the `+` sign. Alternatively, you can use the `-f`2878flag to force updates of all the fetched branches, as in:28792880-------------------------------------------------2881$ git fetch -f origin2882-------------------------------------------------28832884Be aware that commits that the old version of example/master pointed at2885may be lost, as we saw in the previous section.28862887[[remote-branch-configuration]]2888Configuring remote-tracking branches2889------------------------------------28902891We saw above that `origin` is just a shortcut to refer to the2892repository that you originally cloned from. This information is2893stored in Git configuration variables, which you can see using2894linkgit:git-config[1]:28952896-------------------------------------------------2897$ git config -l2898core.repositoryformatversion=02899core.filemode=true2900core.logallrefupdates=true2901remote.origin.url=git://git.kernel.org/pub/scm/git/git.git2902remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*2903branch.master.remote=origin2904branch.master.merge=refs/heads/master2905-------------------------------------------------29062907If there are other repositories that you also use frequently, you can2908create similar configuration options to save typing; for example,29092910-------------------------------------------------2911$ git remote add example git://example.com/proj.git2912-------------------------------------------------29132914adds the following to `.git/config`:29152916-------------------------------------------------2917[remote "example"]2918 url = git://example.com/proj.git2919 fetch = +refs/heads/*:refs/remotes/example/*2920-------------------------------------------------29212922Also note that the above configuration can be performed by directly2923editing the file `.git/config` instead of using linkgit:git-remote[1].29242925After configuring the remote, the following three commands will do the2926same thing:29272928-------------------------------------------------2929$ git fetch git://example.com/proj.git +refs/heads/*:refs/remotes/example/*2930$ git fetch example +refs/heads/*:refs/remotes/example/*2931$ git fetch example2932-------------------------------------------------29332934See linkgit:git-config[1] for more details on the configuration2935options mentioned above and linkgit:git-fetch[1] for more details on2936the refspec syntax.293729382939[[git-concepts]]2940Git concepts2941============29422943Git is built on a small number of simple but powerful ideas. While it2944is possible to get things done without understanding them, you will find2945Git much more intuitive if you do.29462947We start with the most important, the <<def_object_database,object2948database>> and the <<def_index,index>>.29492950[[the-object-database]]2951The Object Database2952-------------------295329542955We already saw in <<understanding-commits>> that all commits are stored2956under a 40-digit "object name". In fact, all the information needed to2957represent the history of a project is stored in objects with such names.2958In each case the name is calculated by taking the SHA-1 hash of the2959contents of the object. The SHA-1 hash is a cryptographic hash function.2960What that means to us is that it is impossible to find two different2961objects with the same name. This has a number of advantages; among2962others:29632964- Git can quickly determine whether two objects are identical or not,2965 just by comparing names.2966- Since object names are computed the same way in every repository, the2967 same content stored in two repositories will always be stored under2968 the same name.2969- Git can detect errors when it reads an object, by checking that the2970 object's name is still the SHA-1 hash of its contents.29712972(See <<object-details>> for the details of the object formatting and2973SHA-1 calculation.)29742975There are four different types of objects: "blob", "tree", "commit", and2976"tag".29772978- A <<def_blob_object,"blob" object>> is used to store file data.2979- A <<def_tree_object,"tree" object>> ties one or more2980 "blob" objects into a directory structure. In addition, a tree object2981 can refer to other tree objects, thus creating a directory hierarchy.2982- A <<def_commit_object,"commit" object>> ties such directory hierarchies2983 together into a <<def_DAG,directed acyclic graph>> of revisions--each2984 commit contains the object name of exactly one tree designating the2985 directory hierarchy at the time of the commit. In addition, a commit2986 refers to "parent" commit objects that describe the history of how we2987 arrived at that directory hierarchy.2988- A <<def_tag_object,"tag" object>> symbolically identifies and can be2989 used to sign other objects. It contains the object name and type of2990 another object, a symbolic name (of course!) and, optionally, a2991 signature.29922993The object types in some more detail:29942995[[commit-object]]2996Commit Object2997~~~~~~~~~~~~~29982999The "commit" object links a physical state of a tree with a description3000of how we got there and why. Use the `--pretty=raw` option to3001linkgit:git-show[1] or linkgit:git-log[1] to examine your favorite3002commit:30033004------------------------------------------------3005$ git show -s --pretty=raw 2be7fcb4763006commit 2be7fcb4764f2dbcee52635b91fedb1b3dcf7ab43007tree fb3a8bdd0ceddd019615af4d57a53f43d8cee2bf3008parent 257a84d9d02e90447b149af58b271c19405edb6a3009author Dave Watson <dwatson@mimvista.com> 1187576872 -04003010committer Junio C Hamano <gitster@pobox.com> 1187591163 -070030113012 Fix misspelling of 'suppress' in docs30133014 Signed-off-by: Junio C Hamano <gitster@pobox.com>3015------------------------------------------------30163017As you can see, a commit is defined by:30183019- a tree: The SHA-1 name of a tree object (as defined below), representing3020 the contents of a directory at a certain point in time.3021- parent(s): The SHA-1 name(s) of some number of commits which represent the3022 immediately previous step(s) in the history of the project. The3023 example above has one parent; merge commits may have more than3024 one. A commit with no parents is called a "root" commit, and3025 represents the initial revision of a project. Each project must have3026 at least one root. A project can also have multiple roots, though3027 that isn't common (or necessarily a good idea).3028- an author: The name of the person responsible for this change, together3029 with its date.3030- a committer: The name of the person who actually created the commit,3031 with the date it was done. This may be different from the author, for3032 example, if the author was someone who wrote a patch and emailed it3033 to the person who used it to create the commit.3034- a comment describing this commit.30353036Note that a commit does not itself contain any information about what3037actually changed; all changes are calculated by comparing the contents3038of the tree referred to by this commit with the trees associated with3039its parents. In particular, Git does not attempt to record file renames3040explicitly, though it can identify cases where the existence of the same3041file data at changing paths suggests a rename. (See, for example, the3042`-M` option to linkgit:git-diff[1]).30433044A commit is usually created by linkgit:git-commit[1], which creates a3045commit whose parent is normally the current HEAD, and whose tree is3046taken from the content currently stored in the index.30473048[[tree-object]]3049Tree Object3050~~~~~~~~~~~30513052The ever-versatile linkgit:git-show[1] command can also be used to3053examine tree objects, but linkgit:git-ls-tree[1] will give you more3054details:30553056------------------------------------------------3057$ git ls-tree fb3a8bdd0ce3058100644 blob 63c918c667fa005ff12ad89437f2fdc80926e21c .gitignore3059100644 blob 5529b198e8d14decbe4ad99db3f7fb632de0439d .mailmap3060100644 blob 6ff87c4664981e4397625791c8ea3bbb5f2279a3 COPYING3061040000 tree 2fb783e477100ce076f6bf57e4a6f026013dc745 Documentation3062100755 blob 3c0032cec592a765692234f1cba47dfdcc3a9200 GIT-VERSION-GEN3063100644 blob 289b046a443c0647624607d471289b2c7dcd470b INSTALL3064100644 blob 4eb463797adc693dc168b926b6932ff53f17d0b1 Makefile3065100644 blob 548142c327a6790ff8821d67c2ee1eff7a656b52 README3066...3067------------------------------------------------30683069As you can see, a tree object contains a list of entries, each with a3070mode, object type, SHA-1 name, and name, sorted by name. It represents3071the contents of a single directory tree.30723073The object type may be a blob, representing the contents of a file, or3074another tree, representing the contents of a subdirectory. Since trees3075and blobs, like all other objects, are named by the SHA-1 hash of their3076contents, two trees have the same SHA-1 name if and only if their3077contents (including, recursively, the contents of all subdirectories)3078are identical. This allows Git to quickly determine the differences3079between two related tree objects, since it can ignore any entries with3080identical object names.30813082(Note: in the presence of submodules, trees may also have commits as3083entries. See <<submodules>> for documentation.)30843085Note that the files all have mode 644 or 755: Git actually only pays3086attention to the executable bit.30873088[[blob-object]]3089Blob Object3090~~~~~~~~~~~30913092You can use linkgit:git-show[1] to examine the contents of a blob; take,3093for example, the blob in the entry for `COPYING` from the tree above:30943095------------------------------------------------3096$ git show 6ff87c466430973098 Note that the only valid version of the GPL as far as this project3099 is concerned is _this_ particular version of the license (ie v2, not3100 v2.2 or v3.x or whatever), unless explicitly otherwise stated.3101...3102------------------------------------------------31033104A "blob" object is nothing but a binary blob of data. It doesn't refer3105to anything else or have attributes of any kind.31063107Since the blob is entirely defined by its data, if two files in a3108directory tree (or in multiple different versions of the repository)3109have the same contents, they will share the same blob object. The object3110is totally independent of its location in the directory tree, and3111renaming a file does not change the object that file is associated with.31123113Note that any tree or blob object can be examined using3114linkgit:git-show[1] with the <revision>:<path> syntax. This can3115sometimes be useful for browsing the contents of a tree that is not3116currently checked out.31173118[[trust]]3119Trust3120~~~~~31213122If you receive the SHA-1 name of a blob from one source, and its contents3123from another (possibly untrusted) source, you can still trust that those3124contents are correct as long as the SHA-1 name agrees. This is because3125the SHA-1 is designed so that it is infeasible to find different contents3126that produce the same hash.31273128Similarly, you need only trust the SHA-1 name of a top-level tree object3129to trust the contents of the entire directory that it refers to, and if3130you receive the SHA-1 name of a commit from a trusted source, then you3131can easily verify the entire history of commits reachable through3132parents of that commit, and all of those contents of the trees referred3133to by those commits.31343135So to introduce some real trust in the system, the only thing you need3136to do is to digitally sign just 'one' special note, which includes the3137name of a top-level commit. Your digital signature shows others3138that you trust that commit, and the immutability of the history of3139commits tells others that they can trust the whole history.31403141In other words, you can easily validate a whole archive by just3142sending out a single email that tells the people the name (SHA-1 hash)3143of the top commit, and digitally sign that email using something3144like GPG/PGP.31453146To assist in this, Git also provides the tag object...31473148[[tag-object]]3149Tag Object3150~~~~~~~~~~31513152A tag object contains an object, object type, tag name, the name of the3153person ("tagger") who created the tag, and a message, which may contain3154a signature, as can be seen using linkgit:git-cat-file[1]:31553156------------------------------------------------3157$ git cat-file tag v1.5.03158object 437b1b20df4b356c9342dac8d38849f24ef44f273159type commit3160tag v1.5.03161tagger Junio C Hamano <junkio@cox.net> 1171411200 +000031623163GIT 1.5.03164-----BEGIN PGP SIGNATURE-----3165Version: GnuPG v1.4.6 (GNU/Linux)31663167iD8DBQBF0lGqwMbZpPMRm5oRAuRiAJ9ohBLd7s2kqjkKlq1qqC57SbnmzQCdG4ui3168nLE/L9aUXdWeTFPron96DLA=3169=2E+03170-----END PGP SIGNATURE-----3171------------------------------------------------31723173See the linkgit:git-tag[1] command to learn how to create and verify tag3174objects. (Note that linkgit:git-tag[1] can also be used to create3175"lightweight tags", which are not tag objects at all, but just simple3176references whose names begin with `refs/tags/`).31773178[[pack-files]]3179How Git stores objects efficiently: pack files3180~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~31813182Newly created objects are initially created in a file named after the3183object's SHA-1 hash (stored in `.git/objects`).31843185Unfortunately this system becomes inefficient once a project has a3186lot of objects. Try this on an old project:31873188------------------------------------------------3189$ git count-objects31906930 objects, 47620 kilobytes3191------------------------------------------------31923193The first number is the number of objects which are kept in3194individual files. The second is the amount of space taken up by3195those "loose" objects.31963197You can save space and make Git faster by moving these loose objects in3198to a "pack file", which stores a group of objects in an efficient3199compressed format; the details of how pack files are formatted can be3200found in link:technical/pack-format.txt[technical/pack-format.txt].32013202To put the loose objects into a pack, just run git repack:32033204------------------------------------------------3205$ git repack3206Counting objects: 6020, done.3207Delta compression using up to 4 threads.3208Compressing objects: 100% (6020/6020), done.3209Writing objects: 100% (6020/6020), done.3210Total 6020 (delta 4070), reused 0 (delta 0)3211------------------------------------------------32123213This creates a single "pack file" in .git/objects/pack/3214containing all currently unpacked objects. You can then run32153216------------------------------------------------3217$ git prune3218------------------------------------------------32193220to remove any of the "loose" objects that are now contained in the3221pack. This will also remove any unreferenced objects (which may be3222created when, for example, you use `git reset` to remove a commit).3223You can verify that the loose objects are gone by looking at the3224`.git/objects` directory or by running32253226------------------------------------------------3227$ git count-objects32280 objects, 0 kilobytes3229------------------------------------------------32303231Although the object files are gone, any commands that refer to those3232objects will work exactly as they did before.32333234The linkgit:git-gc[1] command performs packing, pruning, and more for3235you, so is normally the only high-level command you need.32363237[[dangling-objects]]3238Dangling objects3239~~~~~~~~~~~~~~~~32403241The linkgit:git-fsck[1] command will sometimes complain about dangling3242objects. They are not a problem.32433244The most common cause of dangling objects is that you've rebased a3245branch, or you have pulled from somebody else who rebased a branch--see3246<<cleaning-up-history>>. In that case, the old head of the original3247branch still exists, as does everything it pointed to. The branch3248pointer itself just doesn't, since you replaced it with another one.32493250There are also other situations that cause dangling objects. For3251example, a "dangling blob" may arise because you did a `git add` of a3252file, but then, before you actually committed it and made it part of the3253bigger picture, you changed something else in that file and committed3254that *updated* thing--the old state that you added originally ends up3255not being pointed to by any commit or tree, so it's now a dangling blob3256object.32573258Similarly, when the "recursive" merge strategy runs, and finds that3259there are criss-cross merges and thus more than one merge base (which is3260fairly unusual, but it does happen), it will generate one temporary3261midway tree (or possibly even more, if you had lots of criss-crossing3262merges and more than two merge bases) as a temporary internal merge3263base, and again, those are real objects, but the end result will not end3264up pointing to them, so they end up "dangling" in your repository.32653266Generally, dangling objects aren't anything to worry about. They can3267even be very useful: if you screw something up, the dangling objects can3268be how you recover your old tree (say, you did a rebase, and realized3269that you really didn't want to--you can look at what dangling objects3270you have, and decide to reset your head to some old dangling state).32713272For commits, you can just use:32733274------------------------------------------------3275$ gitk <dangling-commit-sha-goes-here> --not --all3276------------------------------------------------32773278This asks for all the history reachable from the given commit but not3279from any branch, tag, or other reference. If you decide it's something3280you want, you can always create a new reference to it, e.g.,32813282------------------------------------------------3283$ git branch recovered-branch <dangling-commit-sha-goes-here>3284------------------------------------------------32853286For blobs and trees, you can't do the same, but you can still examine3287them. You can just do32883289------------------------------------------------3290$ git show <dangling-blob/tree-sha-goes-here>3291------------------------------------------------32923293to show what the contents of the blob were (or, for a tree, basically3294what the `ls` for that directory was), and that may give you some idea3295of what the operation was that left that dangling object.32963297Usually, dangling blobs and trees aren't very interesting. They're3298almost always the result of either being a half-way mergebase (the blob3299will often even have the conflict markers from a merge in it, if you3300have had conflicting merges that you fixed up by hand), or simply3301because you interrupted a `git fetch` with ^C or something like that,3302leaving _some_ of the new objects in the object database, but just3303dangling and useless.33043305Anyway, once you are sure that you're not interested in any dangling3306state, you can just prune all unreachable objects:33073308------------------------------------------------3309$ git prune3310------------------------------------------------33113312and they'll be gone. But you should only run `git prune` on a quiescent3313repository--it's kind of like doing a filesystem fsck recovery: you3314don't want to do that while the filesystem is mounted.33153316(The same is true of `git fsck` itself, btw, but since3317`git fsck` never actually *changes* the repository, it just reports3318on what it found, `git fsck` itself is never 'dangerous' to run.3319Running it while somebody is actually changing the repository can cause3320confusing and scary messages, but it won't actually do anything bad. In3321contrast, running `git prune` while somebody is actively changing the3322repository is a *BAD* idea).33233324[[recovering-from-repository-corruption]]3325Recovering from repository corruption3326~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~33273328By design, Git treats data trusted to it with caution. However, even in3329the absence of bugs in Git itself, it is still possible that hardware or3330operating system errors could corrupt data.33313332The first defense against such problems is backups. You can back up a3333Git directory using clone, or just using cp, tar, or any other backup3334mechanism.33353336As a last resort, you can search for the corrupted objects and attempt3337to replace them by hand. Back up your repository before attempting this3338in case you corrupt things even more in the process.33393340We'll assume that the problem is a single missing or corrupted blob,3341which is sometimes a solvable problem. (Recovering missing trees and3342especially commits is *much* harder).33433344Before starting, verify that there is corruption, and figure out where3345it is with linkgit:git-fsck[1]; this may be time-consuming.33463347Assume the output looks like this:33483349------------------------------------------------3350$ git fsck --full --no-dangling3351broken link from tree 2d9263c6d23595e7cb2a21e5ebbb53655278dff83352 to blob 4b9458b3786228369c63936db65827de3cc062003353missing blob 4b9458b3786228369c63936db65827de3cc062003354------------------------------------------------33553356Now you know that blob 4b9458b3 is missing, and that the tree 2d9263c63357points to it. If you could find just one copy of that missing blob3358object, possibly in some other repository, you could move it into3359`.git/objects/4b/9458b3...` and be done. Suppose you can't. You can3360still examine the tree that pointed to it with linkgit:git-ls-tree[1],3361which might output something like:33623363------------------------------------------------3364$ git ls-tree 2d9263c6d23595e7cb2a21e5ebbb53655278dff83365100644 blob 8d14531846b95bfa3564b58ccfb7913a034323b8 .gitignore3366100644 blob ebf9bf84da0aab5ed944264a5db2a65fe3a3e883 .mailmap3367100644 blob ca442d313d86dc67e0a2e5d584b465bd382cbf5c COPYING3368...3369100644 blob 4b9458b3786228369c63936db65827de3cc06200 myfile3370...3371------------------------------------------------33723373So now you know that the missing blob was the data for a file named3374`myfile`. And chances are you can also identify the directory--let's3375say it's in `somedirectory`. If you're lucky the missing copy might be3376the same as the copy you have checked out in your working tree at3377`somedirectory/myfile`; you can test whether that's right with3378linkgit:git-hash-object[1]:33793380------------------------------------------------3381$ git hash-object -w somedirectory/myfile3382------------------------------------------------33833384which will create and store a blob object with the contents of3385somedirectory/myfile, and output the SHA-1 of that object. if you're3386extremely lucky it might be 4b9458b3786228369c63936db65827de3cc06200, in3387which case you've guessed right, and the corruption is fixed!33883389Otherwise, you need more information. How do you tell which version of3390the file has been lost?33913392The easiest way to do this is with:33933394------------------------------------------------3395$ git log --raw --all --full-history -- somedirectory/myfile3396------------------------------------------------33973398Because you're asking for raw output, you'll now get something like33993400------------------------------------------------3401commit abc3402Author:3403Date:3404...3405:100644 100644 4b9458b... newsha... M somedirectory/myfile340634073408commit xyz3409Author:3410Date:34113412...3413:100644 100644 oldsha... 4b9458b... M somedirectory/myfile3414------------------------------------------------34153416This tells you that the immediately following version of the file was3417"newsha", and that the immediately preceding version was "oldsha".3418You also know the commit messages that went with the change from oldsha3419to 4b9458b and with the change from 4b9458b to newsha.34203421If you've been committing small enough changes, you may now have a good3422shot at reconstructing the contents of the in-between state 4b9458b.34233424If you can do that, you can now recreate the missing object with34253426------------------------------------------------3427$ git hash-object -w <recreated-file>3428------------------------------------------------34293430and your repository is good again!34313432(Btw, you could have ignored the `fsck`, and started with doing a34333434------------------------------------------------3435$ git log --raw --all3436------------------------------------------------34373438and just looked for the sha of the missing object (4b9458b..) in that3439whole thing. It's up to you--Git does *have* a lot of information, it is3440just missing one particular blob version.34413442[[the-index]]3443The index3444-----------34453446The index is a binary file (generally kept in `.git/index`) containing a3447sorted list of path names, each with permissions and the SHA-1 of a blob3448object; linkgit:git-ls-files[1] can show you the contents of the index:34493450-------------------------------------------------3451$ git ls-files --stage3452100644 63c918c667fa005ff12ad89437f2fdc80926e21c 0 .gitignore3453100644 5529b198e8d14decbe4ad99db3f7fb632de0439d 0 .mailmap3454100644 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0 COPYING3455100644 a37b2152bd26be2c2289e1f57a292534a51a93c7 0 Documentation/.gitignore3456100644 fbefe9a45b00a54b58d94d06eca48b03d40a50e0 0 Documentation/Makefile3457...3458100644 2511aef8d89ab52be5ec6a5e46236b4b6bcd07ea 0 xdiff/xtypes.h3459100644 2ade97b2574a9f77e7ae4002a4e07a6a38e46d07 0 xdiff/xutils.c3460100644 d5de8292e05e7c36c4b68857c1cf9855e3d2f70a 0 xdiff/xutils.h3461-------------------------------------------------34623463Note that in older documentation you may see the index called the3464"current directory cache" or just the "cache". It has three important3465properties:346634671. The index contains all the information necessary to generate a single3468(uniquely determined) tree object.3469+3470For example, running linkgit:git-commit[1] generates this tree object3471from the index, stores it in the object database, and uses it as the3472tree object associated with the new commit.347334742. The index enables fast comparisons between the tree object it defines3475and the working tree.3476+3477It does this by storing some additional data for each entry (such as3478the last modified time). This data is not displayed above, and is not3479stored in the created tree object, but it can be used to determine3480quickly which files in the working directory differ from what was3481stored in the index, and thus save Git from having to read all of the3482data from such files to look for changes.348334843. It can efficiently represent information about merge conflicts3485between different tree objects, allowing each pathname to be3486associated with sufficient information about the trees involved that3487you can create a three-way merge between them.3488+3489We saw in <<conflict-resolution>> that during a merge the index can3490store multiple versions of a single file (called "stages"). The third3491column in the linkgit:git-ls-files[1] output above is the stage3492number, and will take on values other than 0 for files with merge3493conflicts.34943495The index is thus a sort of temporary staging area, which is filled with3496a tree which you are in the process of working on.34973498If you blow the index away entirely, you generally haven't lost any3499information as long as you have the name of the tree that it described.35003501[[submodules]]3502Submodules3503==========35043505Large projects are often composed of smaller, self-contained modules. For3506example, an embedded Linux distribution's source tree would include every3507piece of software in the distribution with some local modifications; a movie3508player might need to build against a specific, known-working version of a3509decompression library; several independent programs might all share the same3510build scripts.35113512With centralized revision control systems this is often accomplished by3513including every module in one single repository. Developers can check out3514all modules or only the modules they need to work with. They can even modify3515files across several modules in a single commit while moving things around3516or updating APIs and translations.35173518Git does not allow partial checkouts, so duplicating this approach in Git3519would force developers to keep a local copy of modules they are not3520interested in touching. Commits in an enormous checkout would be slower3521than you'd expect as Git would have to scan every directory for changes.3522If modules have a lot of local history, clones would take forever.35233524On the plus side, distributed revision control systems can much better3525integrate with external sources. In a centralized model, a single arbitrary3526snapshot of the external project is exported from its own revision control3527and then imported into the local revision control on a vendor branch. All3528the history is hidden. With distributed revision control you can clone the3529entire external history and much more easily follow development and re-merge3530local changes.35313532Git's submodule support allows a repository to contain, as a subdirectory, a3533checkout of an external project. Submodules maintain their own identity;3534the submodule support just stores the submodule repository location and3535commit ID, so other developers who clone the containing project3536("superproject") can easily clone all the submodules at the same revision.3537Partial checkouts of the superproject are possible: you can tell Git to3538clone none, some or all of the submodules.35393540The linkgit:git-submodule[1] command is available since Git 1.5.3. Users3541with Git 1.5.2 can look up the submodule commits in the repository and3542manually check them out; earlier versions won't recognize the submodules at3543all.35443545To see how submodule support works, create four example3546repositories that can be used later as a submodule:35473548-------------------------------------------------3549$ mkdir ~/git3550$ cd ~/git3551$ for i in a b c d3552do3553 mkdir $i3554 cd $i3555 git init3556 echo "module $i" > $i.txt3557 git add $i.txt3558 git commit -m "Initial commit, submodule $i"3559 cd ..3560done3561-------------------------------------------------35623563Now create the superproject and add all the submodules:35643565-------------------------------------------------3566$ mkdir super3567$ cd super3568$ git init3569$ for i in a b c d3570do3571 git submodule add ~/git/$i $i3572done3573-------------------------------------------------35743575NOTE: Do not use local URLs here if you plan to publish your superproject!35763577See what files `git submodule` created:35783579-------------------------------------------------3580$ ls -a3581. .. .git .gitmodules a b c d3582-------------------------------------------------35833584The `git submodule add <repo> <path>` command does a couple of things:35853586- It clones the submodule from `<repo>` to the given `<path>` under the3587 current directory and by default checks out the master branch.3588- It adds the submodule's clone path to the linkgit:gitmodules[5] file and3589 adds this file to the index, ready to be committed.3590- It adds the submodule's current commit ID to the index, ready to be3591 committed.35923593Commit the superproject:35943595-------------------------------------------------3596$ git commit -m "Add submodules a, b, c and d."3597-------------------------------------------------35983599Now clone the superproject:36003601-------------------------------------------------3602$ cd ..3603$ git clone super cloned3604$ cd cloned3605-------------------------------------------------36063607The submodule directories are there, but they're empty:36083609-------------------------------------------------3610$ ls -a a3611. ..3612$ git submodule status3613-d266b9873ad50488163457f025db7cdd9683d88b a3614-e81d457da15309b4fef4249aba9b50187999670d b3615-c1536a972b9affea0f16e0680ba87332dc059146 c3616-d96249ff5d57de5de093e6baff9e0aafa5276a74 d3617-------------------------------------------------36183619NOTE: The commit object names shown above would be different for you, but they3620should match the HEAD commit object names of your repositories. You can check3621it by running `git ls-remote ../a`.36223623Pulling down the submodules is a two-step process. First run `git submodule3624init` to add the submodule repository URLs to `.git/config`:36253626-------------------------------------------------3627$ git submodule init3628-------------------------------------------------36293630Now use `git submodule update` to clone the repositories and check out the3631commits specified in the superproject:36323633-------------------------------------------------3634$ git submodule update3635$ cd a3636$ ls -a3637. .. .git a.txt3638-------------------------------------------------36393640One major difference between `git submodule update` and `git submodule add` is3641that `git submodule update` checks out a specific commit, rather than the tip3642of a branch. It's like checking out a tag: the head is detached, so you're not3643working on a branch.36443645-------------------------------------------------3646$ git branch3647* (detached from d266b98)3648 master3649-------------------------------------------------36503651If you want to make a change within a submodule and you have a detached head,3652then you should create or checkout a branch, make your changes, publish the3653change within the submodule, and then update the superproject to reference the3654new commit:36553656-------------------------------------------------3657$ git checkout master3658-------------------------------------------------36593660or36613662-------------------------------------------------3663$ git checkout -b fix-up3664-------------------------------------------------36653666then36673668-------------------------------------------------3669$ echo "adding a line again" >> a.txt3670$ git commit -a -m "Updated the submodule from within the superproject."3671$ git push3672$ cd ..3673$ git diff3674diff --git a/a b/a3675index d266b98..261dfac 1600003676--- a/a3677+++ b/a3678@@ -1 +1 @@3679-Subproject commit d266b9873ad50488163457f025db7cdd9683d88b3680+Subproject commit 261dfac35cb99d380eb966e102c1197139f7fa243681$ git add a3682$ git commit -m "Updated submodule a."3683$ git push3684-------------------------------------------------36853686You have to run `git submodule update` after `git pull` if you want to update3687submodules, too.36883689Pitfalls with submodules3690------------------------36913692Always publish the submodule change before publishing the change to the3693superproject that references it. If you forget to publish the submodule change,3694others won't be able to clone the repository:36953696-------------------------------------------------3697$ cd ~/git/super/a3698$ echo i added another line to this file >> a.txt3699$ git commit -a -m "doing it wrong this time"3700$ cd ..3701$ git add a3702$ git commit -m "Updated submodule a again."3703$ git push3704$ cd ~/git/cloned3705$ git pull3706$ git submodule update3707error: pathspec '261dfac35cb99d380eb966e102c1197139f7fa24' did not match any file(s) known to git.3708Did you forget to 'git add'?3709Unable to checkout '261dfac35cb99d380eb966e102c1197139f7fa24' in submodule path 'a'3710-------------------------------------------------37113712In older Git versions it could be easily forgotten to commit new or modified3713files in a submodule, which silently leads to similar problems as not pushing3714the submodule changes. Starting with Git 1.7.0 both `git status` and `git diff`3715in the superproject show submodules as modified when they contain new or3716modified files to protect against accidentally committing such a state. `git3717diff` will also add a `-dirty` to the work tree side when generating patch3718output or used with the `--submodule` option:37193720-------------------------------------------------3721$ git diff3722diff --git a/sub b/sub3723--- a/sub3724+++ b/sub3725@@ -1 +1 @@3726-Subproject commit 3f356705649b5d566d97ff843cf193359229a4533727+Subproject commit 3f356705649b5d566d97ff843cf193359229a453-dirty3728$ git diff --submodule3729Submodule sub 3f35670..3f35670-dirty:3730-------------------------------------------------37313732You also should not rewind branches in a submodule beyond commits that were3733ever recorded in any superproject.37343735It's not safe to run `git submodule update` if you've made and committed3736changes within a submodule without checking out a branch first. They will be3737silently overwritten:37383739-------------------------------------------------3740$ cat a.txt3741module a3742$ echo line added from private2 >> a.txt3743$ git commit -a -m "line added inside private2"3744$ cd ..3745$ git submodule update3746Submodule path 'a': checked out 'd266b9873ad50488163457f025db7cdd9683d88b'3747$ cd a3748$ cat a.txt3749module a3750-------------------------------------------------37513752NOTE: The changes are still visible in the submodule's reflog.37533754If you have uncommitted changes in your submodule working tree, `git3755submodule update` will not overwrite them. Instead, you get the usual3756warning about not being able switch from a dirty branch.37573758[[low-level-operations]]3759Low-level Git operations3760========================37613762Many of the higher-level commands were originally implemented as shell3763scripts using a smaller core of low-level Git commands. These can still3764be useful when doing unusual things with Git, or just as a way to3765understand its inner workings.37663767[[object-manipulation]]3768Object access and manipulation3769------------------------------37703771The linkgit:git-cat-file[1] command can show the contents of any object,3772though the higher-level linkgit:git-show[1] is usually more useful.37733774The linkgit:git-commit-tree[1] command allows constructing commits with3775arbitrary parents and trees.37763777A tree can be created with linkgit:git-write-tree[1] and its data can be3778accessed by linkgit:git-ls-tree[1]. Two trees can be compared with3779linkgit:git-diff-tree[1].37803781A tag is created with linkgit:git-mktag[1], and the signature can be3782verified by linkgit:git-verify-tag[1], though it is normally simpler to3783use linkgit:git-tag[1] for both.37843785[[the-workflow]]3786The Workflow3787------------37883789High-level operations such as linkgit:git-commit[1],3790linkgit:git-checkout[1] and linkgit:git-reset[1] work by moving data3791between the working tree, the index, and the object database. Git3792provides low-level operations which perform each of these steps3793individually.37943795Generally, all Git operations work on the index file. Some operations3796work *purely* on the index file (showing the current state of the3797index), but most operations move data between the index file and either3798the database or the working directory. Thus there are four main3799combinations:38003801[[working-directory-to-index]]3802working directory -> index3803~~~~~~~~~~~~~~~~~~~~~~~~~~38043805The linkgit:git-update-index[1] command updates the index with3806information from the working directory. You generally update the3807index information by just specifying the filename you want to update,3808like so:38093810-------------------------------------------------3811$ git update-index filename3812-------------------------------------------------38133814but to avoid common mistakes with filename globbing etc, the command3815will not normally add totally new entries or remove old entries,3816i.e. it will normally just update existing cache entries.38173818To tell Git that yes, you really do realize that certain files no3819longer exist, or that new files should be added, you3820should use the `--remove` and `--add` flags respectively.38213822NOTE! A `--remove` flag does 'not' mean that subsequent filenames will3823necessarily be removed: if the files still exist in your directory3824structure, the index will be updated with their new status, not3825removed. The only thing `--remove` means is that update-index will be3826considering a removed file to be a valid thing, and if the file really3827does not exist any more, it will update the index accordingly.38283829As a special case, you can also do `git update-index --refresh`, which3830will refresh the "stat" information of each index to match the current3831stat information. It will 'not' update the object status itself, and3832it will only update the fields that are used to quickly test whether3833an object still matches its old backing store object.38343835The previously introduced linkgit:git-add[1] is just a wrapper for3836linkgit:git-update-index[1].38373838[[index-to-object-database]]3839index -> object database3840~~~~~~~~~~~~~~~~~~~~~~~~38413842You write your current index file to a "tree" object with the program38433844-------------------------------------------------3845$ git write-tree3846-------------------------------------------------38473848that doesn't come with any options--it will just write out the3849current index into the set of tree objects that describe that state,3850and it will return the name of the resulting top-level tree. You can3851use that tree to re-generate the index at any time by going in the3852other direction:38533854[[object-database-to-index]]3855object database -> index3856~~~~~~~~~~~~~~~~~~~~~~~~38573858You read a "tree" file from the object database, and use that to3859populate (and overwrite--don't do this if your index contains any3860unsaved state that you might want to restore later!) your current3861index. Normal operation is just38623863-------------------------------------------------3864$ git read-tree <SHA-1 of tree>3865-------------------------------------------------38663867and your index file will now be equivalent to the tree that you saved3868earlier. However, that is only your 'index' file: your working3869directory contents have not been modified.38703871[[index-to-working-directory]]3872index -> working directory3873~~~~~~~~~~~~~~~~~~~~~~~~~~38743875You update your working directory from the index by "checking out"3876files. This is not a very common operation, since normally you'd just3877keep your files updated, and rather than write to your working3878directory, you'd tell the index files about the changes in your3879working directory (i.e. `git update-index`).38803881However, if you decide to jump to a new version, or check out somebody3882else's version, or just restore a previous tree, you'd populate your3883index file with read-tree, and then you need to check out the result3884with38853886-------------------------------------------------3887$ git checkout-index filename3888-------------------------------------------------38893890or, if you want to check out all of the index, use `-a`.38913892NOTE! `git checkout-index` normally refuses to overwrite old files, so3893if you have an old version of the tree already checked out, you will3894need to use the `-f` flag ('before' the `-a` flag or the filename) to3895'force' the checkout.389638973898Finally, there are a few odds and ends which are not purely moving3899from one representation to the other:39003901[[tying-it-all-together]]3902Tying it all together3903~~~~~~~~~~~~~~~~~~~~~39043905To commit a tree you have instantiated with `git write-tree`, you'd3906create a "commit" object that refers to that tree and the history3907behind it--most notably the "parent" commits that preceded it in3908history.39093910Normally a "commit" has one parent: the previous state of the tree3911before a certain change was made. However, sometimes it can have two3912or more parent commits, in which case we call it a "merge", due to the3913fact that such a commit brings together ("merges") two or more3914previous states represented by other commits.39153916In other words, while a "tree" represents a particular directory state3917of a working directory, a "commit" represents that state in time,3918and explains how we got there.39193920You create a commit object by giving it the tree that describes the3921state at the time of the commit, and a list of parents:39223923-------------------------------------------------3924$ git commit-tree <tree> -p <parent> [(-p <parent2>)...]3925-------------------------------------------------39263927and then giving the reason for the commit on stdin (either through3928redirection from a pipe or file, or by just typing it at the tty).39293930`git commit-tree` will return the name of the object that represents3931that commit, and you should save it away for later use. Normally,3932you'd commit a new `HEAD` state, and while Git doesn't care where you3933save the note about that state, in practice we tend to just write the3934result to the file pointed at by `.git/HEAD`, so that we can always see3935what the last committed state was.39363937Here is an ASCII art by Jon Loeliger that illustrates how3938various pieces fit together.39393940------------39413942 commit-tree3943 commit obj3944 +----+3945 | |3946 | |3947 V V3948 +-----------+3949 | Object DB |3950 | Backing |3951 | Store |3952 +-----------+3953 ^3954 write-tree | |3955 tree obj | |3956 | | read-tree3957 | | tree obj3958 V3959 +-----------+3960 | Index |3961 | "cache" |3962 +-----------+3963 update-index ^3964 blob obj | |3965 | |3966 checkout-index -u | | checkout-index3967 stat | | blob obj3968 V3969 +-----------+3970 | Working |3971 | Directory |3972 +-----------+39733974------------397539763977[[examining-the-data]]3978Examining the data3979------------------39803981You can examine the data represented in the object database and the3982index with various helper tools. For every object, you can use3983linkgit:git-cat-file[1] to examine details about the3984object:39853986-------------------------------------------------3987$ git cat-file -t <objectname>3988-------------------------------------------------39893990shows the type of the object, and once you have the type (which is3991usually implicit in where you find the object), you can use39923993-------------------------------------------------3994$ git cat-file blob|tree|commit|tag <objectname>3995-------------------------------------------------39963997to show its contents. NOTE! Trees have binary content, and as a result3998there is a special helper for showing that content, called3999`git ls-tree`, which turns the binary content into a more easily4000readable form.40014002It's especially instructive to look at "commit" objects, since those4003tend to be small and fairly self-explanatory. In particular, if you4004follow the convention of having the top commit name in `.git/HEAD`,4005you can do40064007-------------------------------------------------4008$ git cat-file commit HEAD4009-------------------------------------------------40104011to see what the top commit was.40124013[[merging-multiple-trees]]4014Merging multiple trees4015----------------------40164017Git helps you do a three-way merge, which you can expand to n-way by4018repeating the merge procedure arbitrary times until you finally4019"commit" the state. The normal situation is that you'd only do one4020three-way merge (two parents), and commit it, but if you like to, you4021can do multiple parents in one go.40224023To do a three-way merge, you need the two sets of "commit" objects4024that you want to merge, use those to find the closest common parent (a4025third "commit" object), and then use those commit objects to find the4026state of the directory ("tree" object) at these points.40274028To get the "base" for the merge, you first look up the common parent4029of two commits with40304031-------------------------------------------------4032$ git merge-base <commit1> <commit2>4033-------------------------------------------------40344035which will return you the commit they are both based on. You should4036now look up the "tree" objects of those commits, which you can easily4037do with (for example)40384039-------------------------------------------------4040$ git cat-file commit <commitname> | head -14041-------------------------------------------------40424043since the tree object information is always the first line in a commit4044object.40454046Once you know the three trees you are going to merge (the one "original"4047tree, aka the common tree, and the two "result" trees, aka the branches4048you want to merge), you do a "merge" read into the index. This will4049complain if it has to throw away your old index contents, so you should4050make sure that you've committed those--in fact you would normally4051always do a merge against your last commit (which should thus match what4052you have in your current index anyway).40534054To do the merge, do40554056-------------------------------------------------4057$ git read-tree -m -u <origtree> <yourtree> <targettree>4058-------------------------------------------------40594060which will do all trivial merge operations for you directly in the4061index file, and you can just write the result out with4062`git write-tree`.406340644065[[merging-multiple-trees-2]]4066Merging multiple trees, continued4067---------------------------------40684069Sadly, many merges aren't trivial. If there are files that have4070been added, moved or removed, or if both branches have modified the4071same file, you will be left with an index tree that contains "merge4072entries" in it. Such an index tree can 'NOT' be written out to a tree4073object, and you will have to resolve any such merge clashes using4074other tools before you can write out the result.40754076You can examine such index state with `git ls-files --unmerged`4077command. An example:40784079------------------------------------------------4080$ git read-tree -m $orig HEAD $target4081$ git ls-files --unmerged4082100644 263414f423d0e4d70dae8fe53fa34614ff3e2860 1 hello.c4083100644 06fa6a24256dc7e560efa5687fa84b51f0263c3a 2 hello.c4084100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello.c4085------------------------------------------------40864087Each line of the `git ls-files --unmerged` output begins with4088the blob mode bits, blob SHA-1, 'stage number', and the4089filename. The 'stage number' is Git's way to say which tree it4090came from: stage 1 corresponds to the `$orig` tree, stage 2 to4091the `HEAD` tree, and stage 3 to the `$target` tree.40924093Earlier we said that trivial merges are done inside4094`git read-tree -m`. For example, if the file did not change4095from `$orig` to `HEAD` nor `$target`, or if the file changed4096from `$orig` to `HEAD` and `$orig` to `$target` the same way,4097obviously the final outcome is what is in `HEAD`. What the4098above example shows is that file `hello.c` was changed from4099`$orig` to `HEAD` and `$orig` to `$target` in a different way.4100You could resolve this by running your favorite 3-way merge4101program, e.g. `diff3`, `merge`, or Git's own merge-file, on4102the blob objects from these three stages yourself, like this:41034104------------------------------------------------4105$ git cat-file blob 263414f... >hello.c~14106$ git cat-file blob 06fa6a2... >hello.c~24107$ git cat-file blob cc44c73... >hello.c~34108$ git merge-file hello.c~2 hello.c~1 hello.c~34109------------------------------------------------41104111This would leave the merge result in `hello.c~2` file, along4112with conflict markers if there are conflicts. After verifying4113the merge result makes sense, you can tell Git what the final4114merge result for this file is by:41154116-------------------------------------------------4117$ mv -f hello.c~2 hello.c4118$ git update-index hello.c4119-------------------------------------------------41204121When a path is in the "unmerged" state, running `git update-index` for4122that path tells Git to mark the path resolved.41234124The above is the description of a Git merge at the lowest level,4125to help you understand what conceptually happens under the hood.4126In practice, nobody, not even Git itself, runs `git cat-file` three times4127for this. There is a `git merge-index` program that extracts the4128stages to temporary files and calls a "merge" script on it:41294130-------------------------------------------------4131$ git merge-index git-merge-one-file hello.c4132-------------------------------------------------41334134and that is what higher level `git merge -s resolve` is implemented with.41354136[[hacking-git]]4137Hacking Git4138===========41394140This chapter covers internal details of the Git implementation which4141probably only Git developers need to understand.41424143[[object-details]]4144Object storage format4145---------------------41464147All objects have a statically determined "type" which identifies the4148format of the object (i.e. how it is used, and how it can refer to other4149objects). There are currently four different object types: "blob",4150"tree", "commit", and "tag".41514152Regardless of object type, all objects share the following4153characteristics: they are all deflated with zlib, and have a header4154that not only specifies their type, but also provides size information4155about the data in the object. It's worth noting that the SHA-1 hash4156that is used to name the object is the hash of the original data4157plus this header, so `sha1sum` 'file' does not match the object name4158for 'file'.4159(Historical note: in the dawn of the age of Git the hash4160was the SHA-1 of the 'compressed' object.)41614162As a result, the general consistency of an object can always be tested4163independently of the contents or the type of the object: all objects can4164be validated by verifying that (a) their hashes match the content of the4165file and (b) the object successfully inflates to a stream of bytes that4166forms a sequence of4167`<ascii type without space> + <space> + <ascii decimal size> +4168<byte\0> + <binary object data>`.41694170The structured objects can further have their structure and4171connectivity to other objects verified. This is generally done with4172the `git fsck` program, which generates a full dependency graph4173of all objects, and verifies their internal consistency (in addition4174to just verifying their superficial consistency through the hash).41754176[[birdview-on-the-source-code]]4177A birds-eye view of Git's source code4178-------------------------------------41794180It is not always easy for new developers to find their way through Git's4181source code. This section gives you a little guidance to show where to4182start.41834184A good place to start is with the contents of the initial commit, with:41854186----------------------------------------------------4187$ git checkout e83c51634188----------------------------------------------------41894190The initial revision lays the foundation for almost everything Git has4191today, but is small enough to read in one sitting.41924193Note that terminology has changed since that revision. For example, the4194README in that revision uses the word "changeset" to describe what we4195now call a <<def_commit_object,commit>>.41964197Also, we do not call it "cache" any more, but rather "index"; however, the4198file is still called `cache.h`. Remark: Not much reason to change it now,4199especially since there is no good single name for it anyway, because it is4200basically _the_ header file which is included by _all_ of Git's C sources.42014202If you grasp the ideas in that initial commit, you should check out a4203more recent version and skim `cache.h`, `object.h` and `commit.h`.42044205In the early days, Git (in the tradition of UNIX) was a bunch of programs4206which were extremely simple, and which you used in scripts, piping the4207output of one into another. This turned out to be good for initial4208development, since it was easier to test new things. However, recently4209many of these parts have become builtins, and some of the core has been4210"libified", i.e. put into libgit.a for performance, portability reasons,4211and to avoid code duplication.42124213By now, you know what the index is (and find the corresponding data4214structures in `cache.h`), and that there are just a couple of object types4215(blobs, trees, commits and tags) which inherit their common structure from4216`struct object`, which is their first member (and thus, you can cast e.g.4217`(struct object *)commit` to achieve the _same_ as `&commit->object`, i.e.4218get at the object name and flags).42194220Now is a good point to take a break to let this information sink in.42214222Next step: get familiar with the object naming. Read <<naming-commits>>.4223There are quite a few ways to name an object (and not only revisions!).4224All of these are handled in `sha1_name.c`. Just have a quick look at4225the function `get_sha1()`. A lot of the special handling is done by4226functions like `get_sha1_basic()` or the likes.42274228This is just to get you into the groove for the most libified part of Git:4229the revision walker.42304231Basically, the initial version of `git log` was a shell script:42324233----------------------------------------------------------------4234$ git-rev-list --pretty $(git-rev-parse --default HEAD "$@") | \4235 LESS=-S ${PAGER:-less}4236----------------------------------------------------------------42374238What does this mean?42394240`git rev-list` is the original version of the revision walker, which4241_always_ printed a list of revisions to stdout. It is still functional,4242and needs to, since most new Git commands start out as scripts using4243`git rev-list`.42444245`git rev-parse` is not as important any more; it was only used to filter out4246options that were relevant for the different plumbing commands that were4247called by the script.42484249Most of what `git rev-list` did is contained in `revision.c` and4250`revision.h`. It wraps the options in a struct named `rev_info`, which4251controls how and what revisions are walked, and more.42524253The original job of `git rev-parse` is now taken by the function4254`setup_revisions()`, which parses the revisions and the common command line4255options for the revision walker. This information is stored in the struct4256`rev_info` for later consumption. You can do your own command line option4257parsing after calling `setup_revisions()`. After that, you have to call4258`prepare_revision_walk()` for initialization, and then you can get the4259commits one by one with the function `get_revision()`.42604261If you are interested in more details of the revision walking process,4262just have a look at the first implementation of `cmd_log()`; call4263`git show v1.3.0~155^2~4` and scroll down to that function (note that you4264no longer need to call `setup_pager()` directly).42654266Nowadays, `git log` is a builtin, which means that it is _contained_ in the4267command `git`. The source side of a builtin is42684269- a function called `cmd_<bla>`, typically defined in `builtin/<bla.c>`4270 (note that older versions of Git used to have it in `builtin-<bla>.c`4271 instead), and declared in `builtin.h`.42724273- an entry in the `commands[]` array in `git.c`, and42744275- an entry in `BUILTIN_OBJECTS` in the `Makefile`.42764277Sometimes, more than one builtin is contained in one source file. For4278example, `cmd_whatchanged()` and `cmd_log()` both reside in `builtin/log.c`,4279since they share quite a bit of code. In that case, the commands which are4280_not_ named like the `.c` file in which they live have to be listed in4281`BUILT_INS` in the `Makefile`.42824283`git log` looks more complicated in C than it does in the original script,4284but that allows for a much greater flexibility and performance.42854286Here again it is a good point to take a pause.42874288Lesson three is: study the code. Really, it is the best way to learn about4289the organization of Git (after you know the basic concepts).42904291So, think about something which you are interested in, say, "how can I4292access a blob just knowing the object name of it?". The first step is to4293find a Git command with which you can do it. In this example, it is either4294`git show` or `git cat-file`.42954296For the sake of clarity, let's stay with `git cat-file`, because it42974298- is plumbing, and42994300- was around even in the initial commit (it literally went only through4301 some 20 revisions as `cat-file.c`, was renamed to `builtin/cat-file.c`4302 when made a builtin, and then saw less than 10 versions).43034304So, look into `builtin/cat-file.c`, search for `cmd_cat_file()` and look what4305it does.43064307------------------------------------------------------------------4308 git_config(git_default_config);4309 if (argc != 3)4310 usage("git cat-file [-t|-s|-e|-p|<type>] <sha1>");4311 if (get_sha1(argv[2], sha1))4312 die("Not a valid object name %s", argv[2]);4313------------------------------------------------------------------43144315Let's skip over the obvious details; the only really interesting part4316here is the call to `get_sha1()`. It tries to interpret `argv[2]` as an4317object name, and if it refers to an object which is present in the current4318repository, it writes the resulting SHA-1 into the variable `sha1`.43194320Two things are interesting here:43214322- `get_sha1()` returns 0 on _success_. This might surprise some new4323 Git hackers, but there is a long tradition in UNIX to return different4324 negative numbers in case of different errors--and 0 on success.43254326- the variable `sha1` in the function signature of `get_sha1()` is `unsigned4327 char *`, but is actually expected to be a pointer to `unsigned4328 char[20]`. This variable will contain the 160-bit SHA-1 of the given4329 commit. Note that whenever a SHA-1 is passed as `unsigned char *`, it4330 is the binary representation, as opposed to the ASCII representation in4331 hex characters, which is passed as `char *`.43324333You will see both of these things throughout the code.43344335Now, for the meat:43364337-----------------------------------------------------------------------------4338 case 0:4339 buf = read_object_with_reference(sha1, argv[1], &size, NULL);4340-----------------------------------------------------------------------------43414342This is how you read a blob (actually, not only a blob, but any type of4343object). To know how the function `read_object_with_reference()` actually4344works, find the source code for it (something like `git grep4345read_object_with | grep ":[a-z]"` in the Git repository), and read4346the source.43474348To find out how the result can be used, just read on in `cmd_cat_file()`:43494350-----------------------------------4351 write_or_die(1, buf, size);4352-----------------------------------43534354Sometimes, you do not know where to look for a feature. In many such cases,4355it helps to search through the output of `git log`, and then `git show` the4356corresponding commit.43574358Example: If you know that there was some test case for `git bundle`, but4359do not remember where it was (yes, you _could_ `git grep bundle t/`, but that4360does not illustrate the point!):43614362------------------------4363$ git log --no-merges t/4364------------------------43654366In the pager (`less`), just search for "bundle", go a few lines back,4367and see that it is in commit 18449ab0... Now just copy this object name,4368and paste it into the command line43694370-------------------4371$ git show 18449ab04372-------------------43734374Voila.43754376Another example: Find out what to do in order to make some script a4377builtin:43784379-------------------------------------------------4380$ git log --no-merges --diff-filter=A builtin/*.c4381-------------------------------------------------43824383You see, Git is actually the best tool to find out about the source of Git4384itself!43854386[[glossary]]4387Git Glossary4388============43894390include::glossary-content.txt[]43914392[[git-quick-start]]4393Appendix A: Git Quick Reference4394===============================43954396This is a quick summary of the major commands; the previous chapters4397explain how these work in more detail.43984399[[quick-creating-a-new-repository]]4400Creating a new repository4401-------------------------44024403From a tarball:44044405-----------------------------------------------4406$ tar xzf project.tar.gz4407$ cd project4408$ git init4409Initialized empty Git repository in .git/4410$ git add .4411$ git commit4412-----------------------------------------------44134414From a remote repository:44154416-----------------------------------------------4417$ git clone git://example.com/pub/project.git4418$ cd project4419-----------------------------------------------44204421[[managing-branches]]4422Managing branches4423-----------------44244425-----------------------------------------------4426$ git branch # list all local branches in this repo4427$ git checkout test # switch working directory to branch "test"4428$ git branch new # create branch "new" starting at current HEAD4429$ git branch -d new # delete branch "new"4430-----------------------------------------------44314432Instead of basing a new branch on current HEAD (the default), use:44334434-----------------------------------------------4435$ git branch new test # branch named "test"4436$ git branch new v2.6.15 # tag named v2.6.154437$ git branch new HEAD^ # commit before the most recent4438$ git branch new HEAD^^ # commit before that4439$ git branch new test~10 # ten commits before tip of branch "test"4440-----------------------------------------------44414442Create and switch to a new branch at the same time:44434444-----------------------------------------------4445$ git checkout -b new v2.6.154446-----------------------------------------------44474448Update and examine branches from the repository you cloned from:44494450-----------------------------------------------4451$ git fetch # update4452$ git branch -r # list4453 origin/master4454 origin/next4455 ...4456$ git checkout -b masterwork origin/master4457-----------------------------------------------44584459Fetch a branch from a different repository, and give it a new4460name in your repository:44614462-----------------------------------------------4463$ git fetch git://example.com/project.git theirbranch:mybranch4464$ git fetch git://example.com/project.git v2.6.15:mybranch4465-----------------------------------------------44664467Keep a list of repositories you work with regularly:44684469-----------------------------------------------4470$ git remote add example git://example.com/project.git4471$ git remote # list remote repositories4472example4473origin4474$ git remote show example # get details4475* remote example4476 URL: git://example.com/project.git4477 Tracked remote branches4478 master4479 next4480 ...4481$ git fetch example # update branches from example4482$ git branch -r # list all remote branches4483-----------------------------------------------448444854486[[exploring-history]]4487Exploring history4488-----------------44894490-----------------------------------------------4491$ gitk # visualize and browse history4492$ git log # list all commits4493$ git log src/ # ...modifying src/4494$ git log v2.6.15..v2.6.16 # ...in v2.6.16, not in v2.6.154495$ git log master..test # ...in branch test, not in branch master4496$ git log test..master # ...in branch master, but not in test4497$ git log test...master # ...in one branch, not in both4498$ git log -S'foo()' # ...where difference contain "foo()"4499$ git log --since="2 weeks ago"4500$ git log -p # show patches as well4501$ git show # most recent commit4502$ git diff v2.6.15..v2.6.16 # diff between two tagged versions4503$ git diff v2.6.15..HEAD # diff with current head4504$ git grep "foo()" # search working directory for "foo()"4505$ git grep v2.6.15 "foo()" # search old tree for "foo()"4506$ git show v2.6.15:a.txt # look at old version of a.txt4507-----------------------------------------------45084509Search for regressions:45104511-----------------------------------------------4512$ git bisect start4513$ git bisect bad # current version is bad4514$ git bisect good v2.6.13-rc2 # last known good revision4515Bisecting: 675 revisions left to test after this4516 # test here, then:4517$ git bisect good # if this revision is good, or4518$ git bisect bad # if this revision is bad.4519 # repeat until done.4520-----------------------------------------------45214522[[making-changes]]4523Making changes4524--------------45254526Make sure Git knows who to blame:45274528------------------------------------------------4529$ cat >>~/.gitconfig <<\EOF4530[user]4531 name = Your Name Comes Here4532 email = you@yourdomain.example.com4533EOF4534------------------------------------------------45354536Select file contents to include in the next commit, then make the4537commit:45384539-----------------------------------------------4540$ git add a.txt # updated file4541$ git add b.txt # new file4542$ git rm c.txt # old file4543$ git commit4544-----------------------------------------------45454546Or, prepare and create the commit in one step:45474548-----------------------------------------------4549$ git commit d.txt # use latest content only of d.txt4550$ git commit -a # use latest content of all tracked files4551-----------------------------------------------45524553[[merging]]4554Merging4555-------45564557-----------------------------------------------4558$ git merge test # merge branch "test" into the current branch4559$ git pull git://example.com/project.git master4560 # fetch and merge in remote branch4561$ git pull . test # equivalent to git merge test4562-----------------------------------------------45634564[[sharing-your-changes]]4565Sharing your changes4566--------------------45674568Importing or exporting patches:45694570-----------------------------------------------4571$ git format-patch origin..HEAD # format a patch for each commit4572 # in HEAD but not in origin4573$ git am mbox # import patches from the mailbox "mbox"4574-----------------------------------------------45754576Fetch a branch in a different Git repository, then merge into the4577current branch:45784579-----------------------------------------------4580$ git pull git://example.com/project.git theirbranch4581-----------------------------------------------45824583Store the fetched branch into a local branch before merging into the4584current branch:45854586-----------------------------------------------4587$ git pull git://example.com/project.git theirbranch:mybranch4588-----------------------------------------------45894590After creating commits on a local branch, update the remote4591branch with your commits:45924593-----------------------------------------------4594$ git push ssh://example.com/project.git mybranch:theirbranch4595-----------------------------------------------45964597When remote and local branch are both named "test":45984599-----------------------------------------------4600$ git push ssh://example.com/project.git test4601-----------------------------------------------46024603Shortcut version for a frequently used remote repository:46044605-----------------------------------------------4606$ git remote add example ssh://example.com/project.git4607$ git push example test4608-----------------------------------------------46094610[[repository-maintenance]]4611Repository maintenance4612----------------------46134614Check for corruption:46154616-----------------------------------------------4617$ git fsck4618-----------------------------------------------46194620Recompress, remove unused cruft:46214622-----------------------------------------------4623$ git gc4624-----------------------------------------------462546264627[[todo]]4628Appendix B: Notes and todo list for this manual4629===============================================46304631This is a work in progress.46324633The basic requirements:46344635- It must be readable in order, from beginning to end, by someone4636 intelligent with a basic grasp of the UNIX command line, but without4637 any special knowledge of Git. If necessary, any other prerequisites4638 should be specifically mentioned as they arise.4639- Whenever possible, section headings should clearly describe the task4640 they explain how to do, in language that requires no more knowledge4641 than necessary: for example, "importing patches into a project" rather4642 than "the `git am` command"46434644Think about how to create a clear chapter dependency graph that will4645allow people to get to important topics without necessarily reading4646everything in between.46474648Scan `Documentation/` for other stuff left out; in particular:46494650- howto's4651- some of `technical/`?4652- hooks4653- list of commands in linkgit:git[1]46544655Scan email archives for other stuff left out46564657Scan man pages to see if any assume more background than this manual4658provides.46594660Simplify beginning by suggesting disconnected head instead of4661temporary branch creation?46624663Add more good examples. Entire sections of just cookbook examples4664might be a good idea; maybe make an "advanced examples" section a4665standard end-of-chapter section?46664667Include cross-references to the glossary, where appropriate.46684669Document shallow clones? See draft 1.5.0 release notes for some4670documentation.46714672Add a section on working with other version control systems, including4673CVS, Subversion, and just imports of series of release tarballs.46744675More details on gitweb?46764677Write a chapter on using plumbing and writing scripts.46784679Alternates, clone -reference, etc.46804681More on recovery from repository corruption. See:4682 http://marc.info/?l=git&m=117263864820799&w=24683 http://marc.info/?l=git&m=117147855503798&w=2