]> git.ipfire.org Git - thirdparty/git.git/blame - Documentation/user-manual.txt
Fix some typos and improve wording
[thirdparty/git.git] / Documentation / user-manual.txt
CommitLineData
333d7d37
TA
1Git User Manual
2_______________
99eaefdd
BF
3
4Git is a fast distributed revision control system.
5
02783075 6This manual is designed to be readable by someone with basic UNIX
2de9b711 7command-line skills, but no previous knowledge of Git.
d19fbc3c 8
2624d9a5
BF
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.
ef89f701 13
2624d9a5 14People needing to do actual development will also want to read
aa971cb9 15<<Developing-With-git>> and <<sharing-development>>.
6bd9b682
BF
16
17Further chapters cover more specialized topics.
18
d19fbc3c 19Comprehensive reference documentation is available through the man
b3d98887 20pages, or linkgit:git-help[1] command. For example, for the command
1249d8ad 21`git clone <repo>`, you can either use:
d19fbc3c
BF
22
23------------------------------------------------
24$ man git-clone
25------------------------------------------------
26
b3d98887
CC
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
2de9b711 36See also <<git-quick-start>> for a brief overview of Git commands,
2624d9a5 37without any explanation.
b181d57f 38
99f171bb 39Finally, see <<todo>> for ways that you can help make this manual more
2624d9a5 40complete.
b181d57f 41
b181d57f 42
e34caace 43[[repositories-and-branches]]
d19fbc3c
BF
44Repositories and Branches
45=========================
46
e34caace 47[[how-to-get-a-git-repository]]
2de9b711 48How to get a Git repository
d19fbc3c
BF
49---------------------------
50
2de9b711 51It will be useful to have a Git repository to experiment with as you
d19fbc3c
BF
52read this manual.
53
5162e697 54The best way to get one is by using the linkgit:git-clone[1] command to
a5f90f31
BF
55download a copy of an existing repository. If you don't already have a
56project in mind, here are some interesting examples:
d19fbc3c
BF
57
58------------------------------------------------
4b9ced27 59 # Git itself (approx. 40MB download):
d19fbc3c 60$ git clone git://git.kernel.org/pub/scm/git/git.git
4b9ced27
TK
61 # the Linux kernel (approx. 640MB download):
62$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
d19fbc3c
BF
63------------------------------------------------
64
65The initial clone may be time-consuming for a large project, but you
66will only need to clone once.
67
283efb01
TK
68The clone command creates a new directory named after the project
69(`git` or `linux` in the examples above). After you cd into this
d19fbc3c 70directory, you will see that it contains a copy of the project files,
0c4a33b5 71called the <<def_working_tree,working tree>>, together with a special
1249d8ad 72top-level directory named `.git`, which contains all the information
0c4a33b5 73about the history of the project.
d19fbc3c 74
e34caace 75[[how-to-check-out]]
d19fbc3c
BF
76How to check out a different version of a project
77-------------------------------------------------
78
a2ef9d63
BF
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
2de9b711 81interrelated snapshots of the project's contents. In Git each such
a2ef9d63 82version is called a <<def_commit,commit>>.
d19fbc3c 83
0c4a33b5
BF
84Those snapshots aren't necessarily all arranged in a single line from
85oldest to newest; instead, work may simultaneously proceed along
57283291 86parallel lines of development, called <<def_branch,branches>>, which may
0c4a33b5
BF
87merge and diverge.
88
2de9b711 89A single Git repository can track development on multiple branches. It
0c4a33b5 90does this by keeping a list of <<def_head,heads>> which reference the
5162e697 91latest commit on each branch; the linkgit:git-branch[1] command shows
81b6c950 92you the list of branch heads:
d19fbc3c
BF
93
94------------------------------------------------
95$ git branch
96* master
97------------------------------------------------
98
4f752407
BF
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.
d19fbc3c 102
81b6c950
BF
103Most projects also use <<def_tag,tags>>. Tags, like heads, are
104references into the project's history, and can be listed using the
5162e697 105linkgit:git-tag[1] command:
d19fbc3c
BF
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
fe4b3e59 121Tags are expected to always point at the same version of a project,
81b6c950 122while heads are expected to advance as development progresses.
fe4b3e59 123
81b6c950 124Create a new branch head pointing to one of these versions and check it
5162e697 125out using linkgit:git-checkout[1]:
d19fbc3c
BF
126
127------------------------------------------------
128$ git checkout -b new v2.6.13
129------------------------------------------------
130
131The working directory then reflects the contents that the project had
5162e697 132when it was tagged v2.6.13, and linkgit:git-branch[1] shows two
d19fbc3c
BF
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
81b6c950 148Note that if the current branch head was your only reference to a
d19fbc3c 149particular point in history, then resetting that branch may leave you
81b6c950
BF
150with no way to find the history it used to point to; so use this command
151carefully.
d19fbc3c 152
e34caace 153[[understanding-commits]]
d19fbc3c
BF
154Understanding History: Commits
155------------------------------
156
157Every change in the history of a project is represented by a commit.
5162e697 158The linkgit:git-show[1] command shows the most recent commit on the
d19fbc3c
BF
159current branch:
160
161------------------------------------------------
162$ git show
e2618ff4
BF
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 @@
d19fbc3c 176
e2618ff4
BF
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) {
d19fbc3c
BF
184------------------------------------------------
185
186As you can see, a commit shows who made the latest change, what they
187did, and why.
188
35121930 189Every commit has a 40-hexdigit id, sometimes called the "object name" or the
1249d8ad 190"SHA-1 id", shown on the first line of the `git show` output. You can usually
35121930
BF
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
2de9b711 200In fact, in <<git-concepts>> we shall see that everything stored in Git
35121930
BF
201history, including file data and directory contents, is stored in an object
202with a name that is a hash of its contents.
d19fbc3c 203
e34caace 204[[understanding-reachability]]
d19fbc3c
BF
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
2de9b711 213However, the commits do not form a simple list; Git allows lines of
d19fbc3c
BF
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
5162e697 220The best way to see how this works is using the linkgit:gitk[1]
2de9b711 221command; running gitk now on a Git repository and looking for merge
ddd4ddef 222commits will help understand how Git organizes history.
d19fbc3c
BF
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
02783075 226that Y is a descendant of X, or that there is a chain of parents
d19fbc3c
BF
227leading from commit Y to commit X.
228
e34caace 229[[history-diagrams]]
3dff5379
PR
230Understanding history: History diagrams
231~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
d19fbc3c 232
2de9b711 233We will sometimes represent Git history using diagrams like the one
d19fbc3c
BF
234below. Commits are shown as "o", and the links between them with
235lines drawn with - / and \. Time goes left to right:
236
1dc71a91
BF
237
238................................................
d19fbc3c
BF
239 o--o--o <-- Branch A
240 /
241 o--o--o <-- master
242 \
243 o--o--o <-- Branch B
1dc71a91 244................................................
d19fbc3c
BF
245
246If we need to talk about a particular commit, the character "o" may
247be replaced with another letter or number.
248
e34caace 249[[what-is-a-branch]]
d19fbc3c
BF
250Understanding history: What is a branch?
251~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
252
81b6c950
BF
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
d19fbc3c
BF
258"branch A".
259
81b6c950
BF
260However, when no confusion will result, we often just use the term
261"branch" both for branches and for branch heads.
d19fbc3c 262
e34caace 263[[manipulating-branches]]
d19fbc3c
BF
264Manipulating branches
265---------------------
266
267Creating, deleting, and modifying branches is quick and easy; here's
268a summary of the commands:
269
1249d8ad 270`git branch`::
d19fbc3c 271 list all branches
1249d8ad
TK
272`git branch <branch>`::
273 create a new branch named `<branch>`, referencing the same
d19fbc3c 274 point in history as the current branch
1249d8ad
TK
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,
d19fbc3c 278 including using a branch name or a tag name
1249d8ad
TK
279`git branch -d <branch>`::
280 delete the branch `<branch>`; if the branch you are deleting
c64415e2
BF
281 points to a commit which is not reachable from the current
282 branch, this command will fail with a warning.
1249d8ad 283`git branch -D <branch>`::
d19fbc3c
BF
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
2de9b711 287 case it is safe to use this command to force Git to delete
d19fbc3c 288 the branch.
1249d8ad
TK
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
d19fbc3c
BF
294 check it out.
295
72a76c95 296The special symbol "HEAD" can always be used to refer to the current
1249d8ad
TK
297branch. In fact, Git uses a file named `HEAD` in the `.git` directory
298to remember which branch is current:
72a76c95
BF
299
300------------------------------------------------
301$ cat .git/HEAD
302ref: refs/heads/master
303------------------------------------------------
304
25d9f3fa 305[[detached-head]]
72a76c95
BF
306Examining an old version without creating a new branch
307------------------------------------------------------
308
6127c086 309The `git checkout` command normally expects a branch head, but will also
72a76c95
BF
310accept an arbitrary commit; for example, you can check out the commit
311referenced by a tag:
312
313------------------------------------------------
314$ git checkout v2.6.17
95f9be55
TA
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
72a76c95
BF
326HEAD is now at 427abfa... Linux v2.6.17
327------------------------------------------------
328
a6e5ef7d 329The HEAD then refers to the SHA-1 of the commit instead of to a branch,
72a76c95
BF
330and git branch shows that you are no longer on a branch:
331
332------------------------------------------------
333$ cat .git/HEAD
334427abfa28afedffadfca9dd8b067eb6d36bac53f
953f3d6f 335$ git branch
95f9be55 336* (detached from v2.6.17)
72a76c95
BF
337 master
338------------------------------------------------
339
340In this case we say that the HEAD is "detached".
341
953f3d6f
BF
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.
d19fbc3c 345
e34caace 346[[examining-remote-branches]]
d19fbc3c
BF
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
66a062a1
MM
353keeps branches which track each of those remote branches, called
354remote-tracking branches, which you
1249d8ad 355can view using the `-r` option to linkgit:git-branch[1]:
d19fbc3c
BF
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
66a062a1
MM
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
1249d8ad 373be updated by `git fetch` (hence `git pull`) and `git push`. See
66a062a1
MM
374<<Updating-a-repository-With-git-fetch>> for details.
375
45dfd403
JN
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:
d19fbc3c
BF
378
379------------------------------------------------
380$ git checkout -b my-todo-copy origin/todo
381------------------------------------------------
382
1249d8ad 383You can also check out `origin/todo` directly to examine it or
45dfd403
JN
384write a one-off patch. See <<detached-head,detached head>>.
385
2de9b711 386Note that the name "origin" is just the name that Git uses by default
d19fbc3c
BF
387to refer to the repository that you cloned from.
388
389[[how-git-stores-references]]
f60b9642
BF
390Naming branches, tags, and other references
391-------------------------------------------
d19fbc3c
BF
392
393Branches, remote-tracking branches, and tags are all references to
f60b9642 394commits. All references are named with a slash-separated path name
1249d8ad 395starting with `refs`; the names we've been using so far are actually
f60b9642 396shorthand:
d19fbc3c 397
1249d8ad
TK
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`.
d19fbc3c 401
f60b9642
BF
402The full name is occasionally useful if, for example, there ever
403exists a tag and a branch with the same name.
d19fbc3c 404
1249d8ad 405(Newly created refs are actually stored in the `.git/refs` directory,
fc74ecc1
BF
406under the path given by their name. However, for efficiency reasons
407they may also be packed together in a single file; see
5162e697 408linkgit:git-pack-refs[1]).
fc74ecc1 409
c64415e2
BF
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".
d19fbc3c 413
2de9b711 414For the complete list of paths which Git checks for references, and
f60b9642
BF
415the order it uses to decide which to choose when there are multiple
416references with the same shorthand name, see the "SPECIFYING
9d83e382 417REVISIONS" section of linkgit:gitrevisions[7].
d19fbc3c 418
aa971cb9 419[[Updating-a-repository-With-git-fetch]]
6127c086 420Updating a repository with git fetch
d19fbc3c
BF
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
1249d8ad 427The command `git fetch`, with no arguments, will update all of the
d19fbc3c
BF
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
e34caace 432[[fetching-branches]]
d5cd5de4
BF
433Fetching branches from other repositories
434-----------------------------------------
435
436You can also track branches from repositories other than the one you
5162e697 437cloned from, using linkgit:git-remote[1]:
d5cd5de4
BF
438
439-------------------------------------------------
34a25d4c
TK
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
d5cd5de4
BF
447-------------------------------------------------
448
449New remote-tracking branches will be stored under the shorthand name
34a25d4c 450that you gave `git remote add`, in this case `staging`:
d5cd5de4
BF
451
452-------------------------------------------------
453$ git branch -r
34a25d4c
TK
454 origin/HEAD -> origin/master
455 origin/master
456 staging/master
457 staging/staging-linus
458 staging/staging-next
d5cd5de4
BF
459-------------------------------------------------
460
1249d8ad
TK
461If you run `git fetch <remote>` later, the remote-tracking branches
462for the named `<remote>` will be updated.
d5cd5de4 463
1249d8ad 464If you examine the file `.git/config`, you will see that Git has added
d5cd5de4
BF
465a new stanza:
466
467-------------------------------------------------
468$ cat .git/config
469...
34a25d4c
TK
470[remote "staging"]
471 url = git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging.git
472 fetch = +refs/heads/*:refs/remotes/staging/*
d5cd5de4
BF
473...
474-------------------------------------------------
475
2de9b711 476This is what causes Git to track the remote's branches; you may modify
1249d8ad 477or delete these configuration options by editing `.git/config` with a
fc90c536 478text editor. (See the "CONFIGURATION FILE" section of
5162e697 479linkgit:git-config[1] for details.)
d5cd5de4 480
e34caace 481[[exploring-git-history]]
2de9b711 482Exploring Git history
d19fbc3c
BF
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
1130845b 487the contents of a file hierarchy, together with "commits" which show
d19fbc3c
BF
488the relationships between these snapshots.
489
490Git provides extremely flexible and fast tools for exploring the
491history of a project.
492
aacd404e 493We start with one specialized tool that is useful for finding the
d19fbc3c
BF
494commit that introduced a bug into a project.
495
e34caace 496[[using-bisect]]
d19fbc3c
BF
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
5162e697 504linkgit:git-bisect[1] command can help you do this:
d19fbc3c
BF
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
1249d8ad 514If you run `git branch` at this point, you'll see that Git has
0e25790f
CC
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:
d19fbc3c
BF
519
520-------------------------------------------------
521$ git bisect bad
522Bisecting: 1769 revisions left to test after this
523[7eff82c8b1511017ae605f0c99ac275a7e21b867] i2c-core: Drop useless bitmaskings
524-------------------------------------------------
525
2de9b711 526checks out an older version. Continue like this, telling Git at each
d19fbc3c
BF
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
5162e697 533linkgit:git-show[1], find out who wrote it, and mail them your bug
d19fbc3c
BF
534report with the commit id. Finally, run
535
536-------------------------------------------------
537$ git bisect reset
538-------------------------------------------------
539
0e25790f 540to return you to the branch you were on before.
d19fbc3c 541
6127c086 542Note that the version which `git bisect` checks out for you at each
d19fbc3c
BF
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-------------------------------------------------
04483524 549$ git bisect visualize
d19fbc3c
BF
550-------------------------------------------------
551
552which will run gitk and label the commit it chose with a marker that
843c81dc 553says "bisect". Choose a safe-looking commit nearby, note its commit
d19fbc3c
BF
554id, and check it out with:
555
556-------------------------------------------------
557$ git reset --hard fb47ddb2db...
558-------------------------------------------------
559
1249d8ad 560then test, run `bisect good` or `bisect bad` as appropriate, and
d19fbc3c
BF
561continue.
562
1249d8ad
TK
563Instead of `git bisect visualize` and then `git reset --hard
564fb47ddb2db...`, you might just want to tell Git that you want to skip
0e25790f
CC
565the current commit:
566
567-------------------------------------------------
568$ git bisect skip
569-------------------------------------------------
570
2de9b711 571In this case, though, Git may not eventually be able to tell the first
a0178ae2 572bad one between some first skipped commits and a later bad commit.
0e25790f
CC
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
1249d8ad
TK
576linkgit:git-bisect[1] for more information about this and other `git
577bisect` features.
0e25790f 578
e34caace 579[[naming-commits]]
d19fbc3c
BF
580Naming commits
581--------------
582
583We have seen several ways of naming commits already:
584
d55ae921 585 - 40-hexdigit object name
d19fbc3c
BF
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
eb6ae7f4 593There are many more; see the "SPECIFYING REVISIONS" section of the
9d83e382 594linkgit:gitrevisions[7] man page for the complete list of ways to
d19fbc3c
BF
595name revisions. Some examples:
596
597-------------------------------------------------
d55ae921 598$ git show fb47ddb2 # the first few characters of the object name
d19fbc3c
BF
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,
1249d8ad 606`^` and `~` follow the first parent listed in the commit, but you can
d19fbc3c
BF
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
6127c086 618`git reset`, which change the currently checked-out commit, generally
d19fbc3c
BF
619set ORIG_HEAD to the value HEAD had before the current operation.
620
6127c086
FC
621The `git fetch` operation always stores the head of the last fetched
622branch in FETCH_HEAD. For example, if you run `git fetch` without
d19fbc3c
BF
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
5162e697 635The linkgit:git-rev-parse[1] command is a low-level command that is
d55ae921
BF
636occasionally useful for translating some name for a commit to the object
637name for that commit:
aec053bb
BF
638
639-------------------------------------------------
640$ git rev-parse origin
641e05db0fd4f31dde7005f075a84f96b360d05984b
642-------------------------------------------------
643
e34caace 644[[creating-tags]]
d19fbc3c
BF
645Creating tags
646-------------
647
648We can also create a tag to refer to a particular commit; after
649running
650
651-------------------------------------------------
04483524 652$ git tag stable-1 1b2e1d63ff
d19fbc3c
BF
653-------------------------------------------------
654
1249d8ad 655You can use `stable-1` to refer to the commit 1b2e1d63ff.
d19fbc3c 656
c64415e2
BF
657This creates a "lightweight" tag. If you would also like to include a
658comment with the tag, and possibly sign it cryptographically, then you
5162e697 659should create a tag object instead; see the linkgit:git-tag[1] man page
c64415e2 660for details.
d19fbc3c 661
e34caace 662[[browsing-revisions]]
d19fbc3c
BF
663Browsing revisions
664------------------
665
5162e697 666The linkgit:git-log[1] command can show lists of commits. On its
d19fbc3c
BF
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
1249d8ad 684commits since v2.5 which touch the `Makefile` or any file under `fs`:
d19fbc3c
BF
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
1249d8ad 696See the `--pretty` option in the linkgit:git-log[1] man page for more
d19fbc3c
BF
697display options.
698
699Note that git log starts with the most recent commit and works
2de9b711 700backwards through the parents; however, since Git history can contain
3dff5379 701multiple independent lines of development, the particular order that
d19fbc3c
BF
702commits are listed in may be somewhat arbitrary.
703
e34caace 704[[generating-diffs]]
d19fbc3c
BF
705Generating diffs
706----------------
707
708You can generate diffs between any two versions using
5162e697 709linkgit:git-diff[1]:
d19fbc3c
BF
710
711-------------------------------------------------
712$ git diff master..test
713-------------------------------------------------
714
5b98d9bc
BF
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
5162e697 724use linkgit:git-format-patch[1]:
d19fbc3c
BF
725
726-------------------------------------------------
727$ git format-patch master..test
728-------------------------------------------------
729
730will generate a file with a patch for each commit reachable from test
5b98d9bc 731but not from master.
d19fbc3c 732
e34caace 733[[viewing-old-file-versions]]
d19fbc3c
BF
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
2de9b711 747may be any path to a file tracked by Git.
d19fbc3c 748
e34caace 749[[history-examples]]
aec053bb
BF
750Examples
751--------
752
46acd3fa
BF
753[[counting-commits-on-a-branch]]
754Counting the number of commits on a branch
755~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
756
1249d8ad
TK
757Suppose you want to know how many commits you've made on `mybranch`
758since it diverged from `origin`:
46acd3fa
BF
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
a6e5ef7d 765lower-level command linkgit:git-rev-list[1], which just lists the SHA-1's
46acd3fa
BF
766of all the given commits:
767
768-------------------------------------------------
769$ git rev-list origin..mybranch | wc -l
770-------------------------------------------------
771
e34caace 772[[checking-for-equal-branches]]
aec053bb 773Check whether two branches point at the same history
2f99710c 774~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
aec053bb
BF
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
69f7ad73
BF
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
d55ae921 786routes. You could compare the object names:
aec053bb
BF
787
788-------------------------------------------------
789$ git rev-list origin
790e05db0fd4f31dde7005f075a84f96b360d05984b
791$ git rev-list master
792e05db0fd4f31dde7005f075a84f96b360d05984b
793-------------------------------------------------
794
1249d8ad 795Or you could recall that the `...` operator selects all commits
ddd4ddef 796reachable from either one reference or the other but not
ddd2369c 797both; so
aec053bb
BF
798
799-------------------------------------------------
800$ git log origin...master
801-------------------------------------------------
802
803will return no commits when the two branches are equal.
804
e34caace 805[[finding-tagged-descendants]]
b181d57f
BF
806Find first tagged version including a given fix
807~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
aec053bb 808
69f7ad73
BF
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
ddd4ddef 823or you can use linkgit:git-name-rev[1], which will give the commit a
b181d57f
BF
824name based on any tag it finds pointing to one of the commit's
825descendants:
826
827-------------------------------------------------
04483524 828$ git name-rev --tags e05db0fd
b181d57f
BF
829e05db0fd tags/v1.5.0-rc1^0~23
830-------------------------------------------------
831
5162e697 832The linkgit:git-describe[1] command does the opposite, naming the
b181d57f
BF
833revision using a tag on which the given commit is based:
834
835-------------------------------------------------
836$ git describe e05db0fd
04483524 837v1.5.0-rc0-260-ge05db0f
b181d57f
BF
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
5162e697 844given commit, you could use linkgit:git-merge-base[1]:
b181d57f
BF
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-------------------------------------------------
4a7979ca 859$ git log v1.5.0-rc1..e05db0fd
b181d57f
BF
860-------------------------------------------------
861
4a7979ca 862will produce empty output if and only if v1.5.0-rc1 includes e05db0fd,
b181d57f 863because it outputs only commits that are not reachable from v1.5.0-rc1.
aec053bb 864
5162e697 865As yet another alternative, the linkgit:git-show-branch[1] command lists
4a7979ca 866the commits reachable from its arguments with a display on the left-hand
ddd4ddef
TA
867side that indicates which arguments that commit is reachable from.
868So, if you run something like
4a7979ca
BF
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
ddd4ddef 880then a line like
4a7979ca
BF
881
882-------------------------------------------------
883+ ++ [e05db0fd] Fix warnings in sha1_file.c - use C99 printf format if
884available
885-------------------------------------------------
886
ddd4ddef
TA
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.
4a7979ca 889
629d9f78
BF
890[[showing-commits-unique-to-a-branch]]
891Showing commits unique to a given branch
892~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4a7979ca 893
629d9f78 894Suppose you would like to see all the commits reachable from the branch
1249d8ad 895head named `master` but not from any other head in your repository.
d19fbc3c 896
629d9f78 897We can list all the heads in this repository with
5162e697 898linkgit:git-show-ref[1]:
d19fbc3c 899
629d9f78
BF
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-------------------------------------------------
d19fbc3c 908
1249d8ad 909We can get just the branch-head names, and remove `master`, with
629d9f78
BF
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-------------------------------------------------
c78974f7 932$ gitk $( git show-ref --heads ) --not $( git show-ref --tags )
629d9f78
BF
933-------------------------------------------------
934
9d83e382 935(See linkgit:gitrevisions[7] for explanations of commit-selecting
629d9f78
BF
936syntax such as `--not`.)
937
82c8bf28
BF
938[[making-a-release]]
939Creating a changelog and tarball for a software release
940~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
941
5162e697 942The linkgit:git-archive[1] command can create a tar or zip archive from
82c8bf28
BF
943any version of a project; for example:
944
945-------------------------------------------------
7ed1690c 946$ git archive -o latest.tar.gz --prefix=project/ HEAD
82c8bf28
BF
947-------------------------------------------------
948
7ed1690c
TK
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
1249d8ad 954Versions of Git older than 1.7.7 don't know about the `tar.gz` format,
7ed1690c
TK
955you'll need to use gzip explicitly:
956
957-------------------------------------------------
958$ git archive --format=tar --prefix=project/ HEAD | gzip >latest.tar.gz
959-------------------------------------------------
82c8bf28
BF
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.
4a7979ca 989
e1ba4c32 990[[Finding-commits-With-given-Content]]
187b0d80 991Finding commits referencing a file with given content
d5821de2 992~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187b0d80
BF
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-------------------------------------------------
477ff5b7 999$ git log --raw --abbrev=40 --pretty=oneline |
187b0d80
BF
1000 grep -B 1 `git hash-object filename`
1001-------------------------------------------------
1002
1003Figuring out why this works is left as an exercise to the (advanced)
5162e697
DM
1004student. The linkgit:git-log[1], linkgit:git-diff-tree[1], and
1005linkgit:git-hash-object[1] man pages may prove helpful.
187b0d80 1006
aa971cb9 1007[[Developing-With-git]]
2de9b711 1008Developing with Git
d19fbc3c
BF
1009===================
1010
e34caace 1011[[telling-git-your-name]]
2de9b711 1012Telling Git your name
d19fbc3c
BF
1013---------------------
1014
632cc3e6
TK
1015Before creating any commits, you should introduce yourself to Git.
1016The easiest way to do so is to use linkgit:git-config[1]:
1017
1018------------------------------------------------
1019$ git config --global user.name 'Your Name Comes Here'
1020$ git config --global user.email 'you@yourdomain.example.com'
1021------------------------------------------------
1022
1023Which will add the following to a file named `.gitconfig` in your
1024home directory:
d19fbc3c
BF
1025
1026------------------------------------------------
d19fbc3c
BF
1027[user]
1028 name = Your Name Comes Here
1029 email = you@yourdomain.example.com
d19fbc3c
BF
1030------------------------------------------------
1031
632cc3e6
TK
1032See the "CONFIGURATION FILE" section of linkgit:git-config[1] for
1033details on the configuration file. The file is plain text, so you can
1034also edit it with your favorite editor.
fc90c536 1035
d19fbc3c 1036
e34caace 1037[[creating-a-new-repository]]
d19fbc3c
BF
1038Creating a new repository
1039-------------------------
1040
1041Creating a new repository from scratch is very easy:
1042
1043-------------------------------------------------
1044$ mkdir project
1045$ cd project
f1d2b477 1046$ git init
d19fbc3c
BF
1047-------------------------------------------------
1048
1049If you have some initial content (say, a tarball):
1050
1051-------------------------------------------------
0ddd93b2 1052$ tar xzvf project.tar.gz
d19fbc3c 1053$ cd project
f1d2b477 1054$ git init
d19fbc3c
BF
1055$ git add . # include everything below ./ in the first commit:
1056$ git commit
1057-------------------------------------------------
1058
1059[[how-to-make-a-commit]]
ae25c67a 1060How to make a commit
d19fbc3c
BF
1061--------------------
1062
1063Creating a new commit takes three steps:
1064
1065 1. Making some changes to the working directory using your
1066 favorite editor.
2de9b711
TA
1067 2. Telling Git about your changes.
1068 3. Creating the commit using the content you told Git about
d19fbc3c
BF
1069 in step 2.
1070
1071In practice, you can interleave and repeat steps 1 and 2 as many
1072times as you want: in order to keep track of what you want committed
2de9b711 1073at step 3, Git maintains a snapshot of the tree's contents in a
d19fbc3c
BF
1074special staging area called "the index."
1075
01997b4a 1076At the beginning, the content of the index will be identical to
1249d8ad 1077that of the HEAD. The command `git diff --cached`, which shows
01997b4a
BF
1078the difference between the HEAD and the index, should therefore
1079produce no output at that point.
eb6ae7f4 1080
d19fbc3c
BF
1081Modifying the index is easy:
1082
1083To update the index with the new contents of a modified file, use
1084
1085-------------------------------------------------
1086$ git add path/to/file
1087-------------------------------------------------
1088
1089To add the contents of a new file to the index, use
1090
1091-------------------------------------------------
1092$ git add path/to/file
1093-------------------------------------------------
1094
eb6ae7f4 1095To remove a file from the index and from the working tree,
d19fbc3c
BF
1096
1097-------------------------------------------------
1098$ git rm path/to/file
1099-------------------------------------------------
1100
1101After each step you can verify that
1102
1103-------------------------------------------------
1104$ git diff --cached
1105-------------------------------------------------
1106
1107always shows the difference between the HEAD and the index file--this
1108is what you'd commit if you created the commit now--and that
1109
1110-------------------------------------------------
1111$ git diff
1112-------------------------------------------------
1113
1114shows the difference between the working tree and the index file.
1115
1249d8ad 1116Note that `git add` always adds just the current contents of a file
d19fbc3c 1117to the index; further changes to the same file will be ignored unless
6127c086 1118you run `git add` on the file again.
d19fbc3c
BF
1119
1120When you're ready, just run
1121
1122-------------------------------------------------
1123$ git commit
1124-------------------------------------------------
1125
2de9b711 1126and Git will prompt you for a commit message and then create the new
3dff5379 1127commit. Check to make sure it looks like what you expected with
d19fbc3c
BF
1128
1129-------------------------------------------------
1130$ git show
1131-------------------------------------------------
1132
1133As a special shortcut,
a6080a0a 1134
d19fbc3c
BF
1135-------------------------------------------------
1136$ git commit -a
1137-------------------------------------------------
1138
1139will update the index with any files that you've modified or removed
1140and create a commit, all in one step.
1141
1142A number of commands are useful for keeping track of what you're
1143about to commit:
1144
1145-------------------------------------------------
1146$ git diff --cached # difference between HEAD and the index; what
1130845b 1147 # would be committed if you ran "commit" now.
d19fbc3c
BF
1148$ git diff # difference between the index file and your
1149 # working directory; changes that would not
1150 # be included if you ran "commit" now.
c64415e2
BF
1151$ git diff HEAD # difference between HEAD and working tree; what
1152 # would be committed if you ran "commit -a" now.
d19fbc3c
BF
1153$ git status # a brief per-file summary of the above.
1154-------------------------------------------------
1155
5162e697 1156You can also use linkgit:git-gui[1] to create commits, view changes in
407c0c87
BF
1157the index and the working tree files, and individually select diff hunks
1158for inclusion in the index (by right-clicking on the diff hunk and
1159choosing "Stage Hunk For Commit").
1160
e34caace 1161[[creating-good-commit-messages]]
ae25c67a 1162Creating good commit messages
d19fbc3c
BF
1163-----------------------------
1164
1165Though not required, it's a good idea to begin the commit message
1166with a single short (less than 50 character) line summarizing the
1167change, followed by a blank line and then a more thorough
52ffe995
JW
1168description. The text up to the first blank line in a commit
1169message is treated as the commit title, and that title is used
2de9b711 1170throughout Git. For example, linkgit:git-format-patch[1] turns a
52ffe995
JW
1171commit into email, and it uses the title on the Subject line and the
1172rest of the commit in the body.
1173
d19fbc3c 1174
2dc53617
JH
1175[[ignoring-files]]
1176Ignoring files
1177--------------
1178
2de9b711 1179A project will often generate files that you do 'not' want to track with Git.
2dc53617 1180This typically includes files generated by a build process or temporary
2de9b711 1181backup files made by your editor. Of course, 'not' tracking files with Git
6127c086 1182is just a matter of 'not' calling `git add` on them. But it quickly becomes
2dc53617 1183annoying to have these untracked files lying around; e.g. they make
dcb11263
CJ
1184`git add .` practically useless, and they keep showing up in the output of
1185`git status`.
2dc53617 1186
1249d8ad
TK
1187You can tell Git to ignore certain files by creating a file called
1188`.gitignore` in the top level of your working directory, with contents
1189such as:
2dc53617
JH
1190
1191-------------------------------------------------
1192# Lines starting with '#' are considered comments.
464a8a7a 1193# Ignore any file named foo.txt.
2dc53617
JH
1194foo.txt
1195# Ignore (generated) html files,
1196*.html
1197# except foo.html which is maintained by hand.
1198!foo.html
1199# Ignore objects and archives.
1200*.[oa]
1201-------------------------------------------------
1202
5162e697 1203See linkgit:gitignore[5] for a detailed explanation of the syntax. You can
464a8a7a
BF
1204also place .gitignore files in other directories in your working tree, and they
1205will apply to those directories and their subdirectories. The `.gitignore`
1206files can be added to your repository like any other files (just run `git add
1207.gitignore` and `git commit`, as usual), which is convenient when the exclude
1208patterns (such as patterns matching build output files) would also make sense
1209for other users who clone your repository.
1210
1211If you wish the exclude patterns to affect only certain repositories
1212(instead of every repository for a given project), you may instead put
1249d8ad
TK
1213them in a file in your repository named `.git/info/exclude`, or in any
1214file specified by the `core.excludesfile` configuration variable.
1215Some Git commands can also take exclude patterns directly on the
1216command line. See linkgit:gitignore[5] for the details.
2dc53617 1217
e34caace 1218[[how-to-merge]]
ae25c67a 1219How to merge
d19fbc3c
BF
1220------------
1221
1222You can rejoin two diverging branches of development using
5162e697 1223linkgit:git-merge[1]:
d19fbc3c
BF
1224
1225-------------------------------------------------
1226$ git merge branchname
1227-------------------------------------------------
1228
1249d8ad 1229merges the development in the branch `branchname` into the current
e63ec003
MM
1230branch.
1231
1249d8ad 1232A merge is made by combining the changes made in `branchname` and the
e63ec003
MM
1233changes made up to the latest commit in your current branch since
1234their histories forked. The work tree is overwritten by the result of
1235the merge when this combining is done cleanly, or overwritten by a
1236half-merged results when this combining results in conflicts.
1237Therefore, if you have uncommitted changes touching the same files as
1238the ones impacted by the merge, Git will refuse to proceed. Most of
1239the 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 changes
1241away while you're doing the merge, and reapply them afterwards.
1242
6a5d0b0a 1243If the changes are independent enough, Git will automatically complete
e63ec003
MM
1244the merge and commit the result (or reuse an existing commit in case
1245of <<fast-forwards,fast-forward>>, see below). On the other hand,
1246if there are conflicts--for example, if the same file is
d19fbc3c
BF
1247modified in two different ways in the remote branch and the local
1248branch--then you are warned; the output may look something like this:
1249
1250-------------------------------------------------
fabbd8f6
BF
1251$ git merge next
1252 100% (4/4) done
1253Auto-merged file.txt
d19fbc3c
BF
1254CONFLICT (content): Merge conflict in file.txt
1255Automatic merge failed; fix conflicts and then commit the result.
1256-------------------------------------------------
1257
1258Conflict markers are left in the problematic files, and after
1259you resolve the conflicts manually, you can update the index
2de9b711 1260with the contents and run Git commit, as you normally would when
d19fbc3c
BF
1261creating a new file.
1262
1263If you examine the resulting commit using gitk, you will see that it
1264has two parents, one pointing to the top of the current branch, and
1265one to the top of the other branch.
1266
d19fbc3c
BF
1267[[resolving-a-merge]]
1268Resolving a merge
1269-----------------
1270
2de9b711 1271When a merge isn't resolved automatically, Git leaves the index and
d19fbc3c
BF
1272the working tree in a special state that gives you all the
1273information you need to help resolve the merge.
1274
1275Files with conflicts are marked specially in the index, so until you
5162e697 1276resolve the problem and update the index, linkgit:git-commit[1] will
ef561ac7 1277fail:
d19fbc3c
BF
1278
1279-------------------------------------------------
1280$ git commit
1281file.txt: needs merge
1282-------------------------------------------------
1283
5162e697 1284Also, linkgit:git-status[1] will list those files as "unmerged", and the
ef561ac7
BF
1285files with conflicts will have conflict markers added, like this:
1286
1287-------------------------------------------------
1288<<<<<<< HEAD:file.txt
1289Hello world
1290=======
1291Goodbye
1292>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
1293-------------------------------------------------
1294
1295All you need to do is edit the files to resolve the conflicts, and then
1296
1297-------------------------------------------------
1298$ git add file.txt
1299$ git commit
1300-------------------------------------------------
1301
1302Note that the commit message will already be filled in for you with
1303some information about the merge. Normally you can just use this
1304default message unchanged, but you may add additional commentary of
1305your own if desired.
1306
2de9b711 1307The above is all you need to know to resolve a simple merge. But Git
ef561ac7
BF
1308also provides more information to help resolve conflicts:
1309
e34caace 1310[[conflict-resolution]]
ef561ac7
BF
1311Getting conflict-resolution help during a merge
1312~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
d19fbc3c 1313
2de9b711 1314All of the changes that Git was able to merge automatically are
5162e697 1315already added to the index file, so linkgit:git-diff[1] shows only
ef561ac7 1316the conflicts. It uses an unusual syntax:
d19fbc3c
BF
1317
1318-------------------------------------------------
1319$ git diff
1320diff --cc file.txt
1321index 802992c,2b60207..0000000
1322--- a/file.txt
1323+++ b/file.txt
1324@@@ -1,1 -1,1 +1,5 @@@
1325++<<<<<<< HEAD:file.txt
1326 +Hello world
1327++=======
1328+ Goodbye
1329++>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
1330-------------------------------------------------
1331
1130845b 1332Recall that the commit which will be committed after we resolve this
d19fbc3c
BF
1333conflict will have two parents instead of the usual one: one parent
1334will be HEAD, the tip of the current branch; the other will be the
1335tip of the other branch, which is stored temporarily in MERGE_HEAD.
1336
ef561ac7
BF
1337During the merge, the index holds three versions of each file. Each of
1338these three "file stages" represents a different version of the file:
1339
1340-------------------------------------------------
1341$ git show :1:file.txt # the file in a common ancestor of both branches
4209752d
JH
1342$ git show :2:file.txt # the version from HEAD.
1343$ git show :3:file.txt # the version from MERGE_HEAD.
ef561ac7
BF
1344-------------------------------------------------
1345
4209752d
JH
1346When you ask linkgit:git-diff[1] to show the conflicts, it runs a
1347three-way diff between the conflicted merge results in the work tree with
1348stages 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).
ef561ac7
BF
1351
1352The diff above shows the differences between the working-tree version of
1353file.txt and the stage 2 and stage 3 versions. So instead of preceding
1249d8ad 1354each line by a single `+` or `-`, it now uses two columns: the first
ef561ac7
BF
1355column is used for differences between the first parent and the working
1356directory copy, and the second for differences between the second parent
1357and the working directory copy. (See the "COMBINED DIFF FORMAT" section
5162e697 1358of linkgit:git-diff-files[1] for a details of the format.)
ef561ac7
BF
1359
1360After resolving the conflict in the obvious way (but before updating the
1361index), the diff will look like:
d19fbc3c
BF
1362
1363-------------------------------------------------
1364$ git diff
1365diff --cc file.txt
1366index 802992c,2b60207..0000000
1367--- a/file.txt
1368+++ b/file.txt
1369@@@ -1,1 -1,1 +1,1 @@@
1370- Hello world
1371 -Goodbye
1372++Goodbye world
1373-------------------------------------------------
1374
1375This shows that our resolved version deleted "Hello world" from the
1376first parent, deleted "Goodbye" from the second parent, and added
1377"Goodbye world", which was previously absent from both.
1378
ef561ac7
BF
1379Some special diff options allow diffing the working directory against
1380any of these stages:
1381
1382-------------------------------------------------
1383$ git diff -1 file.txt # diff against stage 1
1384$ git diff --base file.txt # same as the above
1385$ git diff -2 file.txt # diff against stage 2
1386$ git diff --ours file.txt # same as the above
1387$ git diff -3 file.txt # diff against stage 3
1388$ git diff --theirs file.txt # same as the above.
1389-------------------------------------------------
1390
0cafe944 1391The linkgit:git-log[1] and linkgit:gitk[1] commands also provide special help
ef561ac7 1392for merges:
d19fbc3c
BF
1393
1394-------------------------------------------------
1395$ git log --merge
ef561ac7 1396$ gitk --merge
d19fbc3c
BF
1397-------------------------------------------------
1398
ef561ac7
BF
1399These will display all commits which exist only on HEAD or on
1400MERGE_HEAD, and which touch an unmerged file.
d19fbc3c 1401
5162e697 1402You may also use linkgit:git-mergetool[1], which lets you merge the
c7719fbe 1403unmerged files using external tools such as Emacs or kdiff3.
c64415e2 1404
ef561ac7 1405Each time you resolve the conflicts in a file and update the index:
d19fbc3c
BF
1406
1407-------------------------------------------------
1408$ git add file.txt
d19fbc3c
BF
1409-------------------------------------------------
1410
ef561ac7 1411the different stages of that file will be "collapsed", after which
6127c086 1412`git diff` will (by default) no longer show diffs for that file.
d19fbc3c
BF
1413
1414[[undoing-a-merge]]
ae25c67a 1415Undoing a merge
d19fbc3c
BF
1416---------------
1417
1418If you get stuck and decide to just give up and throw the whole mess
1419away, you can always return to the pre-merge state with
1420
1421-------------------------------------------------
1422$ git reset --hard HEAD
1423-------------------------------------------------
1424
1130845b 1425Or, if you've already committed the merge that you want to throw away,
d19fbc3c
BF
1426
1427-------------------------------------------------
1c73bb0e 1428$ git reset --hard ORIG_HEAD
d19fbc3c
BF
1429-------------------------------------------------
1430
1431However, this last command can be dangerous in some cases--never
1432throw away a commit you have already committed if that commit may
1433itself have been merged into another branch, as doing so may confuse
1434further merges.
1435
e34caace 1436[[fast-forwards]]
d19fbc3c
BF
1437Fast-forward merges
1438-------------------
1439
1440There is one special case not mentioned above, which is treated
1441differently. Normally, a merge results in a merge commit, with two
1442parents, one pointing at each of the two lines of development that
1443were merged.
1444
59723040 1445However, if the current branch is a descendant of the other--so every
2de9b711 1446commit present in the one is already contained in the other--then Git
a75d7b54 1447just performs a "fast-forward"; the head of the current branch is moved
59723040
BF
1448forward to point at the head of the merged-in branch, without any new
1449commits being created.
d19fbc3c 1450
e34caace 1451[[fixing-mistakes]]
b684f830
BF
1452Fixing mistakes
1453---------------
1454
1455If you've messed up the working tree, but haven't yet committed your
1456mistake, you can return the entire working tree to the last committed
1457state with
1458
1459-------------------------------------------------
1460$ git reset --hard HEAD
1461-------------------------------------------------
1462
1463If you make a commit that you later wish you hadn't, there are two
1464fundamentally different ways to fix the problem:
1465
1466 1. You can create a new commit that undoes whatever was done
93cbbd71 1467 by the old commit. This is the correct thing if your
b684f830
BF
1468 mistake has already been made public.
1469
1470 2. You can go back and modify the old commit. You should
1471 never do this if you have already made the history public;
2de9b711 1472 Git does not normally expect the "history" of a project to
b684f830
BF
1473 change, and cannot correctly perform repeated merges from
1474 a branch that has had its history changed.
1475
e34caace 1476[[reverting-a-commit]]
b684f830
BF
1477Fixing a mistake with a new commit
1478~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1479
1480Creating a new commit that reverts an earlier change is very easy;
5162e697 1481just pass the linkgit:git-revert[1] command a reference to the bad
b684f830
BF
1482commit; for example, to revert the most recent commit:
1483
1484-------------------------------------------------
1485$ git revert HEAD
1486-------------------------------------------------
1487
1488This will create a new commit which undoes the change in HEAD. You
1489will be given a chance to edit the commit message for the new commit.
1490
1491You can also revert an earlier change, for example, the next-to-last:
1492
1493-------------------------------------------------
1494$ git revert HEAD^
1495-------------------------------------------------
1496
2de9b711 1497In this case Git will attempt to undo the old change while leaving
b684f830
BF
1498intact any changes made since then. If more recent changes overlap
1499with the changes to be reverted, then you will be asked to fix
1500conflicts manually, just as in the case of <<resolving-a-merge,
1501resolving a merge>>.
1502
7cb192ea
BF
1503[[fixing-a-mistake-by-rewriting-history]]
1504Fixing a mistake by rewriting history
b684f830
BF
1505~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1506
1507If the problematic commit is the most recent commit, and you have not
1508yet made that commit public, then you may just
6127c086 1509<<undoing-a-merge,destroy it using `git reset`>>.
b684f830
BF
1510
1511Alternatively, you
1512can edit the working directory and update the index to fix your
1513mistake, just as if you were going to <<how-to-make-a-commit,create a
1514new commit>>, then run
1515
1516-------------------------------------------------
1517$ git commit --amend
1518-------------------------------------------------
1519
1520which will replace the old commit by a new commit incorporating your
1521changes, giving you a chance to edit the old commit message first.
1522
1523Again, you should never do this to a commit that may already have
5162e697 1524been merged into another branch; use linkgit:git-revert[1] instead in
b684f830
BF
1525that case.
1526
7cb192ea 1527It is also possible to replace commits further back in the history, but
b684f830
BF
1528this is an advanced topic to be left for
1529<<cleaning-up-history,another chapter>>.
1530
e34caace 1531[[checkout-of-path]]
b684f830
BF
1532Checking out an old version of a file
1533~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1534
1535In the process of undoing a previous bad change, you may find it
1536useful to check out an older version of a particular file using
6127c086 1537linkgit:git-checkout[1]. We've used `git checkout` before to switch
b684f830
BF
1538branches, but it has quite different behavior if it is given a path
1539name: the command
1540
1541-------------------------------------------------
1542$ git checkout HEAD^ path/to/file
1543-------------------------------------------------
1544
1545replaces path/to/file by the contents it had in the commit HEAD^, and
1546also updates the index to match. It does not change branches.
1547
1548If you just want to look at an old version of the file, without
1549modifying the working directory, you can do that with
5162e697 1550linkgit:git-show[1]:
b684f830
BF
1551
1552-------------------------------------------------
ed4eb0d8 1553$ git show HEAD^:path/to/file
b684f830
BF
1554-------------------------------------------------
1555
1556which will display the given version of the file.
1557
7a7cc594
JH
1558[[interrupted-work]]
1559Temporarily setting aside work in progress
1560~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1561
1562While you are in the middle of working on something complicated, you
1563find an unrelated but obvious and trivial bug. You would like to fix it
5162e697 1564before continuing. You can use linkgit:git-stash[1] to save the current
7a7cc594
JH
1565state of your work, and after fixing the bug (or, optionally after doing
1566so on a different branch and then coming back), unstash the
1567work-in-progress changes.
1568
1569------------------------------------------------
7a85f6ae 1570$ git stash save "work in progress for foo feature"
7a7cc594
JH
1571------------------------------------------------
1572
1573This command will save your changes away to the `stash`, and
1574reset your working tree and the index to match the tip of your
1575current branch. Then you can make your fix as usual.
1576
1577------------------------------------------------
1578... edit and test ...
1579$ git commit -a -m "blorpl: typofix"
1580------------------------------------------------
1581
1582After that, you can go back to what you were working on with
7b8988e1 1583`git stash pop`:
7a7cc594
JH
1584
1585------------------------------------------------
7b8988e1 1586$ git stash pop
7a7cc594
JH
1587------------------------------------------------
1588
1589
e34caace 1590[[ensuring-good-performance]]
d19fbc3c
BF
1591Ensuring good performance
1592-------------------------
1593
2de9b711 1594On large repositories, Git depends on compression to keep the history
901fd180 1595information from taking up too much space on disk or in memory. Some
e1ebf212 1596Git commands may automatically run linkgit:git-gc[1], so you don't
901fd180
TK
1597have to worry about running it manually. However, compressing a large
1598repository may take a while, so you may want to call `gc` explicitly
1599to avoid automatic compression kicking in when it is not convenient.
d19fbc3c 1600
e34caace
BF
1601
1602[[ensuring-reliability]]
11e016a3
BF
1603Ensuring reliability
1604--------------------
1605
e34caace 1606[[checking-for-corruption]]
11e016a3
BF
1607Checking the repository for corruption
1608~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1609
5162e697 1610The linkgit:git-fsck[1] command runs a number of self-consistency checks
1191ee18 1611on the repository, and reports on any problems. This may take some
c6a13b2c 1612time.
21dcb3b7
BF
1613
1614-------------------------------------------------
04e50e94 1615$ git fsck
21dcb3b7
BF
1616dangling commit 7281251ddd2a61e38657c827739c57015671a6b3
1617dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a63
1618dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b5
1619dangling blob 218761f9d90712d37a9c5e36f406f92202db07eb
1620dangling commit bf093535a34a4d35731aa2bd90fe6b176302f14f
1621dangling commit 8e4bec7f2ddaa268bef999853c25755452100f8e
1622dangling tree d50bb86186bf27b681d25af89d3b5b68382e4085
1623dangling tree b24c2473f1fd3d91352a624795be026d64c8841f
1624...
1625-------------------------------------------------
1626
c6a13b2c
JH
1627You will see informational messages on dangling objects. They are objects
1628that still exist in the repository but are no longer referenced by any of
1249d8ad 1629your branches, and can (and will) be removed after a while with `gc`.
b4ab1980 1630You can run `git fsck --no-dangling` to suppress these messages, and still
c6a13b2c 1631view real errors.
1cdade2c 1632
e34caace 1633[[recovering-lost-changes]]
11e016a3
BF
1634Recovering lost changes
1635~~~~~~~~~~~~~~~~~~~~~~~
1636
e34caace 1637[[reflogs]]
559e4d7a
BF
1638Reflogs
1639^^^^^^^
1640
1249d8ad
TK
1641Say you modify a branch with <<fixing-mistakes,`git reset --hard`>>,
1642and then realize that the branch was the only reference you had to
1643that point in history.
559e4d7a 1644
2de9b711 1645Fortunately, Git also keeps a log, called a "reflog", of all the
559e4d7a 1646previous values of each branch. So in this case you can still find the
a6080a0a 1647old history using, for example,
559e4d7a
BF
1648
1649-------------------------------------------------
1650$ git log master@{1}
1651-------------------------------------------------
1652
e502c2c3 1653This lists the commits reachable from the previous version of the
1249d8ad
TK
1654`master` branch head. This syntax can be used with any Git command
1655that accepts a commit, not just with `git log`. Some other examples:
559e4d7a
BF
1656
1657-------------------------------------------------
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 week
953f3d6f
BF
1662$ git log --walk-reflogs master # show reflog entries for master
1663-------------------------------------------------
1664
1665A separate reflog is kept for the HEAD, so
1666
1667-------------------------------------------------
1668$ git show HEAD@{"1 week ago"}
559e4d7a
BF
1669-------------------------------------------------
1670
953f3d6f
BF
1671will show what HEAD pointed to one week ago, not what the current branch
1672pointed to one week ago. This allows you to see the history of what
1673you've checked out.
1674
559e4d7a 1675The reflogs are kept by default for 30 days, after which they may be
5162e697 1676pruned. See linkgit:git-reflog[1] and linkgit:git-gc[1] to learn
559e4d7a 1677how to control this pruning, and see the "SPECIFYING REVISIONS"
9d83e382 1678section of linkgit:gitrevisions[7] for details.
559e4d7a 1679
2de9b711 1680Note that the reflog history is very different from normal Git history.
559e4d7a
BF
1681While normal history is shared by every repository that works on the
1682same project, the reflog history is not shared: it tells you only about
1683how the branches in your local repository have changed over time.
1684
59723040 1685[[dangling-object-recovery]]
559e4d7a
BF
1686Examining dangling objects
1687^^^^^^^^^^^^^^^^^^^^^^^^^^
1688
59723040
BF
1689In some situations the reflog may not be able to save you. For example,
1690suppose you delete a branch, then realize you need the history it
1691contained. The reflog is also deleted; however, if you have not yet
1692pruned the repository, then you may still be able to find the lost
6127c086 1693commits in the dangling objects that `git fsck` reports. See
59723040 1694<<dangling-objects>> for the details.
559e4d7a
BF
1695
1696-------------------------------------------------
1697$ git fsck
1698dangling commit 7281251ddd2a61e38657c827739c57015671a6b3
1699dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a63
1700dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b5
1701...
1702-------------------------------------------------
1703
aacd404e 1704You can examine
559e4d7a
BF
1705one of those dangling commits with, for example,
1706
1707------------------------------------------------
1708$ gitk 7281251ddd --not --all
1709------------------------------------------------
1710
1711which does what it sounds like: it says that you want to see the commit
1712history that is described by the dangling commit(s), but not the
1713history that is described by all your existing branches and tags. Thus
1714you 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 the
1716"tip of the line" as being dangling, but there might be a whole deep
79c96c57 1717and complex commit history that was dropped.)
559e4d7a
BF
1718
1719If you decide you want the history back, you can always create a new
1720reference pointing to it, for example, a new branch:
1721
1722------------------------------------------------
a6080a0a 1723$ git branch recovered-branch 7281251ddd
559e4d7a
BF
1724------------------------------------------------
1725
59723040
BF
1726Other types of dangling objects (blobs and trees) are also possible, and
1727dangling objects can arise in other situations.
1728
11e016a3 1729
e34caace 1730[[sharing-development]]
d19fbc3c 1731Sharing development with others
b684f830 1732===============================
d19fbc3c 1733
aa971cb9 1734[[getting-updates-With-git-pull]]
6127c086 1735Getting updates with git pull
b684f830 1736-----------------------------
d19fbc3c 1737
e63ec003 1738After you clone a repository and commit a few changes of your own, you
d19fbc3c
BF
1739may wish to check the original repository for updates and merge them
1740into your own work.
1741
aa971cb9 1742We have already seen <<Updating-a-repository-With-git-fetch,how to
0e615b25 1743keep remote-tracking branches up to date>> with linkgit:git-fetch[1],
d19fbc3c
BF
1744and how to merge two branches. So you can merge in changes from the
1745original repository's master branch with:
1746
1747-------------------------------------------------
1748$ git fetch
1749$ git merge origin/master
1750-------------------------------------------------
1751
5162e697 1752However, the linkgit:git-pull[1] command provides a way to do this in
d19fbc3c
BF
1753one step:
1754
1755-------------------------------------------------
1756$ git pull origin master
1757-------------------------------------------------
1758
1249d8ad
TK
1759In fact, if you have `master` checked out, then this branch has been
1760configured by `git clone` to get changes from the HEAD branch of the
66a062a1 1761origin repository. So often you can
0eb4f7cd 1762accomplish the above with just a simple
d19fbc3c
BF
1763
1764-------------------------------------------------
1765$ git pull
1766-------------------------------------------------
1767
66a062a1
MM
1768This command will fetch changes from the remote branches to your
1769remote-tracking branches `origin/*`, and merge the default branch into
1770the current branch.
1771
29b9a66f
MM
1772More generally, a branch that is created from a remote-tracking branch
1773will pull
0eb4f7cd 1774by default from that branch. See the descriptions of the
1249d8ad 1775`branch.<name>.remote` and `branch.<name>.merge` options in
5162e697
DM
1776linkgit:git-config[1], and the discussion of the `--track` option in
1777linkgit:git-checkout[1], to learn how to control these defaults.
d19fbc3c 1778
1249d8ad 1779In addition to saving you keystrokes, `git pull` also helps you by
d19fbc3c
BF
1780producing a default commit message documenting the branch and
1781repository that you pulled from.
1782
1783(But note that no such commit will be created in the case of a
a75d7b54 1784<<fast-forwards,fast-forward>>; instead, your branch will just be
79c96c57 1785updated to point to the latest commit from the upstream branch.)
d19fbc3c 1786
1249d8ad 1787The `git pull` command can also be given `.` as the "remote" repository,
1191ee18 1788in which case it just merges in a branch from the current repository; so
4c63ff45
BF
1789the commands
1790
1791-------------------------------------------------
1792$ git pull . branch
1793$ git merge branch
1794-------------------------------------------------
1795
a7bdee11 1796are roughly equivalent.
4c63ff45 1797
e34caace 1798[[submitting-patches]]
d19fbc3c 1799Submitting patches to a project
b684f830 1800-------------------------------
d19fbc3c
BF
1801
1802If you just have a few changes, the simplest way to submit them may
1803just be to send them as patches in email:
1804
5162e697 1805First, use linkgit:git-format-patch[1]; for example:
d19fbc3c
BF
1806
1807-------------------------------------------------
eb6ae7f4 1808$ git format-patch origin
d19fbc3c
BF
1809-------------------------------------------------
1810
1811will produce a numbered series of files in the current directory, one
1249d8ad 1812for each patch in the current branch but not in `origin/HEAD`.
d19fbc3c 1813
d84cef18
PO
1814`git format-patch` can include an initial "cover letter". You can insert
1815commentary on individual patches after the three dash line which
1816`format-patch` places after the commit message but before the patch
1817itself. If you use `git notes` to track your cover letter material,
1818`git format-patch --notes` will include the commit's notes in a similar
1819manner.
1820
d19fbc3c
BF
1821You can then import these into your mail client and send them by
1822hand. However, if you have a lot to send at once, you may prefer to
5162e697 1823use the linkgit:git-send-email[1] script to automate the process.
d19fbc3c
BF
1824Consult the mailing list for your project first to determine how they
1825prefer such patches be handled.
1826
e34caace 1827[[importing-patches]]
d19fbc3c 1828Importing patches to a project
b684f830 1829------------------------------
d19fbc3c 1830
5162e697 1831Git also provides a tool called linkgit:git-am[1] (am stands for
d19fbc3c
BF
1832"apply mailbox"), for importing such an emailed series of patches.
1833Just save all of the patch-containing messages, in order, into a
1249d8ad 1834single mailbox file, say `patches.mbox`, then run
d19fbc3c
BF
1835
1836-------------------------------------------------
eb6ae7f4 1837$ git am -3 patches.mbox
d19fbc3c
BF
1838-------------------------------------------------
1839
1840Git will apply each patch in order; if any conflicts are found, it
1841will stop, and you can fix the conflicts as described in
1249d8ad 1842"<<resolving-a-merge,Resolving a merge>>". (The `-3` option tells
2de9b711 1843Git to perform a merge; if you would prefer it just to abort and
01997b4a
BF
1844leave your tree and index untouched, you may omit that option.)
1845
1846Once the index is updated with the results of the conflict
1847resolution, instead of creating a new commit, just run
d19fbc3c
BF
1848
1849-------------------------------------------------
8ceb6fbd 1850$ git am --continue
d19fbc3c
BF
1851-------------------------------------------------
1852
2de9b711 1853and Git will create the commit for you and continue applying the
d19fbc3c
BF
1854remaining patches from the mailbox.
1855
1856The final result will be a series of commits, one for each patch in
1857the original mailbox, with authorship and commit log message each
1858taken from the message containing each patch.
1859
eda69449 1860[[public-repositories]]
2de9b711 1861Public Git repositories
eda69449 1862-----------------------
d19fbc3c 1863
6e30fb0c
DK
1864Another way to submit changes to a project is to tell the maintainer
1865of that project to pull the changes from your repository using
aa971cb9 1866linkgit:git-pull[1]. In the section "<<getting-updates-With-git-pull,
6127c086 1867Getting updates with `git pull`>>" we described this as a way to get
6e30fb0c
DK
1868updates from the "main" repository, but it works just as well in the
1869other direction.
d19fbc3c 1870
eda69449
BF
1871If you and the maintainer both have accounts on the same machine, then
1872you can just pull changes from each other's repositories directly;
11d51533 1873commands that accept repository URLs as arguments will also accept a
eda69449 1874local directory name:
d19fbc3c
BF
1875
1876-------------------------------------------------
1877$ git clone /path/to/repository
1878$ git pull /path/to/other/repository
1879-------------------------------------------------
1880
c9016158 1881or an ssh URL:
11d51533
BF
1882
1883-------------------------------------------------
1884$ git clone ssh://yourhost/~you/repository
1885-------------------------------------------------
1886
1887For projects with few developers, or for synchronizing a few private
1888repositories, this may be all you need.
1889
eda69449
BF
1890However, the more common way to do this is to maintain a separate public
1891repository (usually on a different host) for others to pull changes
1892from. This is usually more convenient, and allows you to cleanly
1893separate private work in progress from publicly visible work.
d19fbc3c
BF
1894
1895You will continue to do your day-to-day work in your personal
1896repository, but periodically "push" changes from your personal
1897repository into your public repository, allowing other developers to
1898pull from that repository. So the flow of changes, in a situation
1899where there is one other developer with a public repository, looks
1900like this:
1901
1902 you push
1903 your personal repo ------------------> your public repo
a6080a0a 1904 ^ |
d19fbc3c
BF
1905 | |
1906 | you pull | they pull
1907 | |
1908 | |
1909 | they push V
1910 their public repo <------------------- their repo
1911
11d51533
BF
1912We explain how to do this in the following sections.
1913
eda69449
BF
1914[[setting-up-a-public-repository]]
1915Setting up a public repository
1916~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1917
1249d8ad 1918Assume your personal repository is in the directory `~/proj`. We
6127c086 1919first create a new clone of the repository and tell `git daemon` that it
eda69449 1920is meant to be public:
d19fbc3c
BF
1921
1922-------------------------------------------------
52c80037 1923$ git clone --bare ~/proj proj.git
eda69449 1924$ touch proj.git/git-daemon-export-ok
d19fbc3c
BF
1925-------------------------------------------------
1926
52c80037 1927The resulting directory proj.git contains a "bare" git repository--it is
1249d8ad 1928just the contents of the `.git` directory, without any files checked out
eda69449 1929around it.
d19fbc3c 1930
1249d8ad 1931Next, copy `proj.git` to the server where you plan to host the
d19fbc3c
BF
1932public repository. You can use scp, rsync, or whatever is most
1933convenient.
1934
eda69449 1935[[exporting-via-git]]
2de9b711 1936Exporting a Git repository via the Git protocol
eda69449
BF
1937~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1938
1939This is the preferred method.
1940
1941If someone else administers the server, they should tell you what
1249d8ad
TK
1942directory to put the repository in, and what `git://` URL it will
1943appear at. You can then skip to the section
d19fbc3c
BF
1944"<<pushing-changes-to-a-public-repository,Pushing changes to a public
1945repository>>", below.
1946
5162e697 1947Otherwise, all you need to do is start linkgit:git-daemon[1]; it will
eda69449 1948listen on port 9418. By default, it will allow access to any directory
2de9b711 1949that looks like a Git directory and contains the magic file
6127c086 1950git-daemon-export-ok. Passing some directory paths as `git daemon`
eda69449
BF
1951arguments will further restrict the exports to those paths.
1952
6127c086 1953You can also run `git daemon` as an inetd service; see the
5162e697 1954linkgit:git-daemon[1] man page for details. (See especially the
eda69449 1955examples section.)
d19fbc3c
BF
1956
1957[[exporting-via-http]]
de3f2c7b 1958Exporting a git repository via HTTP
eda69449 1959~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
d19fbc3c 1960
2de9b711 1961The Git protocol gives better performance and reliability, but on a
de3f2c7b 1962host with a web server set up, HTTP exports may be simpler to set up.
d19fbc3c 1963
2de9b711 1964All you need to do is place the newly created bare Git repository in
d19fbc3c
BF
1965a directory that is exported by the web server, and make some
1966adjustments to give web clients some extra information they need:
1967
1968-------------------------------------------------
1969$ mv proj.git /home/you/public_html/proj.git
1970$ cd proj.git
c64415e2 1971$ git --bare update-server-info
7dce9918 1972$ mv hooks/post-update.sample hooks/post-update
d19fbc3c
BF
1973-------------------------------------------------
1974
1975(For an explanation of the last two lines, see
6998e4db 1976linkgit:git-update-server-info[1] and linkgit:githooks[5].)
d19fbc3c 1977
1249d8ad 1978Advertise the URL of `proj.git`. Anybody else should then be able to
c9016158 1979clone or pull from that URL, for example with a command line like:
d19fbc3c
BF
1980
1981-------------------------------------------------
1982$ git clone http://yourserver.com/~you/proj.git
1983-------------------------------------------------
1984
1985(See also
1986link:howto/setup-git-server-over-http.txt[setup-git-server-over-http]
1987for a slightly more sophisticated setup using WebDAV which also
de3f2c7b 1988allows pushing over HTTP.)
d19fbc3c 1989
d19fbc3c
BF
1990[[pushing-changes-to-a-public-repository]]
1991Pushing changes to a public repository
eda69449 1992~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
d19fbc3c 1993
eda69449 1994Note that the two techniques outlined above (exporting via
d19fbc3c
BF
1995<<exporting-via-http,http>> or <<exporting-via-git,git>>) allow other
1996maintainers to fetch your latest changes, but they do not allow write
1997access, which you will need to update the public repository with the
1998latest changes created in your private repository.
1999
5162e697 2000The simplest way to do this is using linkgit:git-push[1] and ssh; to
1249d8ad
TK
2001update the remote branch named `master` with the latest state of your
2002branch named `master`, run
d19fbc3c
BF
2003
2004-------------------------------------------------
2005$ git push ssh://yourserver.com/~you/proj.git master:master
2006-------------------------------------------------
2007
2008or just
2009
2010-------------------------------------------------
2011$ git push ssh://yourserver.com/~you/proj.git master
2012-------------------------------------------------
2013
6127c086 2014As with `git fetch`, `git push` will complain if this does not result in a
a75d7b54 2015<<fast-forwards,fast-forward>>; see the following section for details on
81eb417a 2016handling this case.
d19fbc3c 2017
1249d8ad 2018Note that the target of a `push` is normally a
11d51533 2019<<def_bare_repository,bare>> repository. You can also push to a
d9be2485
TK
2020repository that has a checked-out working tree, but a push to update the
2021currently checked-out branch is denied by default to prevent confusion.
50995edd 2022See the description of the receive.denyCurrentBranch option
d9be2485 2023in linkgit:git-config[1] for details.
11d51533 2024
6127c086 2025As with `git fetch`, you may also set up configuration options to
e9b49083
TK
2026save typing; so, for example:
2027
2028-------------------------------------------------
2029$ git remote add public-repo ssh://yourserver.com/~you/proj.git
2030-------------------------------------------------
2031
2032adds the following to `.git/config`:
d19fbc3c
BF
2033
2034-------------------------------------------------
d19fbc3c 2035[remote "public-repo"]
e9b49083
TK
2036 url = yourserver.com:proj.git
2037 fetch = +refs/heads/*:refs/remotes/example/*
d19fbc3c
BF
2038-------------------------------------------------
2039
e9b49083 2040which lets you do the same push with just
d19fbc3c
BF
2041
2042-------------------------------------------------
2043$ git push public-repo master
2044-------------------------------------------------
2045
1249d8ad
TK
2046See the explanations of the `remote.<name>.url`,
2047`branch.<name>.remote`, and `remote.<name>.push` options in
2048linkgit:git-config[1] for details.
d19fbc3c 2049
81eb417a
BF
2050[[forcing-push]]
2051What to do when a push fails
2052~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2053
a75d7b54 2054If a push would not result in a <<fast-forwards,fast-forward>> of the
81eb417a
BF
2055remote branch, then it will fail with an error like:
2056
2057-------------------------------------------------
2058error: remote 'refs/heads/master' is not an ancestor of
2059 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-------------------------------------------------
2063
2064This can happen, for example, if you:
2065
6127c086
FC
2066 - use `git reset --hard` to remove already-published commits, or
2067 - use `git commit --amend` to replace already-published commits
7cb192ea 2068 (as in <<fixing-a-mistake-by-rewriting-history>>), or
6127c086 2069 - use `git rebase` to rebase any already-published commits (as
81eb417a
BF
2070 in <<using-git-rebase>>).
2071
6127c086 2072You may force `git push` to perform the update anyway by preceding the
81eb417a
BF
2073branch name with a plus sign:
2074
2075-------------------------------------------------
2076$ git push ssh://yourserver.com/~you/proj.git +master
2077-------------------------------------------------
2078
d1471e06
TK
2079Note the addition of the `+` sign. Alternatively, you can use the
2080`-f` flag to force the remote update, as in:
2081
2082-------------------------------------------------
2083$ git push -f ssh://yourserver.com/~you/proj.git master
2084-------------------------------------------------
2085
81eb417a 2086Normally whenever a branch head in a public repository is modified, it
9e5d87d4 2087is modified to point to a descendant of the commit that it pointed to
81eb417a 2088before. By forcing a push in this situation, you break that convention.
aa971cb9 2089(See <<problems-With-rewriting-history>>.)
81eb417a
BF
2090
2091Nevertheless, this is a common practice for people that need a simple
2092way to publish a work-in-progress patch series, and it is an acceptable
2093compromise as long as you warn other developers that this is how you
2094intend to manage the branch.
2095
2096It's also possible for a push to fail in this way when other people have
2097the right to push to the same repository. In that case, the correct
843c81dc
EH
2098solution is to retry the push after first updating your work: either by a
2099pull, or by a fetch followed by a rebase; see the
81eb417a 2100<<setting-up-a-shared-repository,next section>> and
6998e4db 2101linkgit:gitcvs-migration[7] for more.
81eb417a 2102
e34caace 2103[[setting-up-a-shared-repository]]
d19fbc3c 2104Setting up a shared repository
eda69449 2105~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
d19fbc3c
BF
2106
2107Another way to collaborate is by using a model similar to that
2108commonly used in CVS, where several developers with special rights
2109all push to and pull from a single shared repository. See
6998e4db 2110linkgit:gitcvs-migration[7] for instructions on how to
d19fbc3c
BF
2111set this up.
2112
2de9b711 2113However, while there is nothing wrong with Git's support for shared
8fae2225 2114repositories, this mode of operation is not generally recommended,
2de9b711 2115simply because the mode of collaboration that Git supports--by
8fae2225
BF
2116exchanging patches and pulling from public repositories--has so many
2117advantages over the central shared repository:
2118
2119 - Git's ability to quickly import and merge patches allows a
2120 single maintainer to process incoming changes even at very
6127c086 2121 high rates. And when that becomes too much, `git pull` provides
8fae2225
BF
2122 an easy way for that maintainer to delegate this job to other
2123 maintainers while still allowing optional review of incoming
2124 changes.
2125 - Since every developer's repository has the same complete copy
2126 of the project history, no repository is special, and it is
2127 trivial for another developer to take over maintenance of a
2128 project, either by mutual agreement, or because a maintainer
2129 becomes unresponsive or difficult to work with.
2130 - The lack of a central group of "committers" means there is
2131 less need for formal decisions about who is "in" and who is
2132 "out".
2133
e34caace 2134[[setting-up-gitweb]]
eda69449
BF
2135Allowing web browsing of a repository
2136~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
d19fbc3c 2137
a8cd1402 2138The gitweb cgi script provides users an easy way to browse your
2de9b711
TA
2139project's files and history without having to install Git; see the file
2140gitweb/INSTALL in the Git source tree for instructions on setting it up.
d19fbc3c 2141
e34caace 2142[[sharing-development-examples]]
b684f830
BF
2143Examples
2144--------
d19fbc3c 2145
9e2163ea
BF
2146[[maintaining-topic-branches]]
2147Maintaining topic branches for a Linux subsystem maintainer
2148~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2149
2de9b711 2150This describes how Tony Luck uses Git in his role as maintainer of the
9e2163ea
BF
2151IA64 architecture for the Linux kernel.
2152
2153He uses two public branches:
2154
2155 - A "test" tree into which patches are initially placed so that they
2156 can get some exposure when integrated with other ongoing development.
2157 This tree is available to Andrew for pulling into -mm whenever he
2158 wants.
2159
2160 - A "release" tree into which tested patches are moved for final sanity
2161 checking, and as a vehicle to send them upstream to Linus (by sending
2162 him a "please pull" request.)
2163
2164He also uses a set of temporary branches ("topic branches"), each
2165containing a logical grouping of patches.
2166
2167To set this up, first create your work tree by cloning Linus's public
2168tree:
2169
2170-------------------------------------------------
283efb01 2171$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git work
9e2163ea
BF
2172$ cd work
2173-------------------------------------------------
2174
29b9a66f 2175Linus's tree will be stored in the remote-tracking branch named origin/master,
5162e697
DM
2176and can be updated using linkgit:git-fetch[1]; you can track other
2177public trees using linkgit:git-remote[1] to set up a "remote" and
2178linkgit:git-fetch[1] to keep them up-to-date; see
6e30fb0c 2179<<repositories-and-branches>>.
9e2163ea
BF
2180
2181Now create the branches in which you are going to work; these start out
2182at the current tip of origin/master branch, and should be set up (using
1249d8ad 2183the `--track` option to linkgit:git-branch[1]) to merge changes in from
9e2163ea
BF
2184Linus by default.
2185
2186-------------------------------------------------
2187$ git branch --track test origin/master
2188$ git branch --track release origin/master
2189-------------------------------------------------
2190
5162e697 2191These can be easily kept up to date using linkgit:git-pull[1].
9e2163ea
BF
2192
2193-------------------------------------------------
2194$ git checkout test && git pull
2195$ git checkout release && git pull
2196-------------------------------------------------
2197
2198Important note! If you have any local changes in these branches, then
2199this merge will create a commit object in the history (with no local
2de9b711 2200changes Git will simply do a "fast-forward" merge). Many people dislike
9e2163ea 2201the "noise" that this creates in the Linux history, so you should avoid
1249d8ad 2202doing this capriciously in the `release` branch, as these noisy commits
9e2163ea
BF
2203will become part of the permanent history when you ask Linus to pull
2204from the release branch.
2205
5162e697 2206A few configuration variables (see linkgit:git-config[1]) can
9e2163ea
BF
2207make it easy to push both branches to your public tree. (See
2208<<setting-up-a-public-repository>>.)
2209
2210-------------------------------------------------
2211$ cat >> .git/config <<EOF
2212[remote "mytree"]
283efb01 2213 url = master.kernel.org:/pub/scm/linux/kernel/git/aegl/linux.git
9e2163ea
BF
2214 push = release
2215 push = test
2216EOF
2217-------------------------------------------------
2218
2219Then you can push both the test and release trees using
5162e697 2220linkgit:git-push[1]:
9e2163ea
BF
2221
2222-------------------------------------------------
2223$ git push mytree
2224-------------------------------------------------
2225
2226or push just one of the test and release branches using:
2227
2228-------------------------------------------------
2229$ git push mytree test
2230-------------------------------------------------
2231
2232or
2233
2234-------------------------------------------------
2235$ git push mytree release
2236-------------------------------------------------
2237
2238Now to apply some patches from the community. Think of a short
2239snappy name for a branch to hold this patch (or related group of
352953a5
TL
2240patches), and create a new branch from a recent stable tag of
2241Linus's branch. Picking a stable base for your branch will:
22421) help you: by avoiding inclusion of unrelated and perhaps lightly
2243tested changes
1249d8ad 22442) help future bug hunters that use `git bisect` to find problems
9e2163ea
BF
2245
2246-------------------------------------------------
352953a5 2247$ git checkout -b speed-up-spinlocks v2.6.35
9e2163ea
BF
2248-------------------------------------------------
2249
2250Now you apply the patch(es), run some tests, and commit the change(s). If
2251the patch is a multi-part series, then you should apply each as a separate
2252commit to this branch.
2253
2254-------------------------------------------------
2255$ ... patch ... test ... commit [ ... patch ... test ... commit ]*
2256-------------------------------------------------
2257
a7bdee11 2258When you are happy with the state of this change, you can merge it into the
9e2163ea
BF
2259"test" branch in preparation to make it public:
2260
2261-------------------------------------------------
a7bdee11 2262$ git checkout test && git merge speed-up-spinlocks
9e2163ea
BF
2263-------------------------------------------------
2264
2265It is unlikely that you would have any conflicts here ... but you might if you
2266spent a while on this step and had also pulled new versions from upstream.
2267
2268Some time later when enough time has passed and testing done, you can pull the
1249d8ad 2269same branch into the `release` tree ready to go upstream. This is where you
9e2163ea 2270see the value of keeping each patch (or patch series) in its own branch. It
1249d8ad 2271means that the patches can be moved into the `release` tree in any order.
9e2163ea
BF
2272
2273-------------------------------------------------
a7bdee11 2274$ git checkout release && git merge speed-up-spinlocks
9e2163ea
BF
2275-------------------------------------------------
2276
2277After a while, you will have a number of branches, and despite the
2278well chosen names you picked for each of them, you may forget what
2279they are for, or what status they are in. To get a reminder of what
2280changes are in a specific branch, use:
2281
2282-------------------------------------------------
467c0197 2283$ git log linux..branchname | git shortlog
9e2163ea
BF
2284-------------------------------------------------
2285
06ada152 2286To see whether it has already been merged into the test or release branches,
9e2163ea
BF
2287use:
2288
2289-------------------------------------------------
2290$ git log test..branchname
2291-------------------------------------------------
2292
2293or
2294
2295-------------------------------------------------
2296$ git log release..branchname
2297-------------------------------------------------
2298
06ada152 2299(If this branch has not yet been merged, you will see some log entries.
9e2163ea
BF
2300If it has been merged, then there will be no output.)
2301
2302Once a patch completes the great cycle (moving from test to release,
2303then pulled by Linus, and finally coming back into your local
1249d8ad 2304`origin/master` branch), the branch for this change is no longer needed.
9e2163ea
BF
2305You detect this when the output from:
2306
2307-------------------------------------------------
2308$ git log origin..branchname
2309-------------------------------------------------
2310
2311is empty. At this point the branch can be deleted:
2312
2313-------------------------------------------------
2314$ git branch -d branchname
2315-------------------------------------------------
2316
2317Some changes are so trivial that it is not necessary to create a separate
2318branch and then merge into each of the test and release branches. For
1249d8ad
TK
2319these changes, just apply directly to the `release` branch, and then
2320merge that into the `test` branch.
9e2163ea 2321
ae6ef554
TK
2322After pushing your work to `mytree`, you can use
2323linkgit:git-request-pull[1] to prepare a "please pull" request message
2324to send to Linus:
9e2163ea
BF
2325
2326-------------------------------------------------
ae6ef554
TK
2327$ git push mytree
2328$ git request-pull origin mytree release
9e2163ea
BF
2329-------------------------------------------------
2330
2331Here are some of the scripts that simplify all this even further.
2332
2333-------------------------------------------------
2334==== update script ====
48a8c26c 2335# Update a branch in my Git tree. If the branch to be updated
9e2163ea
BF
2336# is origin, then pull from kernel.org. Otherwise merge
2337# origin/master branch into test|release branch
2338
2339case "$1" in
2340test|release)
2341 git checkout $1 && git pull . origin
2342 ;;
2343origin)
fc74ecc1 2344 before=$(git rev-parse refs/remotes/origin/master)
9e2163ea 2345 git fetch origin
fc74ecc1 2346 after=$(git rev-parse refs/remotes/origin/master)
9e2163ea
BF
2347 if [ $before != $after ]
2348 then
2349 git log $before..$after | git shortlog
2350 fi
2351 ;;
2352*)
1a2ba8b9 2353 echo "usage: $0 origin|test|release" 1>&2
9e2163ea
BF
2354 exit 1
2355 ;;
2356esac
2357-------------------------------------------------
2358
2359-------------------------------------------------
2360==== merge script ====
2361# Merge a branch into either the test or release branch
2362
2363pname=$0
2364
2365usage()
2366{
1a2ba8b9 2367 echo "usage: $pname branch test|release" 1>&2
9e2163ea
BF
2368 exit 1
2369}
2370
fc74ecc1 2371git show-ref -q --verify -- refs/heads/"$1" || {
9e2163ea
BF
2372 echo "Can't see branch <$1>" 1>&2
2373 usage
fc74ecc1 2374}
9e2163ea
BF
2375
2376case "$2" in
2377test|release)
2378 if [ $(git log $2..$1 | wc -c) -eq 0 ]
2379 then
2380 echo $1 already merged into $2 1>&2
2381 exit 1
2382 fi
2383 git checkout $2 && git pull . $1
2384 ;;
2385*)
2386 usage
2387 ;;
2388esac
2389-------------------------------------------------
2390
2391-------------------------------------------------
2392==== status script ====
48a8c26c 2393# report on status of my ia64 Git tree
9e2163ea
BF
2394
2395gb=$(tput setab 2)
2396rb=$(tput setab 1)
2397restore=$(tput setab 9)
2398
2399if [ `git rev-list test..release | wc -c` -gt 0 ]
2400then
2401 echo $rb Warning: commits in release that are not in test $restore
2402 git log test..release
2403fi
2404
fc74ecc1 2405for branch in `git show-ref --heads | sed 's|^.*/||'`
9e2163ea
BF
2406do
2407 if [ $branch = test -o $branch = release ]
2408 then
2409 continue
2410 fi
2411
2412 echo -n $gb ======= $branch ====== $restore " "
2413 status=
2414 for ref in test release origin/master
2415 do
2416 if [ `git rev-list $ref..$branch | wc -c` -gt 0 ]
2417 then
2418 status=$status${ref:0:1}
2419 fi
2420 done
2421 case $status in
2422 trl)
2423 echo $rb Need to pull into test $restore
2424 ;;
2425 rl)
2426 echo "In test"
2427 ;;
2428 l)
2429 echo "Waiting for linus"
2430 ;;
2431 "")
2432 echo $rb All done $restore
2433 ;;
2434 *)
2435 echo $rb "<$status>" $restore
2436 ;;
2437 esac
2438 git log origin/master..$branch | git shortlog
2439done
2440-------------------------------------------------
d19fbc3c 2441
d19fbc3c 2442
d19fbc3c 2443[[cleaning-up-history]]
4c63ff45
BF
2444Rewriting history and maintaining patch series
2445==============================================
2446
2447Normally commits are only added to a project, never taken away or
2448replaced. Git is designed with this assumption, and violating it will
2de9b711 2449cause Git's merge machinery (for example) to do the wrong thing.
4c63ff45
BF
2450
2451However, there is a situation in which it can be useful to violate this
2452assumption.
2453
e34caace 2454[[patch-series]]
4c63ff45
BF
2455Creating the perfect patch series
2456---------------------------------
2457
2458Suppose you are a contributor to a large project, and you want to add a
2459complicated feature, and to present it to the other developers in a way
2460that makes it easy for them to read your changes, verify that they are
2461correct, and understand why you made each change.
2462
b181d57f 2463If you present all of your changes as a single patch (or commit), they
79c96c57 2464may find that it is too much to digest all at once.
4c63ff45
BF
2465
2466If you present them with the entire history of your work, complete with
2467mistakes, corrections, and dead ends, they may be overwhelmed.
2468
2469So the ideal is usually to produce a series of patches such that:
2470
2471 1. Each patch can be applied in order.
2472
2473 2. Each patch includes a single logical change, together with a
2474 message explaining the change.
2475
2476 3. No patch introduces a regression: after applying any initial
2477 part of the series, the resulting project still compiles and
2478 works, and has no bugs that it didn't have before.
2479
2480 4. The complete series produces the same end result as your own
2481 (probably much messier!) development process did.
2482
b181d57f
BF
2483We will introduce some tools that can help you do this, explain how to
2484use them, and then explain some of the problems that can arise because
2485you are rewriting history.
4c63ff45 2486
e34caace 2487[[using-git-rebase]]
6127c086 2488Keeping a patch series up to date using git rebase
4c63ff45
BF
2489--------------------------------------------------
2490
1249d8ad
TK
2491Suppose that you create a branch `mywork` on a remote-tracking branch
2492`origin`, and create some commits on top of it:
4c63ff45
BF
2493
2494-------------------------------------------------
2495$ git checkout -b mywork origin
2496$ vi file.txt
2497$ git commit
2498$ vi otherfile.txt
2499$ git commit
2500...
2501-------------------------------------------------
2502
2503You have performed no merges into mywork, so it is just a simple linear
1249d8ad 2504sequence of patches on top of `origin`:
4c63ff45 2505
1dc71a91 2506................................................
fa8347b8 2507 o--o--O <-- origin
4c63ff45 2508 \
fa8347b8 2509 a--b--c <-- mywork
1dc71a91 2510................................................
4c63ff45
BF
2511
2512Some more interesting work has been done in the upstream project, and
1249d8ad 2513`origin` has advanced:
4c63ff45 2514
1dc71a91 2515................................................
4c63ff45
BF
2516 o--o--O--o--o--o <-- origin
2517 \
2518 a--b--c <-- mywork
1dc71a91 2519................................................
4c63ff45 2520
1249d8ad 2521At this point, you could use `pull` to merge your changes back in;
4c63ff45
BF
2522the result would create a new merge commit, like this:
2523
1dc71a91 2524................................................
4c63ff45
BF
2525 o--o--O--o--o--o <-- origin
2526 \ \
2527 a--b--c--m <-- mywork
1dc71a91 2528................................................
a6080a0a 2529
4c63ff45
BF
2530However, if you prefer to keep the history in mywork a simple series of
2531commits without any merges, you may instead choose to use
5162e697 2532linkgit:git-rebase[1]:
4c63ff45
BF
2533
2534-------------------------------------------------
2535$ git checkout mywork
2536$ git rebase origin
2537-------------------------------------------------
2538
b181d57f 2539This will remove each of your commits from mywork, temporarily saving
1249d8ad 2540them as patches (in a directory named `.git/rebase-apply`), update mywork to
b181d57f
BF
2541point at the latest version of origin, then apply each of the saved
2542patches to the new mywork. The result will look like:
4c63ff45
BF
2543
2544
1dc71a91 2545................................................
4c63ff45
BF
2546 o--o--O--o--o--o <-- origin
2547 \
2548 a'--b'--c' <-- mywork
1dc71a91 2549................................................
4c63ff45 2550
b181d57f 2551In the process, it may discover conflicts. In that case it will stop
6127c086 2552and allow you to fix the conflicts; after fixing conflicts, use `git add`
7a7d4ef6 2553to update the index with those contents, and then, instead of
6127c086 2554running `git commit`, just run
4c63ff45
BF
2555
2556-------------------------------------------------
2557$ git rebase --continue
2558-------------------------------------------------
2559
2de9b711 2560and Git will continue applying the rest of the patches.
4c63ff45 2561
b6cbca38 2562At any point you may use the `--abort` option to abort this process and
4c63ff45
BF
2563return mywork to the state it had before you started the rebase:
2564
2565-------------------------------------------------
2566$ git rebase --abort
2567-------------------------------------------------
2568
6c26bf4d
TK
2569If you need to reorder or edit a number of commits in a branch, it may
2570be easier to use `git rebase -i`, which allows you to reorder and
2571squash commits, as well as marking them for individual editing during
2572the rebase. See <<interactive-rebase>> for details, and
2573<<reordering-patch-series>> for alternatives.
2574
7cb192ea
BF
2575[[rewriting-one-commit]]
2576Rewriting a single commit
365aa199
BF
2577-------------------------
2578
7cb192ea 2579We saw in <<fixing-a-mistake-by-rewriting-history>> that you can replace the
365aa199
BF
2580most recent commit using
2581
2582-------------------------------------------------
2583$ git commit --amend
2584-------------------------------------------------
2585
2586which will replace the old commit by a new commit incorporating your
2587changes, giving you a chance to edit the old commit message first.
6c26bf4d
TK
2588This is useful for fixing typos in your last commit, or for adjusting
2589the patch contents of a poorly staged commit.
365aa199 2590
6c26bf4d
TK
2591If you need to amend commits from deeper in your history, you can
2592use <<interactive-rebase,interactive rebase's `edit` instruction>>.
365aa199 2593
6c26bf4d
TK
2594[[reordering-patch-series]]
2595Reordering or selecting from a patch series
2596-------------------------------------------
365aa199 2597
6c26bf4d
TK
2598Sometimes you want to edit a commit deeper in your history. One
2599approach is to use `git format-patch` to create a series of patches
2600and then reset the state to before the patches:
365aa199
BF
2601
2602-------------------------------------------------
6c26bf4d
TK
2603$ git format-patch origin
2604$ git reset --hard origin
365aa199
BF
2605-------------------------------------------------
2606
6c26bf4d
TK
2607Then modify, reorder, or eliminate patches as needed before applying
2608them again with linkgit:git-am[1]:
365aa199
BF
2609
2610-------------------------------------------------
6c26bf4d 2611$ git am *.patch
365aa199
BF
2612-------------------------------------------------
2613
6c26bf4d
TK
2614[[interactive-rebase]]
2615Using interactive rebases
2616-------------------------
365aa199 2617
6c26bf4d
TK
2618You can also edit a patch series with an interactive rebase. This is
2619the same as <<reordering-patch-series,reordering a patch series using
2620`format-patch`>>, so use whichever interface you like best.
4c63ff45 2621
6c26bf4d
TK
2622Rebase 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:
b181d57f
BF
2624
2625-------------------------------------------------
6c26bf4d 2626$ git rebase -i HEAD~5
b181d57f
BF
2627-------------------------------------------------
2628
6c26bf4d
TK
2629This will open your editor with a list of steps to be taken to perform
2630your rebase.
4c63ff45 2631
b181d57f 2632-------------------------------------------------
6c26bf4d
TK
2633pick deadbee The oneline of this commit
2634pick fa1afe1 The oneline of the next commit
2635...
4c63ff45 2636
6c26bf4d
TK
2637# Rebase c0ffeee..deadbee onto c0ffeee
2638#
2639# Commands:
2640# p, pick = use commit
2641# r, reword = use commit, but edit the commit message
2642# e, edit = use commit, but stop for amending
2643# s, squash = use commit, but meld into previous commit
2644# f, fixup = like "squash", but discard this commit's log message
2645# x, exec = run command (the rest of the line) using shell
2646#
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 out
2654-------------------------------------------------
2655
2656As explained in the comments, you can reorder commits, squash them
2657together, edit commit messages, etc. by editing the list. Once you
2658are satisfied, save the list and close your editor, and the rebase
2659will begin.
2660
2661The rebase will stop where `pick` has been replaced with `edit` or
2662when a step in the list fails to mechanically resolve conflicts and
2663needs your help. When you are done editing and/or resolving conflicts
2664you can continue with `git rebase --continue`. If you decide that
2665things are getting too hairy, you can always bail out with `git rebase
2666--abort`. Even after the rebase is complete, you can still recover
2667the original branch by using the <<reflogs,reflog>>.
2668
2669For a more detailed discussion of the procedure and additional tips,
2670see the "INTERACTIVE MODE" section of linkgit:git-rebase[1].
4c63ff45 2671
e34caace 2672[[patch-series-tools]]
4c63ff45
BF
2673Other tools
2674-----------
2675
73a1d050 2676There are numerous other tools, such as StGit, which exist for the
79c96c57 2677purpose of maintaining a patch series. These are outside of the scope of
b181d57f 2678this manual.
4c63ff45 2679
aa971cb9 2680[[problems-With-rewriting-history]]
4c63ff45
BF
2681Problems with rewriting history
2682-------------------------------
2683
b181d57f
BF
2684The primary problem with rewriting the history of a branch has to do
2685with merging. Suppose somebody fetches your branch and merges it into
2686their branch, with a result something like this:
2687
1dc71a91 2688................................................
b181d57f
BF
2689 o--o--O--o--o--o <-- origin
2690 \ \
2691 t--t--t--m <-- their branch:
1dc71a91 2692................................................
b181d57f
BF
2693
2694Then suppose you modify the last three commits:
2695
1dc71a91 2696................................................
b181d57f
BF
2697 o--o--o <-- new head of origin
2698 /
2699 o--o--O--o--o--o <-- old head of origin
1dc71a91 2700................................................
b181d57f
BF
2701
2702If we examined all this history together in one repository, it will
2703look like:
2704
1dc71a91 2705................................................
b181d57f
BF
2706 o--o--o <-- new head of origin
2707 /
2708 o--o--O--o--o--o <-- old head of origin
2709 \ \
2710 t--t--t--m <-- their branch:
1dc71a91 2711................................................
b181d57f
BF
2712
2713Git has no way of knowing that the new head is an updated version of
2714the old head; it treats this situation exactly the same as it would if
2715two developers had independently done the work on the old and new heads
2716in parallel. At this point, if someone attempts to merge the new head
2de9b711 2717in to their branch, Git will attempt to merge together the two (old and
b181d57f
BF
2718new) lines of development, instead of trying to replace the old by the
2719new. The results are likely to be unexpected.
2720
2721You may still choose to publish branches whose history is rewritten,
2722and it may be useful for others to be able to fetch those branches in
2723order to examine or test them, but they should not attempt to pull such
2724branches into their own work.
2725
2726For true distributed development that supports proper merging,
2727published branches should never be rewritten.
2728
3fb00282
SP
2729[[bisect-merges]]
2730Why bisecting merge commits can be harder than bisecting linear history
2731-----------------------------------------------------------------------
2732
5162e697 2733The linkgit:git-bisect[1] command correctly handles history that
3fb00282
SP
2734includes merge commits. However, when the commit that it finds is a
2735merge commit, the user may need to work harder than usual to figure out
2736why that commit introduced a problem.
2737
2738Imagine this history:
2739
2740................................................
2741 ---Z---o---X---...---o---A---C---D
2742 \ /
2743 o---o---Y---...---o---B
2744................................................
2745
2746Suppose that on the upper line of development, the meaning of one
2747of the functions that exists at Z is changed at commit X. The
2748commits from Z leading to A change both the function's
2749implementation and all calling sites that exist at Z, as well
2750as new calling sites they add, to be consistent. There is no
2751bug at A.
2752
2753Suppose that in the meantime on the lower line of development somebody
2754adds a new calling site for that function at commit Y. The
2755commits from Z leading to B all assume the old semantics of that
2756function and the callers and the callee are consistent with each
2757other. There is no bug at B, either.
2758
2759Suppose further that the two development lines merge cleanly at C,
2760so no conflict resolution is required.
2761
2762Nevertheless, the code at C is broken, because the callers added
2763on the lower line of development have not been converted to the new
2764semantics introduced on the upper line of development. So if all
2765you know is that D is bad, that Z is good, and that
5162e697 2766linkgit:git-bisect[1] identifies C as the culprit, how will you
3fb00282
SP
2767figure out that the problem is due to this change in semantics?
2768
6127c086 2769When the result of a `git bisect` is a non-merge commit, you should
3fb00282
SP
2770normally be able to discover the problem by examining just that commit.
2771Developers can make this easy by breaking their changes into small
2772self-contained commits. That won't help in the case above, however,
2773because the problem isn't obvious from examination of any single
2774commit; instead, a global view of the development is required. To
2775make matters worse, the change in semantics in the problematic
2776function may be just one small part of the changes in the upper
2777line of development.
2778
2779On the other hand, if instead of merging at C you had rebased the
2780history between Z to B on top of A, you would have gotten this
2781linear history:
2782
2783................................................................
2784 ---Z---o---X--...---o---A---o---o---Y*--...---o---B*--D*
2785................................................................
2786
2787Bisecting between Z and D* would hit a single culprit commit Y*,
2788and understanding why Y* was broken would probably be easier.
2789
2de9b711 2790Partly for this reason, many experienced Git users, even when
3fb00282
SP
2791working on an otherwise merge-heavy project, keep the history
2792linear by rebasing against the latest upstream version before
2793publishing.
2794
e34caace 2795[[advanced-branch-management]]
b181d57f
BF
2796Advanced branch management
2797==========================
4c63ff45 2798
e34caace 2799[[fetching-individual-branches]]
b181d57f
BF
2800Fetching individual branches
2801----------------------------
2802
5162e697 2803Instead of using linkgit:git-remote[1], you can also choose just
b181d57f
BF
2804to update one branch at a time, and to store it locally under an
2805arbitrary name:
2806
2807-------------------------------------------------
2808$ git fetch origin todo:my-todo-work
2809-------------------------------------------------
2810
1249d8ad 2811The first argument, `origin`, just tells Git to fetch from the
2de9b711 2812repository you originally cloned from. The second argument tells Git
1249d8ad
TK
2813to fetch the branch named `todo` from the remote repository, and to
2814store it locally under the name `refs/heads/my-todo-work`.
b181d57f
BF
2815
2816You can also fetch branches from other repositories; so
2817
2818-------------------------------------------------
2819$ git fetch git://example.com/proj.git master:example-master
2820-------------------------------------------------
2821
1249d8ad
TK
2822will create a new branch named `example-master` and store in it the
2823branch named `master` from the repository at the given URL. If you
b181d57f 2824already have a branch named example-master, it will attempt to
59723040
BF
2825<<fast-forwards,fast-forward>> to the commit given by example.com's
2826master branch. In more detail:
b181d57f 2827
59723040
BF
2828[[fetch-fast-forwards]]
2829git fetch and fast-forwards
2830---------------------------
b181d57f 2831
1249d8ad 2832In the previous example, when updating an existing branch, `git fetch`
7a7d4ef6 2833checks to make sure that the most recent commit on the remote
b181d57f
BF
2834branch is a descendant of the most recent commit on your copy of the
2835branch before updating your copy of the branch to point at the new
a75d7b54 2836commit. Git calls this process a <<fast-forwards,fast-forward>>.
b181d57f 2837
a75d7b54 2838A fast-forward looks something like this:
b181d57f 2839
1dc71a91 2840................................................
b181d57f
BF
2841 o--o--o--o <-- old head of the branch
2842 \
2843 o--o--o <-- new head of the branch
1dc71a91 2844................................................
b181d57f
BF
2845
2846
2847In some cases it is possible that the new head will *not* actually be
2848a descendant of the old head. For example, the developer may have
2849realized she made a serious mistake, and decided to backtrack,
2850resulting in a situation like:
2851
1dc71a91 2852................................................
b181d57f
BF
2853 o--o--o--o--a--b <-- old head of the branch
2854 \
2855 o--o--o <-- new head of the branch
1dc71a91 2856................................................
b181d57f 2857
1249d8ad 2858In this case, `git fetch` will fail, and print out a warning.
b181d57f 2859
2de9b711 2860In that case, you can still force Git to update to the new head, as
b181d57f 2861described in the following section. However, note that in the
1249d8ad 2862situation above this may mean losing the commits labeled `a` and `b`,
b181d57f
BF
2863unless you've already created a reference of your own pointing to
2864them.
2865
e34caace 2866[[forcing-fetch]]
6127c086 2867Forcing git fetch to do non-fast-forward updates
b181d57f
BF
2868------------------------------------------------
2869
2870If git fetch fails because the new head of a branch is not a
2871descendant of the old head, you may force the update with:
2872
2873-------------------------------------------------
2874$ git fetch git://example.com/proj.git +master:refs/remotes/example/master
2875-------------------------------------------------
2876
1249d8ad 2877Note the addition of the `+` sign. Alternatively, you can use the `-f`
c64415e2
BF
2878flag to force updates of all the fetched branches, as in:
2879
2880-------------------------------------------------
2881$ git fetch -f origin
2882-------------------------------------------------
2883
2884Be aware that commits that the old version of example/master pointed at
2885may be lost, as we saw in the previous section.
b181d57f 2886
e34caace 2887[[remote-branch-configuration]]
29b9a66f
MM
2888Configuring remote-tracking branches
2889------------------------------------
b181d57f 2890
1249d8ad 2891We saw above that `origin` is just a shortcut to refer to the
79c96c57 2892repository that you originally cloned from. This information is
2de9b711 2893stored in Git configuration variables, which you can see using
5162e697 2894linkgit:git-config[1]:
b181d57f
BF
2895
2896-------------------------------------------------
9d13bda3 2897$ git config -l
b181d57f
BF
2898core.repositoryformatversion=0
2899core.filemode=true
2900core.logallrefupdates=true
2901remote.origin.url=git://git.kernel.org/pub/scm/git/git.git
2902remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
2903branch.master.remote=origin
2904branch.master.merge=refs/heads/master
2905-------------------------------------------------
2906
2907If there are other repositories that you also use frequently, you can
2908create similar configuration options to save typing; for example,
b181d57f
BF
2909
2910-------------------------------------------------
47adb8ac 2911$ git remote add example git://example.com/proj.git
b181d57f
BF
2912-------------------------------------------------
2913
47adb8ac 2914adds the following to `.git/config`:
b181d57f
BF
2915
2916-------------------------------------------------
47adb8ac
TK
2917[remote "example"]
2918 url = git://example.com/proj.git
2919 fetch = +refs/heads/*:refs/remotes/example/*
b181d57f
BF
2920-------------------------------------------------
2921
47adb8ac
TK
2922Also note that the above configuration can be performed by directly
2923editing the file `.git/config` instead of using linkgit:git-remote[1].
b181d57f 2924
47adb8ac
TK
2925After configuring the remote, the following three commands will do the
2926same thing:
b181d57f
BF
2927
2928-------------------------------------------------
47adb8ac
TK
2929$ git fetch git://example.com/proj.git +refs/heads/*:refs/remotes/example/*
2930$ git fetch example +refs/heads/*:refs/remotes/example/*
b181d57f
BF
2931$ git fetch example
2932-------------------------------------------------
2933
5162e697 2934See linkgit:git-config[1] for more details on the configuration
47adb8ac
TK
2935options mentioned above and linkgit:git-fetch[1] for more details on
2936the refspec syntax.
d19fbc3c 2937
d19fbc3c 2938
036f8199
BF
2939[[git-concepts]]
2940Git concepts
2941============
d19fbc3c 2942
036f8199
BF
2943Git is built on a small number of simple but powerful ideas. While it
2944is possible to get things done without understanding them, you will find
2de9b711 2945Git much more intuitive if you do.
036f8199
BF
2946
2947We start with the most important, the <<def_object_database,object
2948database>> and the <<def_index,index>>.
b181d57f 2949
e34caace 2950[[the-object-database]]
b181d57f
BF
2951The Object Database
2952-------------------
2953
1bbf1c79
BF
2954
2955We already saw in <<understanding-commits>> that all commits are stored
2956under a 40-digit "object name". In fact, all the information needed to
2957represent the history of a project is stored in objects with such names.
a6e5ef7d
FC
2958In each case the name is calculated by taking the SHA-1 hash of the
2959contents of the object. The SHA-1 hash is a cryptographic hash function.
1bbf1c79
BF
2960What that means to us is that it is impossible to find two different
2961objects with the same name. This has a number of advantages; among
2962others:
2963
2964- Git can quickly determine whether two objects are identical or not,
2965 just by comparing names.
06ada152 2966- Since object names are computed the same way in every repository, the
1bbf1c79
BF
2967 same content stored in two repositories will always be stored under
2968 the same name.
2969- Git can detect errors when it reads an object, by checking that the
a6e5ef7d 2970 object's name is still the SHA-1 hash of its contents.
1bbf1c79
BF
2971
2972(See <<object-details>> for the details of the object formatting and
a6e5ef7d 2973SHA-1 calculation.)
1bbf1c79
BF
2974
2975There are four different types of objects: "blob", "tree", "commit", and
2976"tag".
2977
2978- A <<def_blob_object,"blob" object>> is used to store file data.
843c81dc 2979- A <<def_tree_object,"tree" object>> ties one or more
1bbf1c79
BF
2980 "blob" objects into a directory structure. In addition, a tree object
2981 can refer to other tree objects, thus creating a directory hierarchy.
2982- A <<def_commit_object,"commit" object>> ties such directory hierarchies
2ef8ac1b 2983 together into a <<def_DAG,directed acyclic graph>> of revisions--each
1bbf1c79
BF
2984 commit contains the object name of exactly one tree designating the
2985 directory hierarchy at the time of the commit. In addition, a commit
2986 refers to "parent" commit objects that describe the history of how we
2987 arrived at that directory hierarchy.
2988- A <<def_tag_object,"tag" object>> symbolically identifies and can be
2989 used to sign other objects. It contains the object name and type of
2990 another object, a symbolic name (of course!) and, optionally, a
2991 signature.
b181d57f 2992
b181d57f
BF
2993The object types in some more detail:
2994
513d419c
BF
2995[[commit-object]]
2996Commit Object
2997~~~~~~~~~~~~~
b181d57f 2998
1bbf1c79 2999The "commit" object links a physical state of a tree with a description
1249d8ad 3000of how we got there and why. Use the `--pretty=raw` option to
5162e697 3001linkgit:git-show[1] or linkgit:git-log[1] to examine your favorite
1bbf1c79
BF
3002commit:
3003
3004------------------------------------------------
3005$ git show -s --pretty=raw 2be7fcb476
3006commit 2be7fcb4764f2dbcee52635b91fedb1b3dcf7ab4
3007tree fb3a8bdd0ceddd019615af4d57a53f43d8cee2bf
3008parent 257a84d9d02e90447b149af58b271c19405edb6a
3009author Dave Watson <dwatson@mimvista.com> 1187576872 -0400
3010committer Junio C Hamano <gitster@pobox.com> 1187591163 -0700
3011
3012 Fix misspelling of 'suppress' in docs
3013
3014 Signed-off-by: Junio C Hamano <gitster@pobox.com>
3015------------------------------------------------
3016
3017As you can see, a commit is defined by:
3018
a6e5ef7d 3019- a tree: The SHA-1 name of a tree object (as defined below), representing
1bbf1c79 3020 the contents of a directory at a certain point in time.
edfbbf7e 3021- parent(s): The SHA-1 name(s) of some number of commits which represent the
9e5d87d4 3022 immediately previous step(s) in the history of the project. The
1bbf1c79
BF
3023 example above has one parent; merge commits may have more than
3024 one. A commit with no parents is called a "root" commit, and
3025 represents the initial revision of a project. Each project must have
3026 at least one root. A project can also have multiple roots, though
3027 that isn't common (or necessarily a good idea).
3028- an author: The name of the person responsible for this change, together
3029 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, for
3032 example, if the author was someone who wrote a patch and emailed it
3033 to the person who used it to create the commit.
3034- a comment describing this commit.
3035
3036Note that a commit does not itself contain any information about what
3037actually changed; all changes are calculated by comparing the contents
3038of the tree referred to by this commit with the trees associated with
2de9b711 3039its parents. In particular, Git does not attempt to record file renames
1bbf1c79
BF
3040explicitly, though it can identify cases where the existence of the same
3041file data at changing paths suggests a rename. (See, for example, the
1249d8ad 3042`-M` option to linkgit:git-diff[1]).
1bbf1c79 3043
5162e697 3044A commit is usually created by linkgit:git-commit[1], which creates a
1bbf1c79
BF
3045commit whose parent is normally the current HEAD, and whose tree is
3046taken from the content currently stored in the index.
b181d57f 3047
e34caace 3048[[tree-object]]
b181d57f 3049Tree Object
971aa71f 3050~~~~~~~~~~~
b181d57f 3051
5162e697
DM
3052The ever-versatile linkgit:git-show[1] command can also be used to
3053examine tree objects, but linkgit:git-ls-tree[1] will give you more
1bbf1c79
BF
3054details:
3055
3056------------------------------------------------
3057$ git ls-tree fb3a8bdd0ce
3058100644 blob 63c918c667fa005ff12ad89437f2fdc80926e21c .gitignore
3059100644 blob 5529b198e8d14decbe4ad99db3f7fb632de0439d .mailmap
3060100644 blob 6ff87c4664981e4397625791c8ea3bbb5f2279a3 COPYING
3061040000 tree 2fb783e477100ce076f6bf57e4a6f026013dc745 Documentation
3062100755 blob 3c0032cec592a765692234f1cba47dfdcc3a9200 GIT-VERSION-GEN
3063100644 blob 289b046a443c0647624607d471289b2c7dcd470b INSTALL
3064100644 blob 4eb463797adc693dc168b926b6932ff53f17d0b1 Makefile
3065100644 blob 548142c327a6790ff8821d67c2ee1eff7a656b52 README
3066...
3067------------------------------------------------
3068
3069As you can see, a tree object contains a list of entries, each with a
a6e5ef7d 3070mode, object type, SHA-1 name, and name, sorted by name. It represents
1bbf1c79
BF
3071the contents of a single directory tree.
3072
3073The object type may be a blob, representing the contents of a file, or
3074another tree, representing the contents of a subdirectory. Since trees
a6e5ef7d
FC
3075and blobs, like all other objects, are named by the SHA-1 hash of their
3076contents, two trees have the same SHA-1 name if and only if their
1bbf1c79 3077contents (including, recursively, the contents of all subdirectories)
2de9b711 3078are identical. This allows Git to quickly determine the differences
1bbf1c79
BF
3079between two related tree objects, since it can ignore any entries with
3080identical object names.
3081
3082(Note: in the presence of submodules, trees may also have commits as
6dd14366 3083entries. See <<submodules>> for documentation.)
1bbf1c79 3084
2de9b711 3085Note that the files all have mode 644 or 755: Git actually only pays
1bbf1c79 3086attention to the executable bit.
b181d57f 3087
513d419c
BF
3088[[blob-object]]
3089Blob Object
3090~~~~~~~~~~~
b181d57f 3091
5162e697 3092You can use linkgit:git-show[1] to examine the contents of a blob; take,
1249d8ad 3093for example, the blob in the entry for `COPYING` from the tree above:
b181d57f 3094
1bbf1c79
BF
3095------------------------------------------------
3096$ git show 6ff87c4664
3097
3098 Note that the only valid version of the GPL as far as this project
3099 is concerned is _this_ particular version of the license (ie v2, not
3100 v2.2 or v3.x or whatever), unless explicitly otherwise stated.
3101...
3102------------------------------------------------
b181d57f 3103
1bbf1c79
BF
3104A "blob" object is nothing but a binary blob of data. It doesn't refer
3105to anything else or have attributes of any kind.
3106
3107Since the blob is entirely defined by its data, if two files in a
3108directory tree (or in multiple different versions of the repository)
3109have the same contents, they will share the same blob object. The object
3110is totally independent of its location in the directory tree, and
3111renaming a file does not change the object that file is associated with.
3112
3113Note that any tree or blob object can be examined using
5162e697 3114linkgit:git-show[1] with the <revision>:<path> syntax. This can
1bbf1c79
BF
3115sometimes be useful for browsing the contents of a tree that is not
3116currently checked out.
b181d57f 3117
e34caace 3118[[trust]]
b181d57f 3119Trust
971aa71f 3120~~~~~
b181d57f 3121
a6e5ef7d 3122If you receive the SHA-1 name of a blob from one source, and its contents
1bbf1c79 3123from another (possibly untrusted) source, you can still trust that those
a6e5ef7d
FC
3124contents are correct as long as the SHA-1 name agrees. This is because
3125the SHA-1 is designed so that it is infeasible to find different contents
1bbf1c79 3126that produce the same hash.
b181d57f 3127
a6e5ef7d 3128Similarly, you need only trust the SHA-1 name of a top-level tree object
1bbf1c79 3129to trust the contents of the entire directory that it refers to, and if
a6e5ef7d 3130you receive the SHA-1 name of a commit from a trusted source, then you
1bbf1c79
BF
3131can easily verify the entire history of commits reachable through
3132parents of that commit, and all of those contents of the trees referred
3133to by those commits.
b181d57f
BF
3134
3135So to introduce some real trust in the system, the only thing you need
3136to do is to digitally sign just 'one' special note, which includes the
3137name of a top-level commit. Your digital signature shows others
3138that you trust that commit, and the immutability of the history of
3139commits tells others that they can trust the whole history.
3140
3141In other words, you can easily validate a whole archive by just
a6e5ef7d 3142sending out a single email that tells the people the name (SHA-1 hash)
b181d57f
BF
3143of the top commit, and digitally sign that email using something
3144like GPG/PGP.
3145
2de9b711 3146To assist in this, Git also provides the tag object...
b181d57f 3147
e34caace 3148[[tag-object]]
b181d57f 3149Tag Object
971aa71f 3150~~~~~~~~~~
b181d57f 3151
1bbf1c79
BF
3152A tag object contains an object, object type, tag name, the name of the
3153person ("tagger") who created the tag, and a message, which may contain
843c81dc 3154a signature, as can be seen using linkgit:git-cat-file[1]:
b181d57f 3155
1bbf1c79
BF
3156------------------------------------------------
3157$ git cat-file tag v1.5.0
3158object 437b1b20df4b356c9342dac8d38849f24ef44f27
3159type commit
3160tag v1.5.0
3161tagger Junio C Hamano <junkio@cox.net> 1171411200 +0000
3162
3163GIT 1.5.0
3164-----BEGIN PGP SIGNATURE-----
3165Version: GnuPG v1.4.6 (GNU/Linux)
3166
3167iD8DBQBF0lGqwMbZpPMRm5oRAuRiAJ9ohBLd7s2kqjkKlq1qqC57SbnmzQCdG4ui
3168nLE/L9aUXdWeTFPron96DLA=
3169=2E+0
3170-----END PGP SIGNATURE-----
3171------------------------------------------------
b181d57f 3172
5162e697
DM
3173See the linkgit:git-tag[1] command to learn how to create and verify tag
3174objects. (Note that linkgit:git-tag[1] can also be used to create
1bbf1c79 3175"lightweight tags", which are not tag objects at all, but just simple
1249d8ad 3176references whose names begin with `refs/tags/`).
b181d57f 3177
09eff7b0 3178[[pack-files]]
2de9b711 3179How Git stores objects efficiently: pack files
09eff7b0
BF
3180~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3181
9644ffdd 3182Newly created objects are initially created in a file named after the
1249d8ad 3183object's SHA-1 hash (stored in `.git/objects`).
09eff7b0
BF
3184
3185Unfortunately this system becomes inefficient once a project has a
3186lot of objects. Try this on an old project:
3187
3188------------------------------------------------
3189$ git count-objects
31906930 objects, 47620 kilobytes
3191------------------------------------------------
3192
3193The first number is the number of objects which are kept in
3194individual files. The second is the amount of space taken up by
3195those "loose" objects.
3196
2de9b711 3197You can save space and make Git faster by moving these loose objects in
09eff7b0
BF
3198to a "pack file", which stores a group of objects in an efficient
3199compressed format; the details of how pack files are formatted can be
3200found in link:technical/pack-format.txt[technical/pack-format.txt].
3201
3202To put the loose objects into a pack, just run git repack:
3203
3204------------------------------------------------
3205$ git repack
3e65ac49
TA
3206Counting 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)
09eff7b0
BF
3211------------------------------------------------
3212
3e65ac49
TA
3213This creates a single "pack file" in .git/objects/pack/
3214containing all currently unpacked objects. You can then run
09eff7b0
BF
3215
3216------------------------------------------------
3217$ git prune
3218------------------------------------------------
3219
3220to remove any of the "loose" objects that are now contained in the
3221pack. This will also remove any unreferenced objects (which may be
1249d8ad 3222created when, for example, you use `git reset` to remove a commit).
09eff7b0 3223You can verify that the loose objects are gone by looking at the
1249d8ad 3224`.git/objects` directory or by running
09eff7b0
BF
3225
3226------------------------------------------------
3227$ git count-objects
32280 objects, 0 kilobytes
3229------------------------------------------------
3230
3231Although the object files are gone, any commands that refer to those
3232objects will work exactly as they did before.
3233
5162e697 3234The linkgit:git-gc[1] command performs packing, pruning, and more for
09eff7b0
BF
3235you, so is normally the only high-level command you need.
3236
3237[[dangling-objects]]
3238Dangling objects
3239~~~~~~~~~~~~~~~~
3240
5162e697 3241The linkgit:git-fsck[1] command will sometimes complain about dangling
09eff7b0
BF
3242objects. They are not a problem.
3243
3244The most common cause of dangling objects is that you've rebased a
3245branch, or you have pulled from somebody else who rebased a branch--see
3246<<cleaning-up-history>>. In that case, the old head of the original
3247branch still exists, as does everything it pointed to. The branch
3248pointer itself just doesn't, since you replaced it with another one.
3249
3250There are also other situations that cause dangling objects. For
1249d8ad 3251example, a "dangling blob" may arise because you did a `git add` of a
09eff7b0
BF
3252file, but then, before you actually committed it and made it part of the
3253bigger picture, you changed something else in that file and committed
2ef8ac1b 3254that *updated* thing--the old state that you added originally ends up
09eff7b0
BF
3255not being pointed to by any commit or tree, so it's now a dangling blob
3256object.
3257
3258Similarly, when the "recursive" merge strategy runs, and finds that
3259there are criss-cross merges and thus more than one merge base (which is
3260fairly unusual, but it does happen), it will generate one temporary
3261midway tree (or possibly even more, if you had lots of criss-crossing
3262merges and more than two merge bases) as a temporary internal merge
3263base, and again, those are real objects, but the end result will not end
3264up pointing to them, so they end up "dangling" in your repository.
3265
3266Generally, dangling objects aren't anything to worry about. They can
3267even be very useful: if you screw something up, the dangling objects can
3268be how you recover your old tree (say, you did a rebase, and realized
2ef8ac1b 3269that you really didn't want to--you can look at what dangling objects
09eff7b0
BF
3270you have, and decide to reset your head to some old dangling state).
3271
3272For commits, you can just use:
3273
3274------------------------------------------------
3275$ gitk <dangling-commit-sha-goes-here> --not --all
3276------------------------------------------------
3277
3278This asks for all the history reachable from the given commit but not
3279from any branch, tag, or other reference. If you decide it's something
3280you want, you can always create a new reference to it, e.g.,
3281
3282------------------------------------------------
3283$ git branch recovered-branch <dangling-commit-sha-goes-here>
3284------------------------------------------------
3285
3286For blobs and trees, you can't do the same, but you can still examine
3287them. You can just do
3288
3289------------------------------------------------
3290$ git show <dangling-blob/tree-sha-goes-here>
3291------------------------------------------------
3292
3293to show what the contents of the blob were (or, for a tree, basically
1249d8ad 3294what the `ls` for that directory was), and that may give you some idea
09eff7b0
BF
3295of what the operation was that left that dangling object.
3296
3297Usually, dangling blobs and trees aren't very interesting. They're
3298almost always the result of either being a half-way mergebase (the blob
3299will often even have the conflict markers from a merge in it, if you
3300have had conflicting merges that you fixed up by hand), or simply
1249d8ad 3301because you interrupted a `git fetch` with ^C or something like that,
09eff7b0
BF
3302leaving _some_ of the new objects in the object database, but just
3303dangling and useless.
3304
3305Anyway, once you are sure that you're not interested in any dangling
3306state, you can just prune all unreachable objects:
3307
3308------------------------------------------------
3309$ git prune
3310------------------------------------------------
3311
1249d8ad 3312and they'll be gone. But you should only run `git prune` on a quiescent
2ef8ac1b 3313repository--it's kind of like doing a filesystem fsck recovery: you
09eff7b0
BF
3314don't want to do that while the filesystem is mounted.
3315
1249d8ad 3316(The same is true of `git fsck` itself, btw, but since
6127c086
FC
3317`git fsck` never actually *changes* the repository, it just reports
3318on what it found, `git fsck` itself is never 'dangerous' to run.
09eff7b0
BF
3319Running it while somebody is actually changing the repository can cause
3320confusing and scary messages, but it won't actually do anything bad. In
1249d8ad 3321contrast, running `git prune` while somebody is actively changing the
09eff7b0 3322repository is a *BAD* idea).
b181d57f 3323
1cdade2c
BF
3324[[recovering-from-repository-corruption]]
3325Recovering from repository corruption
3326~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3327
2de9b711
TA
3328By design, Git treats data trusted to it with caution. However, even in
3329the absence of bugs in Git itself, it is still possible that hardware or
1cdade2c
BF
3330operating system errors could corrupt data.
3331
3332The first defense against such problems is backups. You can back up a
2de9b711 3333Git directory using clone, or just using cp, tar, or any other backup
1cdade2c
BF
3334mechanism.
3335
3336As a last resort, you can search for the corrupted objects and attempt
3337to replace them by hand. Back up your repository before attempting this
3338in case you corrupt things even more in the process.
3339
3340We'll assume that the problem is a single missing or corrupted blob,
9e5d87d4 3341which is sometimes a solvable problem. (Recovering missing trees and
1cdade2c
BF
3342especially commits is *much* harder).
3343
3344Before starting, verify that there is corruption, and figure out where
5162e697 3345it is with linkgit:git-fsck[1]; this may be time-consuming.
1cdade2c
BF
3346
3347Assume the output looks like this:
3348
3349------------------------------------------------
c6a13b2c 3350$ git fsck --full --no-dangling
1cdade2c
BF
3351broken link from tree 2d9263c6d23595e7cb2a21e5ebbb53655278dff8
3352 to blob 4b9458b3786228369c63936db65827de3cc06200
3353missing blob 4b9458b3786228369c63936db65827de3cc06200
3354------------------------------------------------
3355
1cdade2c
BF
3356Now you know that blob 4b9458b3 is missing, and that the tree 2d9263c6
3357points to it. If you could find just one copy of that missing blob
3358object, possibly in some other repository, you could move it into
1249d8ad 3359`.git/objects/4b/9458b3...` and be done. Suppose you can't. You can
5162e697 3360still examine the tree that pointed to it with linkgit:git-ls-tree[1],
1cdade2c
BF
3361which might output something like:
3362
3363------------------------------------------------
3364$ git ls-tree 2d9263c6d23595e7cb2a21e5ebbb53655278dff8
3365100644 blob 8d14531846b95bfa3564b58ccfb7913a034323b8 .gitignore
3366100644 blob ebf9bf84da0aab5ed944264a5db2a65fe3a3e883 .mailmap
3367100644 blob ca442d313d86dc67e0a2e5d584b465bd382cbf5c COPYING
3368...
3369100644 blob 4b9458b3786228369c63936db65827de3cc06200 myfile
3370...
3371------------------------------------------------
3372
3373So now you know that the missing blob was the data for a file named
1249d8ad
TK
3374`myfile`. And chances are you can also identify the directory--let's
3375say it's in `somedirectory`. If you're lucky the missing copy might be
1cdade2c 3376the same as the copy you have checked out in your working tree at
1249d8ad 3377`somedirectory/myfile`; you can test whether that's right with
5162e697 3378linkgit:git-hash-object[1]:
1cdade2c
BF
3379
3380------------------------------------------------
3381$ git hash-object -w somedirectory/myfile
3382------------------------------------------------
3383
3384which will create and store a blob object with the contents of
a6e5ef7d 3385somedirectory/myfile, and output the SHA-1 of that object. if you're
1cdade2c
BF
3386extremely lucky it might be 4b9458b3786228369c63936db65827de3cc06200, in
3387which case you've guessed right, and the corruption is fixed!
3388
3389Otherwise, you need more information. How do you tell which version of
3390the file has been lost?
3391
3392The easiest way to do this is with:
3393
3394------------------------------------------------
3395$ git log --raw --all --full-history -- somedirectory/myfile
3396------------------------------------------------
3397
3398Because you're asking for raw output, you'll now get something like
3399
3400------------------------------------------------
3401commit abc
3402Author:
3403Date:
3404...
3405:100644 100644 4b9458b... newsha... M somedirectory/myfile
3406
3407
3408commit xyz
3409Author:
3410Date:
3411
3412...
3413:100644 100644 oldsha... 4b9458b... M somedirectory/myfile
3414------------------------------------------------
3415
edfbbf7e
ŠN
3416This tells you that the immediately following version of the file was
3417"newsha", and that the immediately preceding version was "oldsha".
1cdade2c
BF
3418You also know the commit messages that went with the change from oldsha
3419to 4b9458b and with the change from 4b9458b to newsha.
3420
3421If you've been committing small enough changes, you may now have a good
3422shot at reconstructing the contents of the in-between state 4b9458b.
3423
3424If you can do that, you can now recreate the missing object with
3425
3426------------------------------------------------
3427$ git hash-object -w <recreated-file>
3428------------------------------------------------
3429
3430and your repository is good again!
3431
1249d8ad 3432(Btw, you could have ignored the `fsck`, and started with doing a
1cdade2c
BF
3433
3434------------------------------------------------
3435$ git log --raw --all
3436------------------------------------------------
3437
1ab58e8d 3438and just looked for the sha of the missing object (4b9458b..) in that
da2c7b3d 3439whole thing. It's up to you--Git does *have* a lot of information, it is
1cdade2c
BF
3440just missing one particular blob version.
3441
e34caace 3442[[the-index]]
1c097891
BF
3443The index
3444-----------
3445
1249d8ad 3446The index is a binary file (generally kept in `.git/index`) containing a
a6e5ef7d 3447sorted list of path names, each with permissions and the SHA-1 of a blob
5162e697 3448object; linkgit:git-ls-files[1] can show you the contents of the index:
b181d57f 3449
1c097891
BF
3450-------------------------------------------------
3451$ git ls-files --stage
3452100644 63c918c667fa005ff12ad89437f2fdc80926e21c 0 .gitignore
3453100644 5529b198e8d14decbe4ad99db3f7fb632de0439d 0 .mailmap
3454100644 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0 COPYING
3455100644 a37b2152bd26be2c2289e1f57a292534a51a93c7 0 Documentation/.gitignore
3456100644 fbefe9a45b00a54b58d94d06eca48b03d40a50e0 0 Documentation/Makefile
3457...
3458100644 2511aef8d89ab52be5ec6a5e46236b4b6bcd07ea 0 xdiff/xtypes.h
3459100644 2ade97b2574a9f77e7ae4002a4e07a6a38e46d07 0 xdiff/xutils.c
3460100644 d5de8292e05e7c36c4b68857c1cf9855e3d2f70a 0 xdiff/xutils.h
3461-------------------------------------------------
3462
3463Note that in older documentation you may see the index called the
3464"current directory cache" or just the "cache". It has three important
3465properties:
3466
34671. The index contains all the information necessary to generate a single
3468(uniquely determined) tree object.
3469+
5162e697 3470For example, running linkgit:git-commit[1] generates this tree object
1c097891
BF
3471from the index, stores it in the object database, and uses it as the
3472tree object associated with the new commit.
3473
34742. The index enables fast comparisons between the tree object it defines
3475and the working tree.
3476+
3477It does this by storing some additional data for each entry (such as
3478the last modified time). This data is not displayed above, and is not
3479stored in the created tree object, but it can be used to determine
3480quickly which files in the working directory differ from what was
2de9b711 3481stored in the index, and thus save Git from having to read all of the
1c097891
BF
3482data from such files to look for changes.
3483
34843. It can efficiently represent information about merge conflicts
3485between different tree objects, allowing each pathname to be
b181d57f 3486associated with sufficient information about the trees involved that
1c097891
BF
3487you can create a three-way merge between them.
3488+
3489We saw in <<conflict-resolution>> that during a merge the index can
3490store multiple versions of a single file (called "stages"). The third
5162e697 3491column in the linkgit:git-ls-files[1] output above is the stage
1c097891
BF
3492number, and will take on values other than 0 for files with merge
3493conflicts.
3494
3495The index is thus a sort of temporary staging area, which is filled with
3496a tree which you are in the process of working on.
3497
3498If you blow the index away entirely, you generally haven't lost any
3499information as long as you have the name of the tree that it described.
b181d57f 3500
38a457ba
MV
3501[[submodules]]
3502Submodules
3503==========
3504
6dd14366
MS
3505Large projects are often composed of smaller, self-contained modules. For
3506example, an embedded Linux distribution's source tree would include every
3507piece of software in the distribution with some local modifications; a movie
3508player might need to build against a specific, known-working version of a
3509decompression library; several independent programs might all share the same
3510build scripts.
3511
3512With centralized revision control systems this is often accomplished by
3513including every module in one single repository. Developers can check out
3514all modules or only the modules they need to work with. They can even modify
3515files across several modules in a single commit while moving things around
3516or updating APIs and translations.
3517
3518Git does not allow partial checkouts, so duplicating this approach in Git
3519would force developers to keep a local copy of modules they are not
3520interested in touching. Commits in an enormous checkout would be slower
3521than 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.
3523
3524On the plus side, distributed revision control systems can much better
3525integrate with external sources. In a centralized model, a single arbitrary
3526snapshot of the external project is exported from its own revision control
3527and then imported into the local revision control on a vendor branch. All
3528the history is hidden. With distributed revision control you can clone the
3529entire external history and much more easily follow development and re-merge
3530local changes.
3531
3532Git's submodule support allows a repository to contain, as a subdirectory, a
3533checkout of an external project. Submodules maintain their own identity;
3534the submodule support just stores the submodule repository location and
3535commit ID, so other developers who clone the containing project
3536("superproject") can easily clone all the submodules at the same revision.
3537Partial checkouts of the superproject are possible: you can tell Git to
3538clone none, some or all of the submodules.
3539
5162e697 3540The linkgit:git-submodule[1] command is available since Git 1.5.3. Users
6dd14366
MS
3541with Git 1.5.2 can look up the submodule commits in the repository and
3542manually check them out; earlier versions won't recognize the submodules at
3543all.
38a457ba 3544
ddd4ddef 3545To see how submodule support works, create four example
38a457ba
MV
3546repositories that can be used later as a submodule:
3547
3548-------------------------------------------------
3549$ mkdir ~/git
3550$ cd ~/git
3551$ for i in a b c d
3552do
3553 mkdir $i
3554 cd $i
3555 git init
3556 echo "module $i" > $i.txt
3557 git add $i.txt
3558 git commit -m "Initial commit, submodule $i"
3559 cd ..
3560done
3561-------------------------------------------------
3562
3563Now create the superproject and add all the submodules:
3564
3565-------------------------------------------------
3566$ mkdir super
3567$ cd super
3568$ git init
3569$ for i in a b c d
3570do
a56bf585 3571 git submodule add ~/git/$i $i
38a457ba
MV
3572done
3573-------------------------------------------------
3574
3575NOTE: Do not use local URLs here if you plan to publish your superproject!
3576
6127c086 3577See what files `git submodule` created:
38a457ba
MV
3578
3579-------------------------------------------------
3580$ ls -a
3581. .. .git .gitmodules a b c d
3582-------------------------------------------------
3583
6127c086 3584The `git submodule add <repo> <path>` command does a couple of things:
38a457ba 3585
1249d8ad 3586- It clones the submodule from `<repo>` to the given `<path>` under the
a56bf585 3587 current directory and by default checks out the master branch.
5162e697 3588- It adds the submodule's clone path to the linkgit:gitmodules[5] file and
6dd14366 3589 adds this file to the index, ready to be committed.
38a457ba
MV
3590- It adds the submodule's current commit ID to the index, ready to be
3591 committed.
3592
3593Commit the superproject:
3594
3595-------------------------------------------------
3596$ git commit -m "Add submodules a, b, c and d."
3597-------------------------------------------------
3598
3599Now clone the superproject:
3600
3601-------------------------------------------------
3602$ cd ..
3603$ git clone super cloned
3604$ cd cloned
3605-------------------------------------------------
3606
3607The submodule directories are there, but they're empty:
3608
3609-------------------------------------------------
3610$ ls -a a
3611. ..
3612$ git submodule status
3613-d266b9873ad50488163457f025db7cdd9683d88b a
3614-e81d457da15309b4fef4249aba9b50187999670d b
3615-c1536a972b9affea0f16e0680ba87332dc059146 c
3616-d96249ff5d57de5de093e6baff9e0aafa5276a74 d
3617-------------------------------------------------
3618
3619NOTE: The commit object names shown above would be different for you, but they
3620should match the HEAD commit object names of your repositories. You can check
3621it by running `git ls-remote ../a`.
3622
3623Pulling down the submodules is a two-step process. First run `git submodule
3624init` to add the submodule repository URLs to `.git/config`:
3625
3626-------------------------------------------------
3627$ git submodule init
3628-------------------------------------------------
3629
6127c086 3630Now use `git submodule update` to clone the repositories and check out the
38a457ba
MV
3631commits specified in the superproject:
3632
3633-------------------------------------------------
3634$ git submodule update
3635$ cd a
3636$ ls -a
3637. .. .git a.txt
3638-------------------------------------------------
3639
6127c086
FC
3640One major difference between `git submodule update` and `git submodule add` is
3641that `git submodule update` checks out a specific commit, rather than the tip
38a457ba
MV
3642of a branch. It's like checking out a tag: the head is detached, so you're not
3643working on a branch.
3644
3645-------------------------------------------------
3646$ git branch
95f9be55 3647* (detached from d266b98)
38a457ba
MV
3648 master
3649-------------------------------------------------
3650
3651If 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 the
3653change within the submodule, and then update the superproject to reference the
3654new commit:
3655
3656-------------------------------------------------
3657$ git checkout master
3658-------------------------------------------------
3659
3660or
3661
3662-------------------------------------------------
3663$ git checkout -b fix-up
3664-------------------------------------------------
3665
3666then
3667
3668-------------------------------------------------
3669$ echo "adding a line again" >> a.txt
3670$ git commit -a -m "Updated the submodule from within the superproject."
3671$ git push
3672$ cd ..
3673$ git diff
3674diff --git a/a b/a
3675index d266b98..261dfac 160000
3676--- a/a
3677+++ b/a
3678@@ -1 +1 @@
3679-Subproject commit d266b9873ad50488163457f025db7cdd9683d88b
3680+Subproject commit 261dfac35cb99d380eb966e102c1197139f7fa24
3681$ git add a
3682$ git commit -m "Updated submodule a."
3683$ git push
3684-------------------------------------------------
3685
3686You have to run `git submodule update` after `git pull` if you want to update
3687submodules, too.
3688
3689Pitfalls with submodules
3690------------------------
3691
3692Always publish the submodule change before publishing the change to the
3693superproject that references it. If you forget to publish the submodule change,
3694others won't be able to clone the repository:
3695
3696-------------------------------------------------
3697$ cd ~/git/super/a
3698$ echo i added another line to this file >> a.txt
3699$ git commit -a -m "doing it wrong this time"
3700$ cd ..
3701$ git add a
3702$ git commit -m "Updated submodule a again."
3703$ git push
3704$ cd ~/git/cloned
3705$ git pull
3706$ git submodule update
3707error: 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-------------------------------------------------
3711
2de9b711 3712In older Git versions it could be easily forgotten to commit new or modified
8d9e7d52 3713files in a submodule, which silently leads to similar problems as not pushing
1249d8ad 3714the submodule changes. Starting with Git 1.7.0 both `git status` and `git diff`
8d9e7d52 3715in the superproject show submodules as modified when they contain new or
1249d8ad
TK
3716modified files to protect against accidentally committing such a state. `git
3717diff` will also add a `-dirty` to the work tree side when generating patch
3718output or used with the `--submodule` option:
8d9e7d52
JL
3719
3720-------------------------------------------------
3721$ git diff
3722diff --git a/sub b/sub
3723--- a/sub
3724+++ b/sub
3725@@ -1 +1 @@
3726-Subproject commit 3f356705649b5d566d97ff843cf193359229a453
3727+Subproject commit 3f356705649b5d566d97ff843cf193359229a453-dirty
3728$ git diff --submodule
3729Submodule sub 3f35670..3f35670-dirty:
3730-------------------------------------------------
3731
38a457ba
MV
3732You also should not rewind branches in a submodule beyond commits that were
3733ever recorded in any superproject.
3734
3735It's not safe to run `git submodule update` if you've made and committed
3736changes within a submodule without checking out a branch first. They will be
3737silently overwritten:
3738
3739-------------------------------------------------
3740$ cat a.txt
3741module a
3742$ echo line added from private2 >> a.txt
3743$ git commit -a -m "line added inside private2"
3744$ cd ..
3745$ git submodule update
3746Submodule path 'a': checked out 'd266b9873ad50488163457f025db7cdd9683d88b'
3747$ cd a
3748$ cat a.txt
3749module a
3750-------------------------------------------------
3751
3752NOTE: The changes are still visible in the submodule's reflog.
3753
91486733
TK
3754If you have uncommitted changes in your submodule working tree, `git
3755submodule update` will not overwrite them. Instead, you get the usual
3756warning about not being able switch from a dirty branch.
38a457ba 3757
1c6045ff 3758[[low-level-operations]]
2de9b711 3759Low-level Git operations
1c6045ff 3760========================
b181d57f 3761
1c6045ff 3762Many of the higher-level commands were originally implemented as shell
2de9b711
TA
3763scripts using a smaller core of low-level Git commands. These can still
3764be useful when doing unusual things with Git, or just as a way to
1c6045ff 3765understand its inner workings.
b181d57f 3766
1bbf1c79
BF
3767[[object-manipulation]]
3768Object access and manipulation
3769------------------------------
3770
5162e697
DM
3771The 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.
1bbf1c79 3773
5162e697 3774The linkgit:git-commit-tree[1] command allows constructing commits with
1bbf1c79
BF
3775arbitrary parents and trees.
3776
5162e697
DM
3777A tree can be created with linkgit:git-write-tree[1] and its data can be
3778accessed by linkgit:git-ls-tree[1]. Two trees can be compared with
3779linkgit:git-diff-tree[1].
1bbf1c79 3780
5162e697
DM
3781A tag is created with linkgit:git-mktag[1], and the signature can be
3782verified by linkgit:git-verify-tag[1], though it is normally simpler to
3783use linkgit:git-tag[1] for both.
1bbf1c79 3784
e34caace 3785[[the-workflow]]
b181d57f
BF
3786The Workflow
3787------------
3788
5162e697
DM
3789High-level operations such as linkgit:git-commit[1],
3790linkgit:git-checkout[1] and linkgit:git-reset[1] work by moving data
06ada152
RW
3791between the working tree, the index, and the object database. Git
3792provides low-level operations which perform each of these steps
3793individually.
1c6045ff 3794
2de9b711 3795Generally, all Git operations work on the index file. Some operations
b181d57f 3796work *purely* on the index file (showing the current state of the
1c6045ff
BF
3797index), but most operations move data between the index file and either
3798the database or the working directory. Thus there are four main
3799combinations:
b181d57f 3800
e34caace 3801[[working-directory-to-index]]
b181d57f
BF
3802working directory -> index
3803~~~~~~~~~~~~~~~~~~~~~~~~~~
3804
5162e697 3805The linkgit:git-update-index[1] command updates the index with
1c6045ff
BF
3806information from the working directory. You generally update the
3807index information by just specifying the filename you want to update,
3808like so:
b181d57f
BF
3809
3810-------------------------------------------------
1c6045ff 3811$ git update-index filename
b181d57f
BF
3812-------------------------------------------------
3813
3814but to avoid common mistakes with filename globbing etc, the command
3815will not normally add totally new entries or remove old entries,
3816i.e. it will normally just update existing cache entries.
3817
2de9b711 3818To tell Git that yes, you really do realize that certain files no
b181d57f
BF
3819longer exist, or that new files should be added, you
3820should use the `--remove` and `--add` flags respectively.
3821
3822NOTE! A `--remove` flag does 'not' mean that subsequent filenames will
3823necessarily be removed: if the files still exist in your directory
3824structure, the index will be updated with their new status, not
10455d2a 3825removed. The only thing `--remove` means is that update-index will be
b181d57f
BF
3826considering a removed file to be a valid thing, and if the file really
3827does not exist any more, it will update the index accordingly.
3828
467c0197 3829As a special case, you can also do `git update-index --refresh`, which
b181d57f
BF
3830will refresh the "stat" information of each index to match the current
3831stat information. It will 'not' update the object status itself, and
3832it will only update the fields that are used to quickly test whether
3833an object still matches its old backing store object.
3834
5162e697
DM
3835The previously introduced linkgit:git-add[1] is just a wrapper for
3836linkgit:git-update-index[1].
1c6045ff 3837
e34caace 3838[[index-to-object-database]]
b181d57f
BF
3839index -> object database
3840~~~~~~~~~~~~~~~~~~~~~~~~
3841
3842You write your current index file to a "tree" object with the program
3843
3844-------------------------------------------------
1c6045ff 3845$ git write-tree
b181d57f
BF
3846-------------------------------------------------
3847
2ef8ac1b 3848that doesn't come with any options--it will just write out the
b181d57f
BF
3849current index into the set of tree objects that describe that state,
3850and it will return the name of the resulting top-level tree. You can
3851use that tree to re-generate the index at any time by going in the
3852other direction:
3853
e34caace 3854[[object-database-to-index]]
b181d57f
BF
3855object database -> index
3856~~~~~~~~~~~~~~~~~~~~~~~~
3857
3858You read a "tree" file from the object database, and use that to
2ef8ac1b 3859populate (and overwrite--don't do this if your index contains any
b181d57f
BF
3860unsaved state that you might want to restore later!) your current
3861index. Normal operation is just
3862
3863-------------------------------------------------
a6e5ef7d 3864$ git read-tree <SHA-1 of tree>
b181d57f
BF
3865-------------------------------------------------
3866
3867and your index file will now be equivalent to the tree that you saved
3868earlier. However, that is only your 'index' file: your working
3869directory contents have not been modified.
3870
e34caace 3871[[index-to-working-directory]]
b181d57f
BF
3872index -> working directory
3873~~~~~~~~~~~~~~~~~~~~~~~~~~
3874
3875You update your working directory from the index by "checking out"
3876files. This is not a very common operation, since normally you'd just
3877keep your files updated, and rather than write to your working
3878directory, you'd tell the index files about the changes in your
6127c086 3879working directory (i.e. `git update-index`).
b181d57f
BF
3880
3881However, if you decide to jump to a new version, or check out somebody
3882else's version, or just restore a previous tree, you'd populate your
3883index file with read-tree, and then you need to check out the result
3884with
3885
3886-------------------------------------------------
b1889c36 3887$ git checkout-index filename
b181d57f
BF
3888-------------------------------------------------
3889
3890or, if you want to check out all of the index, use `-a`.
3891
6127c086 3892NOTE! `git checkout-index` normally refuses to overwrite old files, so
b181d57f 3893if you have an old version of the tree already checked out, you will
1249d8ad 3894need to use the `-f` flag ('before' the `-a` flag or the filename) to
b181d57f
BF
3895'force' the checkout.
3896
3897
3898Finally, there are a few odds and ends which are not purely moving
3899from one representation to the other:
3900
e34caace 3901[[tying-it-all-together]]
b181d57f
BF
3902Tying it all together
3903~~~~~~~~~~~~~~~~~~~~~
3904
1249d8ad 3905To commit a tree you have instantiated with `git write-tree`, you'd
b181d57f 3906create a "commit" object that refers to that tree and the history
2ef8ac1b 3907behind it--most notably the "parent" commits that preceded it in
b181d57f
BF
3908history.
3909
3910Normally a "commit" has one parent: the previous state of the tree
3911before a certain change was made. However, sometimes it can have two
3912or more parent commits, in which case we call it a "merge", due to the
3913fact that such a commit brings together ("merges") two or more
3914previous states represented by other commits.
3915
3916In other words, while a "tree" represents a particular directory state
ddd4ddef 3917of a working directory, a "commit" represents that state in time,
b181d57f
BF
3918and explains how we got there.
3919
3920You create a commit object by giving it the tree that describes the
3921state at the time of the commit, and a list of parents:
3922
3923-------------------------------------------------
0adda936 3924$ git commit-tree <tree> -p <parent> [(-p <parent2>)...]
b181d57f
BF
3925-------------------------------------------------
3926
3927and then giving the reason for the commit on stdin (either through
3928redirection from a pipe or file, or by just typing it at the tty).
3929
6127c086 3930`git commit-tree` will return the name of the object that represents
b181d57f 3931that commit, and you should save it away for later use. Normally,
2de9b711 3932you'd commit a new `HEAD` state, and while Git doesn't care where you
b181d57f
BF
3933save the note about that state, in practice we tend to just write the
3934result to the file pointed at by `.git/HEAD`, so that we can always see
3935what the last committed state was.
3936
3937Here is an ASCII art by Jon Loeliger that illustrates how
3938various pieces fit together.
3939
3940------------
3941
3942 commit-tree
3943 commit obj
3944 +----+
3945 | |
3946 | |
3947 V V
3948 +-----------+
3949 | Object DB |
3950 | Backing |
3951 | Store |
3952 +-----------+
3953 ^
3954 write-tree | |
3955 tree obj | |
3956 | | read-tree
3957 | | tree obj
3958 V
3959 +-----------+
3960 | Index |
3961 | "cache" |
3962 +-----------+
3963 update-index ^
3964 blob obj | |
3965 | |
3966 checkout-index -u | | checkout-index
3967 stat | | blob obj
3968 V
3969 +-----------+
3970 | Working |
3971 | Directory |
3972 +-----------+
3973
3974------------
3975
3976
e34caace 3977[[examining-the-data]]
b181d57f
BF
3978Examining the data
3979------------------
3980
3981You can examine the data represented in the object database and the
3982index with various helper tools. For every object, you can use
5162e697 3983linkgit:git-cat-file[1] to examine details about the
b181d57f
BF
3984object:
3985
3986-------------------------------------------------
b1889c36 3987$ git cat-file -t <objectname>
b181d57f
BF
3988-------------------------------------------------
3989
3990shows the type of the object, and once you have the type (which is
3991usually implicit in where you find the object), you can use
3992
3993-------------------------------------------------
b1889c36 3994$ git cat-file blob|tree|commit|tag <objectname>
b181d57f
BF
3995-------------------------------------------------
3996
3997to show its contents. NOTE! Trees have binary content, and as a result
3998there is a special helper for showing that content, called
6127c086 3999`git ls-tree`, which turns the binary content into a more easily
b181d57f
BF
4000readable form.
4001
4002It's especially instructive to look at "commit" objects, since those
4003tend to be small and fairly self-explanatory. In particular, if you
4004follow the convention of having the top commit name in `.git/HEAD`,
4005you can do
4006
4007-------------------------------------------------
b1889c36 4008$ git cat-file commit HEAD
b181d57f
BF
4009-------------------------------------------------
4010
4011to see what the top commit was.
4012
e34caace 4013[[merging-multiple-trees]]
b181d57f 4014Merging multiple trees
d19fbc3c
BF
4015----------------------
4016
b181d57f
BF
4017Git helps you do a three-way merge, which you can expand to n-way by
4018repeating the merge procedure arbitrary times until you finally
4019"commit" the state. The normal situation is that you'd only do one
4020three-way merge (two parents), and commit it, but if you like to, you
4021can do multiple parents in one go.
4022
4023To do a three-way merge, you need the two sets of "commit" objects
4024that you want to merge, use those to find the closest common parent (a
4025third "commit" object), and then use those commit objects to find the
4026state of the directory ("tree" object) at these points.
4027
4028To get the "base" for the merge, you first look up the common parent
4029of two commits with
4030
4031-------------------------------------------------
b1889c36 4032$ git merge-base <commit1> <commit2>
b181d57f
BF
4033-------------------------------------------------
4034
4035which will return you the commit they are both based on. You should
4036now look up the "tree" objects of those commits, which you can easily
4037do with (for example)
4038
4039-------------------------------------------------
b1889c36 4040$ git cat-file commit <commitname> | head -1
b181d57f
BF
4041-------------------------------------------------
4042
4043since the tree object information is always the first line in a commit
4044object.
4045
1191ee18 4046Once you know the three trees you are going to merge (the one "original"
c64415e2 4047tree, aka the common tree, and the two "result" trees, aka the branches
1191ee18
BF
4048you want to merge), you do a "merge" read into the index. This will
4049complain if it has to throw away your old index contents, so you should
2ef8ac1b 4050make sure that you've committed those--in fact you would normally
1191ee18
BF
4051always do a merge against your last commit (which should thus match what
4052you have in your current index anyway).
b181d57f
BF
4053
4054To do the merge, do
4055
4056-------------------------------------------------
b1889c36 4057$ git read-tree -m -u <origtree> <yourtree> <targettree>
b181d57f
BF
4058-------------------------------------------------
4059
4060which will do all trivial merge operations for you directly in the
4061index file, and you can just write the result out with
467c0197 4062`git write-tree`.
b181d57f
BF
4063
4064
e34caace 4065[[merging-multiple-trees-2]]
b181d57f
BF
4066Merging multiple trees, continued
4067---------------------------------
4068
4069Sadly, many merges aren't trivial. If there are files that have
06ada152 4070been added, moved or removed, or if both branches have modified the
b181d57f
BF
4071same file, you will be left with an index tree that contains "merge
4072entries" in it. Such an index tree can 'NOT' be written out to a tree
4073object, and you will have to resolve any such merge clashes using
4074other tools before you can write out the result.
4075
b1889c36 4076You can examine such index state with `git ls-files --unmerged`
b181d57f
BF
4077command. An example:
4078
4079------------------------------------------------
b1889c36
JN
4080$ git read-tree -m $orig HEAD $target
4081$ git ls-files --unmerged
b181d57f
BF
4082100644 263414f423d0e4d70dae8fe53fa34614ff3e2860 1 hello.c
4083100644 06fa6a24256dc7e560efa5687fa84b51f0263c3a 2 hello.c
4084100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello.c
4085------------------------------------------------
4086
b1889c36 4087Each line of the `git ls-files --unmerged` output begins with
a6e5ef7d 4088the blob mode bits, blob SHA-1, 'stage number', and the
2de9b711 4089filename. The 'stage number' is Git's way to say which tree it
edfbbf7e
ŠN
4090came from: stage 1 corresponds to the `$orig` tree, stage 2 to
4091the `HEAD` tree, and stage 3 to the `$target` tree.
b181d57f
BF
4092
4093Earlier we said that trivial merges are done inside
6127c086 4094`git read-tree -m`. For example, if the file did not change
b181d57f
BF
4095from `$orig` to `HEAD` nor `$target`, or if the file changed
4096from `$orig` to `HEAD` and `$orig` to `$target` the same way,
4097obviously the final outcome is what is in `HEAD`. What the
4098above example shows is that file `hello.c` was changed from
4099`$orig` to `HEAD` and `$orig` to `$target` in a different way.
4100You could resolve this by running your favorite 3-way merge
2de9b711 4101program, e.g. `diff3`, `merge`, or Git's own merge-file, on
c64415e2 4102the blob objects from these three stages yourself, like this:
b181d57f
BF
4103
4104------------------------------------------------
b1889c36
JN
4105$ git cat-file blob 263414f... >hello.c~1
4106$ git cat-file blob 06fa6a2... >hello.c~2
4107$ git cat-file blob cc44c73... >hello.c~3
c64415e2 4108$ git merge-file hello.c~2 hello.c~1 hello.c~3
b181d57f
BF
4109------------------------------------------------
4110
4111This would leave the merge result in `hello.c~2` file, along
4112with conflict markers if there are conflicts. After verifying
2de9b711 4113the merge result makes sense, you can tell Git what the final
b181d57f
BF
4114merge result for this file is by:
4115
4116-------------------------------------------------
4117$ mv -f hello.c~2 hello.c
b1889c36 4118$ git update-index hello.c
b181d57f
BF
4119-------------------------------------------------
4120
6127c086 4121When a path is in the "unmerged" state, running `git update-index` for
2de9b711 4122that path tells Git to mark the path resolved.
b181d57f 4123
2de9b711 4124The above is the description of a Git merge at the lowest level,
b181d57f 4125to help you understand what conceptually happens under the hood.
2de9b711 4126In practice, nobody, not even Git itself, runs `git cat-file` three times
6127c086 4127for this. There is a `git merge-index` program that extracts the
b181d57f
BF
4128stages to temporary files and calls a "merge" script on it:
4129
4130-------------------------------------------------
b1889c36 4131$ git merge-index git-merge-one-file hello.c
b181d57f
BF
4132-------------------------------------------------
4133
6127c086 4134and that is what higher level `git merge -s resolve` is implemented with.
b181d57f 4135
971aa71f 4136[[hacking-git]]
2de9b711 4137Hacking Git
971aa71f
BF
4138===========
4139
2de9b711
TA
4140This chapter covers internal details of the Git implementation which
4141probably only Git developers need to understand.
971aa71f 4142
f2327c6c
BF
4143[[object-details]]
4144Object storage format
4145---------------------
4146
4147All objects have a statically determined "type" which identifies the
4148format of the object (i.e. how it is used, and how it can refer to other
4149objects). There are currently four different object types: "blob",
4150"tree", "commit", and "tag".
4151
4152Regardless of object type, all objects share the following
4153characteristics: they are all deflated with zlib, and have a header
4154that not only specifies their type, but also provides size information
a6e5ef7d 4155about the data in the object. It's worth noting that the SHA-1 hash
f2327c6c
BF
4156that is used to name the object is the hash of the original data
4157plus this header, so `sha1sum` 'file' does not match the object name
4158for 'file'.
2de9b711 4159(Historical note: in the dawn of the age of Git the hash
a6e5ef7d 4160was the SHA-1 of the 'compressed' object.)
f2327c6c
BF
4161
4162As a result, the general consistency of an object can always be tested
4163independently of the contents or the type of the object: all objects can
4164be validated by verifying that (a) their hashes match the content of the
4165file and (b) the object successfully inflates to a stream of bytes that
1249d8ad
TK
4166forms a sequence of
4167`<ascii type without space> + <space> + <ascii decimal size> +
4168<byte\0> + <binary object data>`.
f2327c6c
BF
4169
4170The structured objects can further have their structure and
4171connectivity to other objects verified. This is generally done with
6127c086 4172the `git fsck` program, which generates a full dependency graph
f2327c6c
BF
4173of all objects, and verifies their internal consistency (in addition
4174to just verifying their superficial consistency through the hash).
4175
126640af 4176[[birdview-on-the-source-code]]
a5fc33b4
BF
4177A birds-eye view of Git's source code
4178-------------------------------------
126640af 4179
a5fc33b4
BF
4180It is not always easy for new developers to find their way through Git's
4181source code. This section gives you a little guidance to show where to
4182start.
126640af 4183
a5fc33b4 4184A good place to start is with the contents of the initial commit, with:
126640af
JS
4185
4186----------------------------------------------------
a5fc33b4 4187$ git checkout e83c5163
126640af
JS
4188----------------------------------------------------
4189
2de9b711 4190The initial revision lays the foundation for almost everything Git has
a5fc33b4 4191today, but is small enough to read in one sitting.
126640af 4192
a5fc33b4
BF
4193Note that terminology has changed since that revision. For example, the
4194README in that revision uses the word "changeset" to describe what we
4195now call a <<def_commit_object,commit>>.
126640af 4196
843c81dc 4197Also, we do not call it "cache" any more, but rather "index"; however, the
126640af
JS
4198file 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 is
4200basically _the_ header file which is included by _all_ of Git's C sources.
4201
a5fc33b4
BF
4202If you grasp the ideas in that initial commit, you should check out a
4203more recent version and skim `cache.h`, `object.h` and `commit.h`.
126640af
JS
4204
4205In the early days, Git (in the tradition of UNIX) was a bunch of programs
4206which were extremely simple, and which you used in scripts, piping the
4207output of one into another. This turned out to be good for initial
4208development, since it was easier to test new things. However, recently
4209many of these parts have become builtins, and some of the core has been
4210"libified", i.e. put into libgit.a for performance, portability reasons,
4211and to avoid code duplication.
4212
4213By now, you know what the index is (and find the corresponding data
4214structures in `cache.h`), and that there are just a couple of object types
4215(blobs, trees, commits and tags) which inherit their common structure from
4216`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).
4219
4220Now is a good point to take a break to let this information sink in.
4221
4222Next 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 at
4225the function `get_sha1()`. A lot of the special handling is done by
4226functions like `get_sha1_basic()` or the likes.
4227
4228This is just to get you into the groove for the most libified part of Git:
4229the revision walker.
4230
6127c086 4231Basically, the initial version of `git log` was a shell script:
126640af
JS
4232
4233----------------------------------------------------------------
4234$ git-rev-list --pretty $(git-rev-parse --default HEAD "$@") | \
4235 LESS=-S ${PAGER:-less}
4236----------------------------------------------------------------
4237
4238What does this mean?
4239
6127c086 4240`git rev-list` is the original version of the revision walker, which
126640af 4241_always_ printed a list of revisions to stdout. It is still functional,
57f6ec02 4242and needs to, since most new Git commands start out as scripts using
6127c086 4243`git rev-list`.
126640af 4244
6127c086 4245`git rev-parse` is not as important any more; it was only used to filter out
126640af
JS
4246options that were relevant for the different plumbing commands that were
4247called by the script.
4248
6127c086 4249Most of what `git rev-list` did is contained in `revision.c` and
126640af
JS
4250`revision.h`. It wraps the options in a struct named `rev_info`, which
4251controls how and what revisions are walked, and more.
4252
6127c086 4253The original job of `git rev-parse` is now taken by the function
126640af
JS
4254`setup_revisions()`, which parses the revisions and the common command line
4255options for the revision walker. This information is stored in the struct
4256`rev_info` for later consumption. You can do your own command line option
4257parsing after calling `setup_revisions()`. After that, you have to call
4258`prepare_revision_walk()` for initialization, and then you can get the
4259commits one by one with the function `get_revision()`.
4260
4261If you are interested in more details of the revision walking process,
4262just have a look at the first implementation of `cmd_log()`; call
6cf378f0 4263`git show v1.3.0~155^2~4` and scroll down to that function (note that you
126640af
JS
4264no longer need to call `setup_pager()` directly).
4265
6127c086 4266Nowadays, `git log` is a builtin, which means that it is _contained_ in the
126640af
JS
4267command `git`. The source side of a builtin is
4268
09b7e220
PH
4269- 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`.
126640af
JS
4272
4273- an entry in the `commands[]` array in `git.c`, and
4274
4275- an entry in `BUILTIN_OBJECTS` in the `Makefile`.
4276
4277Sometimes, more than one builtin is contained in one source file. For
09b7e220 4278example, `cmd_whatchanged()` and `cmd_log()` both reside in `builtin/log.c`,
126640af
JS
4279since they share quite a bit of code. In that case, the commands which are
4280_not_ named like the `.c` file in which they live have to be listed in
4281`BUILT_INS` in the `Makefile`.
4282
6127c086 4283`git log` looks more complicated in C than it does in the original script,
126640af
JS
4284but that allows for a much greater flexibility and performance.
4285
4286Here again it is a good point to take a pause.
4287
4288Lesson three is: study the code. Really, it is the best way to learn about
4289the organization of Git (after you know the basic concepts).
4290
4291So, think about something which you are interested in, say, "how can I
4292access a blob just knowing the object name of it?". The first step is to
4293find a Git command with which you can do it. In this example, it is either
6127c086 4294`git show` or `git cat-file`.
126640af 4295
6127c086 4296For the sake of clarity, let's stay with `git cat-file`, because it
126640af
JS
4297
4298- is plumbing, and
4299
4300- was around even in the initial commit (it literally went only through
09b7e220 4301 some 20 revisions as `cat-file.c`, was renamed to `builtin/cat-file.c`
126640af
JS
4302 when made a builtin, and then saw less than 10 versions).
4303
09b7e220 4304So, look into `builtin/cat-file.c`, search for `cmd_cat_file()` and look what
126640af
JS
4305it does.
4306
4307------------------------------------------------------------------
4308 git_config(git_default_config);
4309 if (argc != 3)
6127c086 4310 usage("git cat-file [-t|-s|-e|-p|<type>] <sha1>");
126640af
JS
4311 if (get_sha1(argv[2], sha1))
4312 die("Not a valid object name %s", argv[2]);
4313------------------------------------------------------------------
4314
4315Let's skip over the obvious details; the only really interesting part
4316here is the call to `get_sha1()`. It tries to interpret `argv[2]` as an
4317object name, and if it refers to an object which is present in the current
4318repository, it writes the resulting SHA-1 into the variable `sha1`.
4319
4320Two things are interesting here:
4321
4322- `get_sha1()` returns 0 on _success_. This might surprise some new
4323 Git hackers, but there is a long tradition in UNIX to return different
2ef8ac1b 4324 negative numbers in case of different errors--and 0 on success.
126640af
JS
4325
4326- the variable `sha1` in the function signature of `get_sha1()` is `unsigned
6cf378f0 4327 char *`, but is actually expected to be a pointer to `unsigned
126640af 4328 char[20]`. This variable will contain the 160-bit SHA-1 of the given
6cf378f0 4329 commit. Note that whenever a SHA-1 is passed as `unsigned char *`, it
126640af 4330 is the binary representation, as opposed to the ASCII representation in
a5fc33b4 4331 hex characters, which is passed as `char *`.
126640af
JS
4332
4333You will see both of these things throughout the code.
4334
4335Now, for the meat:
4336
4337-----------------------------------------------------------------------------
4338 case 0:
4339 buf = read_object_with_reference(sha1, argv[1], &size, NULL);
4340-----------------------------------------------------------------------------
4341
4342This is how you read a blob (actually, not only a blob, but any type of
4343object). To know how the function `read_object_with_reference()` actually
4344works, find the source code for it (something like `git grep
2de9b711 4345read_object_with | grep ":[a-z]"` in the Git repository), and read
126640af
JS
4346the source.
4347
4348To find out how the result can be used, just read on in `cmd_cat_file()`:
4349
4350-----------------------------------
4351 write_or_die(1, buf, size);
4352-----------------------------------
4353
4354Sometimes, you do not know where to look for a feature. In many such cases,
6127c086 4355it helps to search through the output of `git log`, and then `git show` the
126640af
JS
4356corresponding commit.
4357
6127c086 4358Example: If you know that there was some test case for `git bundle`, but
126640af
JS
4359do not remember where it was (yes, you _could_ `git grep bundle t/`, but that
4360does not illustrate the point!):
4361
4362------------------------
4363$ git log --no-merges t/
4364------------------------
4365
4366In 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 line
4369
4370-------------------
4371$ git show 18449ab0
4372-------------------
4373
4374Voila.
4375
4376Another example: Find out what to do in order to make some script a
4377builtin:
4378
4379-------------------------------------------------
09b7e220 4380$ git log --no-merges --diff-filter=A builtin/*.c
126640af
JS
4381-------------------------------------------------
4382
4383You see, Git is actually the best tool to find out about the source of Git
4384itself!
4385
e34caace 4386[[glossary]]
73a1d050 4387Git Glossary
497c8331
CC
4388============
4389
4390include::glossary-content.txt[]
d19fbc3c 4391
2624d9a5 4392[[git-quick-start]]
99f171bb
BF
4393Appendix A: Git Quick Reference
4394===============================
2624d9a5 4395
99f171bb
BF
4396This is a quick summary of the major commands; the previous chapters
4397explain how these work in more detail.
2624d9a5
BF
4398
4399[[quick-creating-a-new-repository]]
4400Creating a new repository
4401-------------------------
4402
4403From a tarball:
4404
4405-----------------------------------------------
4406$ tar xzf project.tar.gz
4407$ cd project
4408$ git init
4409Initialized empty Git repository in .git/
4410$ git add .
4411$ git commit
4412-----------------------------------------------
4413
4414From a remote repository:
4415
4416-----------------------------------------------
4417$ git clone git://example.com/pub/project.git
4418$ cd project
4419-----------------------------------------------
4420
4421[[managing-branches]]
4422Managing branches
4423-----------------
4424
4425-----------------------------------------------
4426$ git branch # list all local branches in this repo
4427$ git checkout test # switch working directory to branch "test"
4428$ git branch new # create branch "new" starting at current HEAD
4429$ git branch -d new # delete branch "new"
4430-----------------------------------------------
4431
06ada152 4432Instead of basing a new branch on current HEAD (the default), use:
2624d9a5
BF
4433
4434-----------------------------------------------
4435$ git branch new test # branch named "test"
4436$ git branch new v2.6.15 # tag named v2.6.15
4437$ git branch new HEAD^ # commit before the most recent
4438$ git branch new HEAD^^ # commit before that
4439$ git branch new test~10 # ten commits before tip of branch "test"
4440-----------------------------------------------
4441
4442Create and switch to a new branch at the same time:
4443
4444-----------------------------------------------
4445$ git checkout -b new v2.6.15
4446-----------------------------------------------
4447
4448Update and examine branches from the repository you cloned from:
4449
4450-----------------------------------------------
4451$ git fetch # update
4452$ git branch -r # list
4453 origin/master
4454 origin/next
4455 ...
4456$ git checkout -b masterwork origin/master
4457-----------------------------------------------
4458
4459Fetch a branch from a different repository, and give it a new
4460name in your repository:
4461
4462-----------------------------------------------
4463$ git fetch git://example.com/project.git theirbranch:mybranch
4464$ git fetch git://example.com/project.git v2.6.15:mybranch
4465-----------------------------------------------
4466
4467Keep a list of repositories you work with regularly:
4468
4469-----------------------------------------------
4470$ git remote add example git://example.com/project.git
4471$ git remote # list remote repositories
4472example
4473origin
4474$ git remote show example # get details
4475* remote example
4476 URL: git://example.com/project.git
4477 Tracked remote branches
20244ea2
JS
4478 master
4479 next
4480 ...
2624d9a5
BF
4481$ git fetch example # update branches from example
4482$ git branch -r # list all remote branches
4483-----------------------------------------------
4484
4485
4486[[exploring-history]]
4487Exploring history
4488-----------------
4489
4490-----------------------------------------------
4491$ gitk # visualize and browse history
4492$ git log # list all commits
4493$ git log src/ # ...modifying src/
4494$ git log v2.6.15..v2.6.16 # ...in v2.6.16, not in v2.6.15
4495$ git log master..test # ...in branch test, not in branch master
4496$ git log test..master # ...in branch master, but not in test
4497$ git log test...master # ...in one branch, not in both
4498$ git log -S'foo()' # ...where difference contain "foo()"
4499$ git log --since="2 weeks ago"
4500$ git log -p # show patches as well
4501$ git show # most recent commit
4502$ git diff v2.6.15..v2.6.16 # diff between two tagged versions
4503$ git diff v2.6.15..HEAD # diff with current head
4504$ 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.txt
4507-----------------------------------------------
4508
4509Search for regressions:
4510
4511-----------------------------------------------
4512$ git bisect start
4513$ git bisect bad # current version is bad
4514$ git bisect good v2.6.13-rc2 # last known good revision
4515Bisecting: 675 revisions left to test after this
4516 # test here, then:
4517$ git bisect good # if this revision is good, or
4518$ git bisect bad # if this revision is bad.
4519 # repeat until done.
4520-----------------------------------------------
4521
4522[[making-changes]]
4523Making changes
4524--------------
4525
2de9b711 4526Make sure Git knows who to blame:
2624d9a5
BF
4527
4528------------------------------------------------
4529$ cat >>~/.gitconfig <<\EOF
4530[user]
4531 name = Your Name Comes Here
4532 email = you@yourdomain.example.com
4533EOF
4534------------------------------------------------
4535
4536Select file contents to include in the next commit, then make the
4537commit:
4538
4539-----------------------------------------------
4540$ git add a.txt # updated file
4541$ git add b.txt # new file
4542$ git rm c.txt # old file
4543$ git commit
4544-----------------------------------------------
4545
4546Or, prepare and create the commit in one step:
4547
4548-----------------------------------------------
4549$ git commit d.txt # use latest content only of d.txt
4550$ git commit -a # use latest content of all tracked files
4551-----------------------------------------------
4552
4553[[merging]]
4554Merging
4555-------
4556
4557-----------------------------------------------
4558$ git merge test # merge branch "test" into the current branch
4559$ git pull git://example.com/project.git master
4560 # fetch and merge in remote branch
4561$ git pull . test # equivalent to git merge test
4562-----------------------------------------------
4563
4564[[sharing-your-changes]]
4565Sharing your changes
4566--------------------
4567
4568Importing or exporting patches:
4569
4570-----------------------------------------------
4571$ git format-patch origin..HEAD # format a patch for each commit
4572 # in HEAD but not in origin
4573$ git am mbox # import patches from the mailbox "mbox"
4574-----------------------------------------------
4575
2de9b711 4576Fetch a branch in a different Git repository, then merge into the
2624d9a5
BF
4577current branch:
4578
4579-----------------------------------------------
4580$ git pull git://example.com/project.git theirbranch
4581-----------------------------------------------
4582
4583Store the fetched branch into a local branch before merging into the
4584current branch:
4585
4586-----------------------------------------------
4587$ git pull git://example.com/project.git theirbranch:mybranch
4588-----------------------------------------------
4589
4590After creating commits on a local branch, update the remote
4591branch with your commits:
4592
4593-----------------------------------------------
4594$ git push ssh://example.com/project.git mybranch:theirbranch
4595-----------------------------------------------
4596
4597When remote and local branch are both named "test":
4598
4599-----------------------------------------------
4600$ git push ssh://example.com/project.git test
4601-----------------------------------------------
4602
4603Shortcut version for a frequently used remote repository:
4604
4605-----------------------------------------------
4606$ git remote add example ssh://example.com/project.git
4607$ git push example test
4608-----------------------------------------------
4609
4610[[repository-maintenance]]
4611Repository maintenance
4612----------------------
4613
4614Check for corruption:
4615
4616-----------------------------------------------
4617$ git fsck
4618-----------------------------------------------
4619
4620Recompress, remove unused cruft:
4621
4622-----------------------------------------------
4623$ git gc
4624-----------------------------------------------
4625
4626
e34caace 4627[[todo]]
2624d9a5
BF
4628Appendix B: Notes and todo list for this manual
4629===============================================
6bd9b682
BF
4630
4631This is a work in progress.
4632
4633The basic requirements:
ecd95b53
BF
4634
4635- It must be readable in order, from beginning to end, by someone
4636 intelligent with a basic grasp of the UNIX command line, but without
2de9b711 4637 any special knowledge of Git. If necessary, any other prerequisites
ecd95b53
BF
4638 should be specifically mentioned as they arise.
4639- Whenever possible, section headings should clearly describe the task
4640 they explain how to do, in language that requires no more knowledge
4641 than necessary: for example, "importing patches into a project" rather
6127c086 4642 than "the `git am` command"
6bd9b682 4643
d5cd5de4
BF
4644Think about how to create a clear chapter dependency graph that will
4645allow people to get to important topics without necessarily reading
4646everything in between.
d19fbc3c 4647
1249d8ad 4648Scan `Documentation/` for other stuff left out; in particular:
ecd95b53
BF
4649
4650- howto's
1249d8ad 4651- some of `technical/`?
ecd95b53 4652- hooks
5162e697 4653- list of commands in linkgit:git[1]
d19fbc3c
BF
4654
4655Scan email archives for other stuff left out
4656
4657Scan man pages to see if any assume more background than this manual
4658provides.
4659
2f99710c 4660Simplify beginning by suggesting disconnected head instead of
b181d57f 4661temporary branch creation?
d19fbc3c 4662
2f99710c
BF
4663Add more good examples. Entire sections of just cookbook examples
4664might be a good idea; maybe make an "advanced examples" section a
4665standard end-of-chapter section?
d19fbc3c
BF
4666
4667Include cross-references to the glossary, where appropriate.
4668
9a241220
BF
4669Document shallow clones? See draft 1.5.0 release notes for some
4670documentation.
4671
3dff5379 4672Add a section on working with other version control systems, including
9a241220
BF
4673CVS, Subversion, and just imports of series of release tarballs.
4674
a8cd1402 4675More details on gitweb?
0b375ab0
BF
4676
4677Write a chapter on using plumbing and writing scripts.
d9bd321c
BF
4678
4679Alternates, clone -reference, etc.
4680
1cdade2c 4681More on recovery from repository corruption. See:
efe6de6e
OB
4682 http://marc.info/?l=git&m=117263864820799&w=2
4683 http://marc.info/?l=git&m=117147855503798&w=2