user-manual: add section documenting shallow clones
[git/git.git] / Documentation / user-manual.txt
CommitLineData
333d7d37 1Git User Manual
145e073b 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`::
df47da75 271 list all branches.
1249d8ad
TK
272`git branch <branch>`::
273 create a new branch named `<branch>`, referencing the same
df47da75 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,
df47da75 278 including using a branch name or a tag name.
1249d8ad 279`git branch -d <branch>`::
df47da75
TA
280 delete the branch `<branch>`; if the branch is not fully
281 merged in its upstream branch or contained in the current branch,
282 this command will fail with a warning.
1249d8ad 283`git branch -D <branch>`::
df47da75 284 delete the branch `<branch>` irrespective of its merged status.
1249d8ad
TK
285`git checkout <branch>`::
286 make the current branch `<branch>`, updating the working
df47da75 287 directory to reflect the version referenced by `<branch>`.
1249d8ad
TK
288`git checkout -b <new> <start-point>`::
289 create a new branch `<new>` referencing `<start-point>`, and
d19fbc3c
BF
290 check it out.
291
72a76c95 292The special symbol "HEAD" can always be used to refer to the current
1249d8ad
TK
293branch. In fact, Git uses a file named `HEAD` in the `.git` directory
294to remember which branch is current:
72a76c95
BF
295
296------------------------------------------------
297$ cat .git/HEAD
298ref: refs/heads/master
299------------------------------------------------
300
25d9f3fa 301[[detached-head]]
72a76c95
BF
302Examining an old version without creating a new branch
303------------------------------------------------------
304
6127c086 305The `git checkout` command normally expects a branch head, but will also
72a76c95
BF
306accept an arbitrary commit; for example, you can check out the commit
307referenced by a tag:
308
309------------------------------------------------
310$ git checkout v2.6.17
95f9be55
TA
311Note: checking out 'v2.6.17'.
312
313You are in 'detached HEAD' state. You can look around, make experimental
314changes and commit them, and you can discard any commits you make in this
315state without impacting any branches by performing another checkout.
316
317If you want to create a new branch to retain commits you create, you may
318do so (now or later) by using -b with the checkout command again. Example:
319
320 git checkout -b new_branch_name
321
72a76c95
BF
322HEAD is now at 427abfa... Linux v2.6.17
323------------------------------------------------
324
a6e5ef7d 325The HEAD then refers to the SHA-1 of the commit instead of to a branch,
72a76c95
BF
326and git branch shows that you are no longer on a branch:
327
328------------------------------------------------
329$ cat .git/HEAD
330427abfa28afedffadfca9dd8b067eb6d36bac53f
953f3d6f 331$ git branch
95f9be55 332* (detached from v2.6.17)
72a76c95
BF
333 master
334------------------------------------------------
335
336In this case we say that the HEAD is "detached".
337
953f3d6f
BF
338This is an easy way to check out a particular version without having to
339make up a name for the new branch. You can still create a new branch
340(or tag) for this version later if you decide to.
d19fbc3c 341
e34caace 342[[examining-remote-branches]]
d19fbc3c
BF
343Examining branches from a remote repository
344-------------------------------------------
345
346The "master" branch that was created at the time you cloned is a copy
347of the HEAD in the repository that you cloned from. That repository
348may also have had other branches, though, and your local repository
66a062a1
MM
349keeps branches which track each of those remote branches, called
350remote-tracking branches, which you
1249d8ad 351can view using the `-r` option to linkgit:git-branch[1]:
d19fbc3c
BF
352
353------------------------------------------------
354$ git branch -r
355 origin/HEAD
356 origin/html
357 origin/maint
358 origin/man
359 origin/master
360 origin/next
361 origin/pu
362 origin/todo
363------------------------------------------------
364
66a062a1
MM
365In this example, "origin" is called a remote repository, or "remote"
366for short. The branches of this repository are called "remote
367branches" from our point of view. The remote-tracking branches listed
368above were created based on the remote branches at clone time and will
1249d8ad 369be updated by `git fetch` (hence `git pull`) and `git push`. See
66a062a1
MM
370<<Updating-a-repository-With-git-fetch>> for details.
371
45dfd403
JN
372You might want to build on one of these remote-tracking branches
373on a branch of your own, just as you would for a tag:
d19fbc3c
BF
374
375------------------------------------------------
376$ git checkout -b my-todo-copy origin/todo
377------------------------------------------------
378
1249d8ad 379You can also check out `origin/todo` directly to examine it or
45dfd403
JN
380write a one-off patch. See <<detached-head,detached head>>.
381
2de9b711 382Note that the name "origin" is just the name that Git uses by default
d19fbc3c
BF
383to refer to the repository that you cloned from.
384
385[[how-git-stores-references]]
f60b9642
BF
386Naming branches, tags, and other references
387-------------------------------------------
d19fbc3c
BF
388
389Branches, remote-tracking branches, and tags are all references to
f60b9642 390commits. All references are named with a slash-separated path name
1249d8ad 391starting with `refs`; the names we've been using so far are actually
f60b9642 392shorthand:
d19fbc3c 393
1249d8ad
TK
394 - The branch `test` is short for `refs/heads/test`.
395 - The tag `v2.6.18` is short for `refs/tags/v2.6.18`.
396 - `origin/master` is short for `refs/remotes/origin/master`.
d19fbc3c 397
f60b9642
BF
398The full name is occasionally useful if, for example, there ever
399exists a tag and a branch with the same name.
d19fbc3c 400
1249d8ad 401(Newly created refs are actually stored in the `.git/refs` directory,
fc74ecc1
BF
402under the path given by their name. However, for efficiency reasons
403they may also be packed together in a single file; see
5162e697 404linkgit:git-pack-refs[1]).
fc74ecc1 405
c64415e2
BF
406As another useful shortcut, the "HEAD" of a repository can be referred
407to just using the name of that repository. So, for example, "origin"
408is usually a shortcut for the HEAD branch in the repository "origin".
d19fbc3c 409
2de9b711 410For the complete list of paths which Git checks for references, and
f60b9642
BF
411the order it uses to decide which to choose when there are multiple
412references with the same shorthand name, see the "SPECIFYING
9d83e382 413REVISIONS" section of linkgit:gitrevisions[7].
d19fbc3c 414
aa971cb9 415[[Updating-a-repository-With-git-fetch]]
6127c086 416Updating a repository with git fetch
d19fbc3c
BF
417------------------------------------
418
3c735e07
JM
419After you clone a repository and commit a few changes of your own, you
420may wish to check the original repository for updates.
d19fbc3c 421
3c735e07
JM
422The `git-fetch` command, with no arguments, will update all of the
423remote-tracking branches to the latest version found in the original
d19fbc3c
BF
424repository. It will not touch any of your own branches--not even the
425"master" branch that was created for you on clone.
426
e34caace 427[[fetching-branches]]
d5cd5de4
BF
428Fetching branches from other repositories
429-----------------------------------------
430
431You can also track branches from repositories other than the one you
5162e697 432cloned from, using linkgit:git-remote[1]:
d5cd5de4
BF
433
434-------------------------------------------------
34a25d4c
TK
435$ git remote add staging git://git.kernel.org/.../gregkh/staging.git
436$ git fetch staging
437...
438From git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging
439 * [new branch] master -> staging/master
440 * [new branch] staging-linus -> staging/staging-linus
441 * [new branch] staging-next -> staging/staging-next
d5cd5de4
BF
442-------------------------------------------------
443
444New remote-tracking branches will be stored under the shorthand name
34a25d4c 445that you gave `git remote add`, in this case `staging`:
d5cd5de4
BF
446
447-------------------------------------------------
448$ git branch -r
34a25d4c
TK
449 origin/HEAD -> origin/master
450 origin/master
451 staging/master
452 staging/staging-linus
453 staging/staging-next
d5cd5de4
BF
454-------------------------------------------------
455
1249d8ad
TK
456If you run `git fetch <remote>` later, the remote-tracking branches
457for the named `<remote>` will be updated.
d5cd5de4 458
1249d8ad 459If you examine the file `.git/config`, you will see that Git has added
d5cd5de4
BF
460a new stanza:
461
462-------------------------------------------------
463$ cat .git/config
464...
34a25d4c
TK
465[remote "staging"]
466 url = git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging.git
467 fetch = +refs/heads/*:refs/remotes/staging/*
d5cd5de4
BF
468...
469-------------------------------------------------
470
2de9b711 471This is what causes Git to track the remote's branches; you may modify
1249d8ad 472or delete these configuration options by editing `.git/config` with a
fc90c536 473text editor. (See the "CONFIGURATION FILE" section of
5162e697 474linkgit:git-config[1] for details.)
d5cd5de4 475
e34caace 476[[exploring-git-history]]
2de9b711 477Exploring Git history
d19fbc3c
BF
478=====================
479
480Git is best thought of as a tool for storing the history of a
481collection of files. It does this by storing compressed snapshots of
1130845b 482the contents of a file hierarchy, together with "commits" which show
d19fbc3c
BF
483the relationships between these snapshots.
484
485Git provides extremely flexible and fast tools for exploring the
486history of a project.
487
aacd404e 488We start with one specialized tool that is useful for finding the
d19fbc3c
BF
489commit that introduced a bug into a project.
490
e34caace 491[[using-bisect]]
d19fbc3c
BF
492How to use bisect to find a regression
493--------------------------------------
494
495Suppose version 2.6.18 of your project worked, but the version at
496"master" crashes. Sometimes the best way to find the cause of such a
497regression is to perform a brute-force search through the project's
498history to find the particular commit that caused the problem. The
5162e697 499linkgit:git-bisect[1] command can help you do this:
d19fbc3c
BF
500
501-------------------------------------------------
502$ git bisect start
503$ git bisect good v2.6.18
504$ git bisect bad master
505Bisecting: 3537 revisions left to test after this
506[65934a9a028b88e83e2b0f8b36618fe503349f8e] BLOCK: Make USB storage depend on SCSI rather than selecting it [try #6]
507-------------------------------------------------
508
1249d8ad 509If you run `git branch` at this point, you'll see that Git has
0e25790f
CC
510temporarily moved you in "(no branch)". HEAD is now detached from any
511branch and points directly to a commit (with commit id 65934...) that
512is reachable from "master" but not from v2.6.18. Compile and test it,
513and see whether it crashes. Assume it does crash. Then:
d19fbc3c
BF
514
515-------------------------------------------------
516$ git bisect bad
517Bisecting: 1769 revisions left to test after this
518[7eff82c8b1511017ae605f0c99ac275a7e21b867] i2c-core: Drop useless bitmaskings
519-------------------------------------------------
520
2de9b711 521checks out an older version. Continue like this, telling Git at each
d19fbc3c
BF
522stage whether the version it gives you is good or bad, and notice
523that the number of revisions left to test is cut approximately in
524half each time.
525
526After about 13 tests (in this case), it will output the commit id of
527the guilty commit. You can then examine the commit with
5162e697 528linkgit:git-show[1], find out who wrote it, and mail them your bug
d19fbc3c
BF
529report with the commit id. Finally, run
530
531-------------------------------------------------
532$ git bisect reset
533-------------------------------------------------
534
0e25790f 535to return you to the branch you were on before.
d19fbc3c 536
6127c086 537Note that the version which `git bisect` checks out for you at each
d19fbc3c
BF
538point is just a suggestion, and you're free to try a different
539version if you think it would be a good idea. For example,
540occasionally you may land on a commit that broke something unrelated;
541run
542
543-------------------------------------------------
04483524 544$ git bisect visualize
d19fbc3c
BF
545-------------------------------------------------
546
547which will run gitk and label the commit it chose with a marker that
843c81dc 548says "bisect". Choose a safe-looking commit nearby, note its commit
d19fbc3c
BF
549id, and check it out with:
550
551-------------------------------------------------
552$ git reset --hard fb47ddb2db...
553-------------------------------------------------
554
1249d8ad 555then test, run `bisect good` or `bisect bad` as appropriate, and
d19fbc3c
BF
556continue.
557
1249d8ad
TK
558Instead of `git bisect visualize` and then `git reset --hard
559fb47ddb2db...`, you might just want to tell Git that you want to skip
0e25790f
CC
560the current commit:
561
562-------------------------------------------------
563$ git bisect skip
564-------------------------------------------------
565
2de9b711 566In this case, though, Git may not eventually be able to tell the first
a0178ae2 567bad one between some first skipped commits and a later bad commit.
0e25790f
CC
568
569There are also ways to automate the bisecting process if you have a
570test script that can tell a good from a bad commit. See
1249d8ad
TK
571linkgit:git-bisect[1] for more information about this and other `git
572bisect` features.
0e25790f 573
e34caace 574[[naming-commits]]
d19fbc3c
BF
575Naming commits
576--------------
577
578We have seen several ways of naming commits already:
579
d55ae921 580 - 40-hexdigit object name
d19fbc3c
BF
581 - branch name: refers to the commit at the head of the given
582 branch
583 - tag name: refers to the commit pointed to by the given tag
584 (we've seen branches and tags are special cases of
585 <<how-git-stores-references,references>>).
586 - HEAD: refers to the head of the current branch
587
eb6ae7f4 588There are many more; see the "SPECIFYING REVISIONS" section of the
9d83e382 589linkgit:gitrevisions[7] man page for the complete list of ways to
d19fbc3c
BF
590name revisions. Some examples:
591
592-------------------------------------------------
d55ae921 593$ git show fb47ddb2 # the first few characters of the object name
d19fbc3c
BF
594 # are usually enough to specify it uniquely
595$ git show HEAD^ # the parent of the HEAD commit
596$ git show HEAD^^ # the grandparent
597$ git show HEAD~4 # the great-great-grandparent
598-------------------------------------------------
599
600Recall that merge commits may have more than one parent; by default,
1249d8ad 601`^` and `~` follow the first parent listed in the commit, but you can
d19fbc3c
BF
602also choose:
603
604-------------------------------------------------
605$ git show HEAD^1 # show the first parent of HEAD
606$ git show HEAD^2 # show the second parent of HEAD
607-------------------------------------------------
608
609In addition to HEAD, there are several other special names for
610commits:
611
612Merges (to be discussed later), as well as operations such as
6127c086 613`git reset`, which change the currently checked-out commit, generally
d19fbc3c
BF
614set ORIG_HEAD to the value HEAD had before the current operation.
615
6127c086
FC
616The `git fetch` operation always stores the head of the last fetched
617branch in FETCH_HEAD. For example, if you run `git fetch` without
d19fbc3c
BF
618specifying a local branch as the target of the operation
619
620-------------------------------------------------
621$ git fetch git://example.com/proj.git theirbranch
622-------------------------------------------------
623
624the fetched commits will still be available from FETCH_HEAD.
625
626When we discuss merges we'll also see the special name MERGE_HEAD,
627which refers to the other branch that we're merging in to the current
628branch.
629
5162e697 630The linkgit:git-rev-parse[1] command is a low-level command that is
d55ae921
BF
631occasionally useful for translating some name for a commit to the object
632name for that commit:
aec053bb
BF
633
634-------------------------------------------------
635$ git rev-parse origin
636e05db0fd4f31dde7005f075a84f96b360d05984b
637-------------------------------------------------
638
e34caace 639[[creating-tags]]
d19fbc3c
BF
640Creating tags
641-------------
642
643We can also create a tag to refer to a particular commit; after
644running
645
646-------------------------------------------------
04483524 647$ git tag stable-1 1b2e1d63ff
d19fbc3c
BF
648-------------------------------------------------
649
1249d8ad 650You can use `stable-1` to refer to the commit 1b2e1d63ff.
d19fbc3c 651
c64415e2
BF
652This creates a "lightweight" tag. If you would also like to include a
653comment with the tag, and possibly sign it cryptographically, then you
5162e697 654should create a tag object instead; see the linkgit:git-tag[1] man page
c64415e2 655for details.
d19fbc3c 656
e34caace 657[[browsing-revisions]]
d19fbc3c
BF
658Browsing revisions
659------------------
660
5162e697 661The linkgit:git-log[1] command can show lists of commits. On its
d19fbc3c
BF
662own, it shows all commits reachable from the parent commit; but you
663can also make more specific requests:
664
665-------------------------------------------------
666$ git log v2.5.. # commits since (not reachable from) v2.5
667$ git log test..master # commits reachable from master but not test
668$ git log master..test # ...reachable from test but not master
669$ git log master...test # ...reachable from either test or master,
670 # but not both
671$ git log --since="2 weeks ago" # commits from the last 2 weeks
672$ git log Makefile # commits which modify Makefile
673$ git log fs/ # ... which modify any file under fs/
674$ git log -S'foo()' # commits which add or remove any file data
675 # matching the string 'foo()'
676-------------------------------------------------
677
678And of course you can combine all of these; the following finds
1249d8ad 679commits since v2.5 which touch the `Makefile` or any file under `fs`:
d19fbc3c
BF
680
681-------------------------------------------------
682$ git log v2.5.. Makefile fs/
683-------------------------------------------------
684
685You can also ask git log to show patches:
686
687-------------------------------------------------
688$ git log -p
689-------------------------------------------------
690
1249d8ad 691See the `--pretty` option in the linkgit:git-log[1] man page for more
d19fbc3c
BF
692display options.
693
694Note that git log starts with the most recent commit and works
2de9b711 695backwards through the parents; however, since Git history can contain
3dff5379 696multiple independent lines of development, the particular order that
d19fbc3c
BF
697commits are listed in may be somewhat arbitrary.
698
e34caace 699[[generating-diffs]]
d19fbc3c
BF
700Generating diffs
701----------------
702
703You can generate diffs between any two versions using
5162e697 704linkgit:git-diff[1]:
d19fbc3c
BF
705
706-------------------------------------------------
707$ git diff master..test
708-------------------------------------------------
709
5b98d9bc
BF
710That will produce the diff between the tips of the two branches. If
711you'd prefer to find the diff from their common ancestor to test, you
712can use three dots instead of two:
713
714-------------------------------------------------
715$ git diff master...test
716-------------------------------------------------
717
718Sometimes what you want instead is a set of patches; for this you can
5162e697 719use linkgit:git-format-patch[1]:
d19fbc3c
BF
720
721-------------------------------------------------
722$ git format-patch master..test
723-------------------------------------------------
724
725will generate a file with a patch for each commit reachable from test
5b98d9bc 726but not from master.
d19fbc3c 727
e34caace 728[[viewing-old-file-versions]]
d19fbc3c
BF
729Viewing old file versions
730-------------------------
731
732You can always view an old version of a file by just checking out the
733correct revision first. But sometimes it is more convenient to be
734able to view an old version of a single file without checking
735anything out; this command does that:
736
737-------------------------------------------------
738$ git show v2.5:fs/locks.c
739-------------------------------------------------
740
741Before the colon may be anything that names a commit, and after it
2de9b711 742may be any path to a file tracked by Git.
d19fbc3c 743
e34caace 744[[history-examples]]
aec053bb
BF
745Examples
746--------
747
46acd3fa
BF
748[[counting-commits-on-a-branch]]
749Counting the number of commits on a branch
750~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
751
1249d8ad
TK
752Suppose you want to know how many commits you've made on `mybranch`
753since it diverged from `origin`:
46acd3fa
BF
754
755-------------------------------------------------
756$ git log --pretty=oneline origin..mybranch | wc -l
757-------------------------------------------------
758
759Alternatively, you may often see this sort of thing done with the
a6e5ef7d 760lower-level command linkgit:git-rev-list[1], which just lists the SHA-1's
46acd3fa
BF
761of all the given commits:
762
763-------------------------------------------------
764$ git rev-list origin..mybranch | wc -l
765-------------------------------------------------
766
e34caace 767[[checking-for-equal-branches]]
aec053bb 768Check whether two branches point at the same history
2f99710c 769~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
aec053bb
BF
770
771Suppose you want to check whether two branches point at the same point
772in history.
773
774-------------------------------------------------
775$ git diff origin..master
776-------------------------------------------------
777
69f7ad73
BF
778will tell you whether the contents of the project are the same at the
779two branches; in theory, however, it's possible that the same project
780contents could have been arrived at by two different historical
d55ae921 781routes. You could compare the object names:
aec053bb
BF
782
783-------------------------------------------------
784$ git rev-list origin
785e05db0fd4f31dde7005f075a84f96b360d05984b
786$ git rev-list master
787e05db0fd4f31dde7005f075a84f96b360d05984b
788-------------------------------------------------
789
1249d8ad 790Or you could recall that the `...` operator selects all commits
ddd4ddef 791reachable from either one reference or the other but not
ddd2369c 792both; so
aec053bb
BF
793
794-------------------------------------------------
795$ git log origin...master
796-------------------------------------------------
797
798will return no commits when the two branches are equal.
799
e34caace 800[[finding-tagged-descendants]]
b181d57f
BF
801Find first tagged version including a given fix
802~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
aec053bb 803
69f7ad73
BF
804Suppose you know that the commit e05db0fd fixed a certain problem.
805You'd like to find the earliest tagged release that contains that
806fix.
807
808Of course, there may be more than one answer--if the history branched
809after commit e05db0fd, then there could be multiple "earliest" tagged
810releases.
811
812You could just visually inspect the commits since e05db0fd:
813
814-------------------------------------------------
815$ gitk e05db0fd..
816-------------------------------------------------
817
ddd4ddef 818or you can use linkgit:git-name-rev[1], which will give the commit a
b181d57f
BF
819name based on any tag it finds pointing to one of the commit's
820descendants:
821
822-------------------------------------------------
04483524 823$ git name-rev --tags e05db0fd
b181d57f
BF
824e05db0fd tags/v1.5.0-rc1^0~23
825-------------------------------------------------
826
5162e697 827The linkgit:git-describe[1] command does the opposite, naming the
b181d57f
BF
828revision using a tag on which the given commit is based:
829
830-------------------------------------------------
831$ git describe e05db0fd
04483524 832v1.5.0-rc0-260-ge05db0f
b181d57f
BF
833-------------------------------------------------
834
835but that may sometimes help you guess which tags might come after the
836given commit.
837
838If you just want to verify whether a given tagged version contains a
5162e697 839given commit, you could use linkgit:git-merge-base[1]:
b181d57f
BF
840
841-------------------------------------------------
842$ git merge-base e05db0fd v1.5.0-rc1
843e05db0fd4f31dde7005f075a84f96b360d05984b
844-------------------------------------------------
845
846The merge-base command finds a common ancestor of the given commits,
847and always returns one or the other in the case where one is a
848descendant of the other; so the above output shows that e05db0fd
849actually is an ancestor of v1.5.0-rc1.
850
851Alternatively, note that
852
853-------------------------------------------------
4a7979ca 854$ git log v1.5.0-rc1..e05db0fd
b181d57f
BF
855-------------------------------------------------
856
4a7979ca 857will produce empty output if and only if v1.5.0-rc1 includes e05db0fd,
b181d57f 858because it outputs only commits that are not reachable from v1.5.0-rc1.
aec053bb 859
5162e697 860As yet another alternative, the linkgit:git-show-branch[1] command lists
4a7979ca 861the commits reachable from its arguments with a display on the left-hand
ddd4ddef
TA
862side that indicates which arguments that commit is reachable from.
863So, if you run something like
4a7979ca
BF
864
865-------------------------------------------------
866$ git show-branch e05db0fd v1.5.0-rc0 v1.5.0-rc1 v1.5.0-rc2
867! [e05db0fd] Fix warnings in sha1_file.c - use C99 printf format if
868available
869 ! [v1.5.0-rc0] GIT v1.5.0 preview
870 ! [v1.5.0-rc1] GIT v1.5.0-rc1
871 ! [v1.5.0-rc2] GIT v1.5.0-rc2
872...
873-------------------------------------------------
874
ddd4ddef 875then a line like
4a7979ca
BF
876
877-------------------------------------------------
878+ ++ [e05db0fd] Fix warnings in sha1_file.c - use C99 printf format if
879available
880-------------------------------------------------
881
ddd4ddef
TA
882shows that e05db0fd is reachable from itself, from v1.5.0-rc1,
883and from v1.5.0-rc2, and not from v1.5.0-rc0.
4a7979ca 884
629d9f78
BF
885[[showing-commits-unique-to-a-branch]]
886Showing commits unique to a given branch
887~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4a7979ca 888
629d9f78 889Suppose you would like to see all the commits reachable from the branch
1249d8ad 890head named `master` but not from any other head in your repository.
d19fbc3c 891
629d9f78 892We can list all the heads in this repository with
5162e697 893linkgit:git-show-ref[1]:
d19fbc3c 894
629d9f78
BF
895-------------------------------------------------
896$ git show-ref --heads
897bf62196b5e363d73353a9dcf094c59595f3153b7 refs/heads/core-tutorial
898db768d5504c1bb46f63ee9d6e1772bd047e05bf9 refs/heads/maint
899a07157ac624b2524a059a3414e99f6f44bebc1e7 refs/heads/master
90024dbc180ea14dc1aebe09f14c8ecf32010690627 refs/heads/tutorial-2
9011e87486ae06626c2f31eaa63d26fc0fd646c8af2 refs/heads/tutorial-fixes
902-------------------------------------------------
d19fbc3c 903
1249d8ad 904We can get just the branch-head names, and remove `master`, with
629d9f78
BF
905the help of the standard utilities cut and grep:
906
907-------------------------------------------------
908$ git show-ref --heads | cut -d' ' -f2 | grep -v '^refs/heads/master'
909refs/heads/core-tutorial
910refs/heads/maint
911refs/heads/tutorial-2
912refs/heads/tutorial-fixes
913-------------------------------------------------
914
915And then we can ask to see all the commits reachable from master
916but not from these other heads:
917
918-------------------------------------------------
919$ gitk master --not $( git show-ref --heads | cut -d' ' -f2 |
920 grep -v '^refs/heads/master' )
921-------------------------------------------------
922
923Obviously, endless variations are possible; for example, to see all
924commits reachable from some head but not from any tag in the repository:
925
926-------------------------------------------------
c78974f7 927$ gitk $( git show-ref --heads ) --not $( git show-ref --tags )
629d9f78
BF
928-------------------------------------------------
929
9d83e382 930(See linkgit:gitrevisions[7] for explanations of commit-selecting
629d9f78
BF
931syntax such as `--not`.)
932
82c8bf28
BF
933[[making-a-release]]
934Creating a changelog and tarball for a software release
935~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
936
5162e697 937The linkgit:git-archive[1] command can create a tar or zip archive from
82c8bf28
BF
938any version of a project; for example:
939
940-------------------------------------------------
7ed1690c 941$ git archive -o latest.tar.gz --prefix=project/ HEAD
82c8bf28
BF
942-------------------------------------------------
943
7ed1690c
TK
944will use HEAD to produce a gzipped tar archive in which each filename
945is preceded by `project/`. The output file format is inferred from
946the output file extension if possible, see linkgit:git-archive[1] for
947details.
948
1249d8ad 949Versions of Git older than 1.7.7 don't know about the `tar.gz` format,
7ed1690c
TK
950you'll need to use gzip explicitly:
951
952-------------------------------------------------
953$ git archive --format=tar --prefix=project/ HEAD | gzip >latest.tar.gz
954-------------------------------------------------
82c8bf28
BF
955
956If you're releasing a new version of a software project, you may want
957to simultaneously make a changelog to include in the release
958announcement.
959
960Linus Torvalds, for example, makes new kernel releases by tagging them,
961then running:
962
963-------------------------------------------------
964$ release-script 2.6.12 2.6.13-rc6 2.6.13-rc7
965-------------------------------------------------
966
967where release-script is a shell script that looks like:
968
969-------------------------------------------------
970#!/bin/sh
971stable="$1"
972last="$2"
973new="$3"
974echo "# git tag v$new"
975echo "git archive --prefix=linux-$new/ v$new | gzip -9 > ../linux-$new.tar.gz"
976echo "git diff v$stable v$new | gzip -9 > ../patch-$new.gz"
977echo "git log --no-merges v$new ^v$last > ../ChangeLog-$new"
978echo "git shortlog --no-merges v$new ^v$last > ../ShortLog"
979echo "git diff --stat --summary -M v$last v$new > ../diffstat-$new"
980-------------------------------------------------
981
982and then he just cut-and-pastes the output commands after verifying that
983they look OK.
4a7979ca 984
e1ba4c32 985[[Finding-commits-With-given-Content]]
187b0d80 986Finding commits referencing a file with given content
d5821de2 987~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187b0d80
BF
988
989Somebody hands you a copy of a file, and asks which commits modified a
990file such that it contained the given content either before or after the
991commit. You can find out with this:
992
993-------------------------------------------------
477ff5b7 994$ git log --raw --abbrev=40 --pretty=oneline |
187b0d80
BF
995 grep -B 1 `git hash-object filename`
996-------------------------------------------------
997
998Figuring out why this works is left as an exercise to the (advanced)
5162e697
DM
999student. The linkgit:git-log[1], linkgit:git-diff-tree[1], and
1000linkgit:git-hash-object[1] man pages may prove helpful.
187b0d80 1001
aa971cb9 1002[[Developing-With-git]]
2de9b711 1003Developing with Git
d19fbc3c
BF
1004===================
1005
e34caace 1006[[telling-git-your-name]]
2de9b711 1007Telling Git your name
d19fbc3c
BF
1008---------------------
1009
632cc3e6
TK
1010Before creating any commits, you should introduce yourself to Git.
1011The easiest way to do so is to use linkgit:git-config[1]:
1012
1013------------------------------------------------
1014$ git config --global user.name 'Your Name Comes Here'
1015$ git config --global user.email 'you@yourdomain.example.com'
1016------------------------------------------------
1017
1018Which will add the following to a file named `.gitconfig` in your
1019home directory:
d19fbc3c
BF
1020
1021------------------------------------------------
d19fbc3c
BF
1022[user]
1023 name = Your Name Comes Here
1024 email = you@yourdomain.example.com
d19fbc3c
BF
1025------------------------------------------------
1026
632cc3e6
TK
1027See the "CONFIGURATION FILE" section of linkgit:git-config[1] for
1028details on the configuration file. The file is plain text, so you can
1029also edit it with your favorite editor.
fc90c536 1030
d19fbc3c 1031
e34caace 1032[[creating-a-new-repository]]
d19fbc3c
BF
1033Creating a new repository
1034-------------------------
1035
1036Creating a new repository from scratch is very easy:
1037
1038-------------------------------------------------
1039$ mkdir project
1040$ cd project
f1d2b477 1041$ git init
d19fbc3c
BF
1042-------------------------------------------------
1043
1044If you have some initial content (say, a tarball):
1045
1046-------------------------------------------------
0ddd93b2 1047$ tar xzvf project.tar.gz
d19fbc3c 1048$ cd project
f1d2b477 1049$ git init
d19fbc3c
BF
1050$ git add . # include everything below ./ in the first commit:
1051$ git commit
1052-------------------------------------------------
1053
1054[[how-to-make-a-commit]]
ae25c67a 1055How to make a commit
d19fbc3c
BF
1056--------------------
1057
1058Creating a new commit takes three steps:
1059
1060 1. Making some changes to the working directory using your
1061 favorite editor.
2de9b711
TA
1062 2. Telling Git about your changes.
1063 3. Creating the commit using the content you told Git about
d19fbc3c
BF
1064 in step 2.
1065
1066In practice, you can interleave and repeat steps 1 and 2 as many
1067times as you want: in order to keep track of what you want committed
2de9b711 1068at step 3, Git maintains a snapshot of the tree's contents in a
d19fbc3c
BF
1069special staging area called "the index."
1070
01997b4a 1071At the beginning, the content of the index will be identical to
1249d8ad 1072that of the HEAD. The command `git diff --cached`, which shows
01997b4a
BF
1073the difference between the HEAD and the index, should therefore
1074produce no output at that point.
eb6ae7f4 1075
d19fbc3c
BF
1076Modifying the index is easy:
1077
d39765b1 1078To update the index with the contents of a new or modified file, use
d19fbc3c
BF
1079
1080-------------------------------------------------
1081$ git add path/to/file
1082-------------------------------------------------
1083
d39765b1 1084To remove a file from the index and from the working tree, use
d19fbc3c
BF
1085
1086-------------------------------------------------
1087$ git rm path/to/file
1088-------------------------------------------------
1089
1090After each step you can verify that
1091
1092-------------------------------------------------
1093$ git diff --cached
1094-------------------------------------------------
1095
1096always shows the difference between the HEAD and the index file--this
1097is what you'd commit if you created the commit now--and that
1098
1099-------------------------------------------------
1100$ git diff
1101-------------------------------------------------
1102
1103shows the difference between the working tree and the index file.
1104
1249d8ad 1105Note that `git add` always adds just the current contents of a file
d19fbc3c 1106to the index; further changes to the same file will be ignored unless
6127c086 1107you run `git add` on the file again.
d19fbc3c
BF
1108
1109When you're ready, just run
1110
1111-------------------------------------------------
1112$ git commit
1113-------------------------------------------------
1114
2de9b711 1115and Git will prompt you for a commit message and then create the new
3dff5379 1116commit. Check to make sure it looks like what you expected with
d19fbc3c
BF
1117
1118-------------------------------------------------
1119$ git show
1120-------------------------------------------------
1121
1122As a special shortcut,
a6080a0a 1123
d19fbc3c
BF
1124-------------------------------------------------
1125$ git commit -a
1126-------------------------------------------------
1127
1128will update the index with any files that you've modified or removed
1129and create a commit, all in one step.
1130
1131A number of commands are useful for keeping track of what you're
1132about to commit:
1133
1134-------------------------------------------------
1135$ git diff --cached # difference between HEAD and the index; what
1130845b 1136 # would be committed if you ran "commit" now.
d19fbc3c
BF
1137$ git diff # difference between the index file and your
1138 # working directory; changes that would not
1139 # be included if you ran "commit" now.
c64415e2
BF
1140$ git diff HEAD # difference between HEAD and working tree; what
1141 # would be committed if you ran "commit -a" now.
d19fbc3c
BF
1142$ git status # a brief per-file summary of the above.
1143-------------------------------------------------
1144
5162e697 1145You can also use linkgit:git-gui[1] to create commits, view changes in
407c0c87
BF
1146the index and the working tree files, and individually select diff hunks
1147for inclusion in the index (by right-clicking on the diff hunk and
1148choosing "Stage Hunk For Commit").
1149
e34caace 1150[[creating-good-commit-messages]]
ae25c67a 1151Creating good commit messages
d19fbc3c
BF
1152-----------------------------
1153
1154Though not required, it's a good idea to begin the commit message
1155with a single short (less than 50 character) line summarizing the
1156change, followed by a blank line and then a more thorough
52ffe995
JW
1157description. The text up to the first blank line in a commit
1158message is treated as the commit title, and that title is used
2de9b711 1159throughout Git. For example, linkgit:git-format-patch[1] turns a
52ffe995
JW
1160commit into email, and it uses the title on the Subject line and the
1161rest of the commit in the body.
1162
d19fbc3c 1163
2dc53617
JH
1164[[ignoring-files]]
1165Ignoring files
1166--------------
1167
2de9b711 1168A project will often generate files that you do 'not' want to track with Git.
2dc53617 1169This typically includes files generated by a build process or temporary
2de9b711 1170backup files made by your editor. Of course, 'not' tracking files with Git
6127c086 1171is just a matter of 'not' calling `git add` on them. But it quickly becomes
2dc53617 1172annoying to have these untracked files lying around; e.g. they make
dcb11263
CJ
1173`git add .` practically useless, and they keep showing up in the output of
1174`git status`.
2dc53617 1175
1249d8ad
TK
1176You can tell Git to ignore certain files by creating a file called
1177`.gitignore` in the top level of your working directory, with contents
1178such as:
2dc53617
JH
1179
1180-------------------------------------------------
1181# Lines starting with '#' are considered comments.
464a8a7a 1182# Ignore any file named foo.txt.
2dc53617
JH
1183foo.txt
1184# Ignore (generated) html files,
1185*.html
1186# except foo.html which is maintained by hand.
1187!foo.html
1188# Ignore objects and archives.
1189*.[oa]
1190-------------------------------------------------
1191
5162e697 1192See linkgit:gitignore[5] for a detailed explanation of the syntax. You can
464a8a7a
BF
1193also place .gitignore files in other directories in your working tree, and they
1194will apply to those directories and their subdirectories. The `.gitignore`
1195files can be added to your repository like any other files (just run `git add
1196.gitignore` and `git commit`, as usual), which is convenient when the exclude
1197patterns (such as patterns matching build output files) would also make sense
1198for other users who clone your repository.
1199
1200If you wish the exclude patterns to affect only certain repositories
1201(instead of every repository for a given project), you may instead put
1249d8ad
TK
1202them in a file in your repository named `.git/info/exclude`, or in any
1203file specified by the `core.excludesfile` configuration variable.
1204Some Git commands can also take exclude patterns directly on the
1205command line. See linkgit:gitignore[5] for the details.
2dc53617 1206
e34caace 1207[[how-to-merge]]
ae25c67a 1208How to merge
d19fbc3c
BF
1209------------
1210
1211You can rejoin two diverging branches of development using
5162e697 1212linkgit:git-merge[1]:
d19fbc3c
BF
1213
1214-------------------------------------------------
1215$ git merge branchname
1216-------------------------------------------------
1217
1249d8ad 1218merges the development in the branch `branchname` into the current
e63ec003
MM
1219branch.
1220
1249d8ad 1221A merge is made by combining the changes made in `branchname` and the
e63ec003
MM
1222changes made up to the latest commit in your current branch since
1223their histories forked. The work tree is overwritten by the result of
1224the merge when this combining is done cleanly, or overwritten by a
1225half-merged results when this combining results in conflicts.
1226Therefore, if you have uncommitted changes touching the same files as
1227the ones impacted by the merge, Git will refuse to proceed. Most of
1228the time, you will want to commit your changes before you can merge,
1229and if you don't, then linkgit:git-stash[1] can take these changes
1230away while you're doing the merge, and reapply them afterwards.
1231
6a5d0b0a 1232If the changes are independent enough, Git will automatically complete
e63ec003
MM
1233the merge and commit the result (or reuse an existing commit in case
1234of <<fast-forwards,fast-forward>>, see below). On the other hand,
1235if there are conflicts--for example, if the same file is
d19fbc3c
BF
1236modified in two different ways in the remote branch and the local
1237branch--then you are warned; the output may look something like this:
1238
1239-------------------------------------------------
fabbd8f6
BF
1240$ git merge next
1241 100% (4/4) done
1242Auto-merged file.txt
d19fbc3c
BF
1243CONFLICT (content): Merge conflict in file.txt
1244Automatic merge failed; fix conflicts and then commit the result.
1245-------------------------------------------------
1246
1247Conflict markers are left in the problematic files, and after
1248you resolve the conflicts manually, you can update the index
2de9b711 1249with the contents and run Git commit, as you normally would when
d19fbc3c
BF
1250creating a new file.
1251
1252If you examine the resulting commit using gitk, you will see that it
1253has two parents, one pointing to the top of the current branch, and
1254one to the top of the other branch.
1255
d19fbc3c
BF
1256[[resolving-a-merge]]
1257Resolving a merge
1258-----------------
1259
2de9b711 1260When a merge isn't resolved automatically, Git leaves the index and
d19fbc3c
BF
1261the working tree in a special state that gives you all the
1262information you need to help resolve the merge.
1263
1264Files with conflicts are marked specially in the index, so until you
5162e697 1265resolve the problem and update the index, linkgit:git-commit[1] will
ef561ac7 1266fail:
d19fbc3c
BF
1267
1268-------------------------------------------------
1269$ git commit
1270file.txt: needs merge
1271-------------------------------------------------
1272
5162e697 1273Also, linkgit:git-status[1] will list those files as "unmerged", and the
ef561ac7
BF
1274files with conflicts will have conflict markers added, like this:
1275
1276-------------------------------------------------
1277<<<<<<< HEAD:file.txt
1278Hello world
1279=======
1280Goodbye
1281>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
1282-------------------------------------------------
1283
1284All you need to do is edit the files to resolve the conflicts, and then
1285
1286-------------------------------------------------
1287$ git add file.txt
1288$ git commit
1289-------------------------------------------------
1290
1291Note that the commit message will already be filled in for you with
1292some information about the merge. Normally you can just use this
1293default message unchanged, but you may add additional commentary of
1294your own if desired.
1295
2de9b711 1296The above is all you need to know to resolve a simple merge. But Git
ef561ac7
BF
1297also provides more information to help resolve conflicts:
1298
e34caace 1299[[conflict-resolution]]
ef561ac7
BF
1300Getting conflict-resolution help during a merge
1301~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
d19fbc3c 1302
2de9b711 1303All of the changes that Git was able to merge automatically are
5162e697 1304already added to the index file, so linkgit:git-diff[1] shows only
ef561ac7 1305the conflicts. It uses an unusual syntax:
d19fbc3c
BF
1306
1307-------------------------------------------------
1308$ git diff
1309diff --cc file.txt
1310index 802992c,2b60207..0000000
1311--- a/file.txt
1312+++ b/file.txt
1313@@@ -1,1 -1,1 +1,5 @@@
1314++<<<<<<< HEAD:file.txt
1315 +Hello world
1316++=======
1317+ Goodbye
1318++>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
1319-------------------------------------------------
1320
1130845b 1321Recall that the commit which will be committed after we resolve this
d19fbc3c
BF
1322conflict will have two parents instead of the usual one: one parent
1323will be HEAD, the tip of the current branch; the other will be the
1324tip of the other branch, which is stored temporarily in MERGE_HEAD.
1325
ef561ac7
BF
1326During the merge, the index holds three versions of each file. Each of
1327these three "file stages" represents a different version of the file:
1328
1329-------------------------------------------------
1330$ git show :1:file.txt # the file in a common ancestor of both branches
4209752d
JH
1331$ git show :2:file.txt # the version from HEAD.
1332$ git show :3:file.txt # the version from MERGE_HEAD.
ef561ac7
BF
1333-------------------------------------------------
1334
4209752d
JH
1335When you ask linkgit:git-diff[1] to show the conflicts, it runs a
1336three-way diff between the conflicted merge results in the work tree with
1337stages 2 and 3 to show only hunks whose contents come from both sides,
1338mixed (in other words, when a hunk's merge results come only from stage 2,
1339that part is not conflicting and is not shown. Same for stage 3).
ef561ac7
BF
1340
1341The diff above shows the differences between the working-tree version of
1342file.txt and the stage 2 and stage 3 versions. So instead of preceding
1249d8ad 1343each line by a single `+` or `-`, it now uses two columns: the first
ef561ac7
BF
1344column is used for differences between the first parent and the working
1345directory copy, and the second for differences between the second parent
1346and the working directory copy. (See the "COMBINED DIFF FORMAT" section
5162e697 1347of linkgit:git-diff-files[1] for a details of the format.)
ef561ac7
BF
1348
1349After resolving the conflict in the obvious way (but before updating the
1350index), the diff will look like:
d19fbc3c
BF
1351
1352-------------------------------------------------
1353$ git diff
1354diff --cc file.txt
1355index 802992c,2b60207..0000000
1356--- a/file.txt
1357+++ b/file.txt
1358@@@ -1,1 -1,1 +1,1 @@@
1359- Hello world
1360 -Goodbye
1361++Goodbye world
1362-------------------------------------------------
1363
1364This shows that our resolved version deleted "Hello world" from the
1365first parent, deleted "Goodbye" from the second parent, and added
1366"Goodbye world", which was previously absent from both.
1367
ef561ac7
BF
1368Some special diff options allow diffing the working directory against
1369any of these stages:
1370
1371-------------------------------------------------
1372$ git diff -1 file.txt # diff against stage 1
1373$ git diff --base file.txt # same as the above
1374$ git diff -2 file.txt # diff against stage 2
1375$ git diff --ours file.txt # same as the above
1376$ git diff -3 file.txt # diff against stage 3
1377$ git diff --theirs file.txt # same as the above.
1378-------------------------------------------------
1379
0cafe944 1380The linkgit:git-log[1] and linkgit:gitk[1] commands also provide special help
ef561ac7 1381for merges:
d19fbc3c
BF
1382
1383-------------------------------------------------
1384$ git log --merge
ef561ac7 1385$ gitk --merge
d19fbc3c
BF
1386-------------------------------------------------
1387
ef561ac7
BF
1388These will display all commits which exist only on HEAD or on
1389MERGE_HEAD, and which touch an unmerged file.
d19fbc3c 1390
5162e697 1391You may also use linkgit:git-mergetool[1], which lets you merge the
c7719fbe 1392unmerged files using external tools such as Emacs or kdiff3.
c64415e2 1393
ef561ac7 1394Each time you resolve the conflicts in a file and update the index:
d19fbc3c
BF
1395
1396-------------------------------------------------
1397$ git add file.txt
d19fbc3c
BF
1398-------------------------------------------------
1399
ef561ac7 1400the different stages of that file will be "collapsed", after which
6127c086 1401`git diff` will (by default) no longer show diffs for that file.
d19fbc3c
BF
1402
1403[[undoing-a-merge]]
ae25c67a 1404Undoing a merge
d19fbc3c
BF
1405---------------
1406
1407If you get stuck and decide to just give up and throw the whole mess
1408away, you can always return to the pre-merge state with
1409
1410-------------------------------------------------
1411$ git reset --hard HEAD
1412-------------------------------------------------
1413
1130845b 1414Or, if you've already committed the merge that you want to throw away,
d19fbc3c
BF
1415
1416-------------------------------------------------
1c73bb0e 1417$ git reset --hard ORIG_HEAD
d19fbc3c
BF
1418-------------------------------------------------
1419
1420However, this last command can be dangerous in some cases--never
1421throw away a commit you have already committed if that commit may
1422itself have been merged into another branch, as doing so may confuse
1423further merges.
1424
e34caace 1425[[fast-forwards]]
d19fbc3c
BF
1426Fast-forward merges
1427-------------------
1428
1429There is one special case not mentioned above, which is treated
1430differently. Normally, a merge results in a merge commit, with two
1431parents, one pointing at each of the two lines of development that
1432were merged.
1433
59723040 1434However, if the current branch is a descendant of the other--so every
2de9b711 1435commit present in the one is already contained in the other--then Git
a75d7b54 1436just performs a "fast-forward"; the head of the current branch is moved
59723040
BF
1437forward to point at the head of the merged-in branch, without any new
1438commits being created.
d19fbc3c 1439
e34caace 1440[[fixing-mistakes]]
b684f830
BF
1441Fixing mistakes
1442---------------
1443
1444If you've messed up the working tree, but haven't yet committed your
1445mistake, you can return the entire working tree to the last committed
1446state with
1447
1448-------------------------------------------------
1449$ git reset --hard HEAD
1450-------------------------------------------------
1451
1452If you make a commit that you later wish you hadn't, there are two
1453fundamentally different ways to fix the problem:
1454
1455 1. You can create a new commit that undoes whatever was done
93cbbd71 1456 by the old commit. This is the correct thing if your
b684f830
BF
1457 mistake has already been made public.
1458
1459 2. You can go back and modify the old commit. You should
1460 never do this if you have already made the history public;
2de9b711 1461 Git does not normally expect the "history" of a project to
b684f830
BF
1462 change, and cannot correctly perform repeated merges from
1463 a branch that has had its history changed.
1464
e34caace 1465[[reverting-a-commit]]
b684f830
BF
1466Fixing a mistake with a new commit
1467~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1468
1469Creating a new commit that reverts an earlier change is very easy;
5162e697 1470just pass the linkgit:git-revert[1] command a reference to the bad
b684f830
BF
1471commit; for example, to revert the most recent commit:
1472
1473-------------------------------------------------
1474$ git revert HEAD
1475-------------------------------------------------
1476
1477This will create a new commit which undoes the change in HEAD. You
1478will be given a chance to edit the commit message for the new commit.
1479
1480You can also revert an earlier change, for example, the next-to-last:
1481
1482-------------------------------------------------
1483$ git revert HEAD^
1484-------------------------------------------------
1485
2de9b711 1486In this case Git will attempt to undo the old change while leaving
b684f830
BF
1487intact any changes made since then. If more recent changes overlap
1488with the changes to be reverted, then you will be asked to fix
1489conflicts manually, just as in the case of <<resolving-a-merge,
1490resolving a merge>>.
1491
7cb192ea
BF
1492[[fixing-a-mistake-by-rewriting-history]]
1493Fixing a mistake by rewriting history
b684f830
BF
1494~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1495
1496If the problematic commit is the most recent commit, and you have not
1497yet made that commit public, then you may just
6127c086 1498<<undoing-a-merge,destroy it using `git reset`>>.
b684f830
BF
1499
1500Alternatively, you
1501can edit the working directory and update the index to fix your
1502mistake, just as if you were going to <<how-to-make-a-commit,create a
1503new commit>>, then run
1504
1505-------------------------------------------------
1506$ git commit --amend
1507-------------------------------------------------
1508
1509which will replace the old commit by a new commit incorporating your
1510changes, giving you a chance to edit the old commit message first.
1511
1512Again, you should never do this to a commit that may already have
5162e697 1513been merged into another branch; use linkgit:git-revert[1] instead in
b684f830
BF
1514that case.
1515
7cb192ea 1516It is also possible to replace commits further back in the history, but
b684f830
BF
1517this is an advanced topic to be left for
1518<<cleaning-up-history,another chapter>>.
1519
e34caace 1520[[checkout-of-path]]
b684f830
BF
1521Checking out an old version of a file
1522~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1523
1524In the process of undoing a previous bad change, you may find it
1525useful to check out an older version of a particular file using
6127c086 1526linkgit:git-checkout[1]. We've used `git checkout` before to switch
b684f830
BF
1527branches, but it has quite different behavior if it is given a path
1528name: the command
1529
1530-------------------------------------------------
1531$ git checkout HEAD^ path/to/file
1532-------------------------------------------------
1533
1534replaces path/to/file by the contents it had in the commit HEAD^, and
1535also updates the index to match. It does not change branches.
1536
1537If you just want to look at an old version of the file, without
1538modifying the working directory, you can do that with
5162e697 1539linkgit:git-show[1]:
b684f830
BF
1540
1541-------------------------------------------------
ed4eb0d8 1542$ git show HEAD^:path/to/file
b684f830
BF
1543-------------------------------------------------
1544
1545which will display the given version of the file.
1546
7a7cc594
JH
1547[[interrupted-work]]
1548Temporarily setting aside work in progress
1549~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1550
1551While you are in the middle of working on something complicated, you
1552find an unrelated but obvious and trivial bug. You would like to fix it
5162e697 1553before continuing. You can use linkgit:git-stash[1] to save the current
7a7cc594
JH
1554state of your work, and after fixing the bug (or, optionally after doing
1555so on a different branch and then coming back), unstash the
1556work-in-progress changes.
1557
1558------------------------------------------------
7a85f6ae 1559$ git stash save "work in progress for foo feature"
7a7cc594
JH
1560------------------------------------------------
1561
1562This command will save your changes away to the `stash`, and
1563reset your working tree and the index to match the tip of your
1564current branch. Then you can make your fix as usual.
1565
1566------------------------------------------------
1567... edit and test ...
1568$ git commit -a -m "blorpl: typofix"
1569------------------------------------------------
1570
1571After that, you can go back to what you were working on with
7b8988e1 1572`git stash pop`:
7a7cc594
JH
1573
1574------------------------------------------------
7b8988e1 1575$ git stash pop
7a7cc594
JH
1576------------------------------------------------
1577
1578
e34caace 1579[[ensuring-good-performance]]
d19fbc3c
BF
1580Ensuring good performance
1581-------------------------
1582
2de9b711 1583On large repositories, Git depends on compression to keep the history
901fd180 1584information from taking up too much space on disk or in memory. Some
e1ebf212 1585Git commands may automatically run linkgit:git-gc[1], so you don't
901fd180
TK
1586have to worry about running it manually. However, compressing a large
1587repository may take a while, so you may want to call `gc` explicitly
1588to avoid automatic compression kicking in when it is not convenient.
d19fbc3c 1589
e34caace
BF
1590
1591[[ensuring-reliability]]
11e016a3
BF
1592Ensuring reliability
1593--------------------
1594
e34caace 1595[[checking-for-corruption]]
11e016a3
BF
1596Checking the repository for corruption
1597~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1598
5162e697 1599The linkgit:git-fsck[1] command runs a number of self-consistency checks
1191ee18 1600on the repository, and reports on any problems. This may take some
c6a13b2c 1601time.
21dcb3b7
BF
1602
1603-------------------------------------------------
04e50e94 1604$ git fsck
21dcb3b7
BF
1605dangling commit 7281251ddd2a61e38657c827739c57015671a6b3
1606dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a63
1607dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b5
1608dangling blob 218761f9d90712d37a9c5e36f406f92202db07eb
1609dangling commit bf093535a34a4d35731aa2bd90fe6b176302f14f
1610dangling commit 8e4bec7f2ddaa268bef999853c25755452100f8e
1611dangling tree d50bb86186bf27b681d25af89d3b5b68382e4085
1612dangling tree b24c2473f1fd3d91352a624795be026d64c8841f
1613...
1614-------------------------------------------------
1615
c6a13b2c
JH
1616You will see informational messages on dangling objects. They are objects
1617that still exist in the repository but are no longer referenced by any of
1249d8ad 1618your branches, and can (and will) be removed after a while with `gc`.
b4ab1980 1619You can run `git fsck --no-dangling` to suppress these messages, and still
c6a13b2c 1620view real errors.
1cdade2c 1621
e34caace 1622[[recovering-lost-changes]]
11e016a3
BF
1623Recovering lost changes
1624~~~~~~~~~~~~~~~~~~~~~~~
1625
e34caace 1626[[reflogs]]
559e4d7a
BF
1627Reflogs
1628^^^^^^^
1629
1249d8ad
TK
1630Say you modify a branch with <<fixing-mistakes,`git reset --hard`>>,
1631and then realize that the branch was the only reference you had to
1632that point in history.
559e4d7a 1633
2de9b711 1634Fortunately, Git also keeps a log, called a "reflog", of all the
559e4d7a 1635previous values of each branch. So in this case you can still find the
a6080a0a 1636old history using, for example,
559e4d7a
BF
1637
1638-------------------------------------------------
1639$ git log master@{1}
1640-------------------------------------------------
1641
e502c2c3 1642This lists the commits reachable from the previous version of the
1249d8ad
TK
1643`master` branch head. This syntax can be used with any Git command
1644that accepts a commit, not just with `git log`. Some other examples:
559e4d7a
BF
1645
1646-------------------------------------------------
1647$ git show master@{2} # See where the branch pointed 2,
1648$ git show master@{3} # 3, ... changes ago.
1649$ gitk master@{yesterday} # See where it pointed yesterday,
1650$ gitk master@{"1 week ago"} # ... or last week
953f3d6f
BF
1651$ git log --walk-reflogs master # show reflog entries for master
1652-------------------------------------------------
1653
1654A separate reflog is kept for the HEAD, so
1655
1656-------------------------------------------------
1657$ git show HEAD@{"1 week ago"}
559e4d7a
BF
1658-------------------------------------------------
1659
953f3d6f
BF
1660will show what HEAD pointed to one week ago, not what the current branch
1661pointed to one week ago. This allows you to see the history of what
1662you've checked out.
1663
559e4d7a 1664The reflogs are kept by default for 30 days, after which they may be
5162e697 1665pruned. See linkgit:git-reflog[1] and linkgit:git-gc[1] to learn
559e4d7a 1666how to control this pruning, and see the "SPECIFYING REVISIONS"
9d83e382 1667section of linkgit:gitrevisions[7] for details.
559e4d7a 1668
2de9b711 1669Note that the reflog history is very different from normal Git history.
559e4d7a
BF
1670While normal history is shared by every repository that works on the
1671same project, the reflog history is not shared: it tells you only about
1672how the branches in your local repository have changed over time.
1673
59723040 1674[[dangling-object-recovery]]
559e4d7a
BF
1675Examining dangling objects
1676^^^^^^^^^^^^^^^^^^^^^^^^^^
1677
59723040
BF
1678In some situations the reflog may not be able to save you. For example,
1679suppose you delete a branch, then realize you need the history it
1680contained. The reflog is also deleted; however, if you have not yet
1681pruned the repository, then you may still be able to find the lost
6127c086 1682commits in the dangling objects that `git fsck` reports. See
59723040 1683<<dangling-objects>> for the details.
559e4d7a
BF
1684
1685-------------------------------------------------
1686$ git fsck
1687dangling commit 7281251ddd2a61e38657c827739c57015671a6b3
1688dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a63
1689dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b5
1690...
1691-------------------------------------------------
1692
aacd404e 1693You can examine
559e4d7a
BF
1694one of those dangling commits with, for example,
1695
1696------------------------------------------------
1697$ gitk 7281251ddd --not --all
1698------------------------------------------------
1699
1700which does what it sounds like: it says that you want to see the commit
1701history that is described by the dangling commit(s), but not the
1702history that is described by all your existing branches and tags. Thus
1703you get exactly the history reachable from that commit that is lost.
1704(And notice that it might not be just one commit: we only report the
1705"tip of the line" as being dangling, but there might be a whole deep
79c96c57 1706and complex commit history that was dropped.)
559e4d7a
BF
1707
1708If you decide you want the history back, you can always create a new
1709reference pointing to it, for example, a new branch:
1710
1711------------------------------------------------
a6080a0a 1712$ git branch recovered-branch 7281251ddd
559e4d7a
BF
1713------------------------------------------------
1714
59723040
BF
1715Other types of dangling objects (blobs and trees) are also possible, and
1716dangling objects can arise in other situations.
1717
11e016a3 1718
e34caace 1719[[sharing-development]]
d19fbc3c 1720Sharing development with others
b684f830 1721===============================
d19fbc3c 1722
aa971cb9 1723[[getting-updates-With-git-pull]]
6127c086 1724Getting updates with git pull
b684f830 1725-----------------------------
d19fbc3c 1726
e63ec003 1727After you clone a repository and commit a few changes of your own, you
d19fbc3c
BF
1728may wish to check the original repository for updates and merge them
1729into your own work.
1730
aa971cb9 1731We have already seen <<Updating-a-repository-With-git-fetch,how to
0e615b25 1732keep remote-tracking branches up to date>> with linkgit:git-fetch[1],
d19fbc3c
BF
1733and how to merge two branches. So you can merge in changes from the
1734original repository's master branch with:
1735
1736-------------------------------------------------
1737$ git fetch
1738$ git merge origin/master
1739-------------------------------------------------
1740
5162e697 1741However, the linkgit:git-pull[1] command provides a way to do this in
d19fbc3c
BF
1742one step:
1743
1744-------------------------------------------------
1745$ git pull origin master
1746-------------------------------------------------
1747
1249d8ad
TK
1748In fact, if you have `master` checked out, then this branch has been
1749configured by `git clone` to get changes from the HEAD branch of the
66a062a1 1750origin repository. So often you can
0eb4f7cd 1751accomplish the above with just a simple
d19fbc3c
BF
1752
1753-------------------------------------------------
1754$ git pull
1755-------------------------------------------------
1756
66a062a1
MM
1757This command will fetch changes from the remote branches to your
1758remote-tracking branches `origin/*`, and merge the default branch into
1759the current branch.
1760
29b9a66f
MM
1761More generally, a branch that is created from a remote-tracking branch
1762will pull
0eb4f7cd 1763by default from that branch. See the descriptions of the
1249d8ad 1764`branch.<name>.remote` and `branch.<name>.merge` options in
5162e697
DM
1765linkgit:git-config[1], and the discussion of the `--track` option in
1766linkgit:git-checkout[1], to learn how to control these defaults.
d19fbc3c 1767
1249d8ad 1768In addition to saving you keystrokes, `git pull` also helps you by
d19fbc3c
BF
1769producing a default commit message documenting the branch and
1770repository that you pulled from.
1771
1772(But note that no such commit will be created in the case of a
a75d7b54 1773<<fast-forwards,fast-forward>>; instead, your branch will just be
79c96c57 1774updated to point to the latest commit from the upstream branch.)
d19fbc3c 1775
1249d8ad 1776The `git pull` command can also be given `.` as the "remote" repository,
1191ee18 1777in which case it just merges in a branch from the current repository; so
4c63ff45
BF
1778the commands
1779
1780-------------------------------------------------
1781$ git pull . branch
1782$ git merge branch
1783-------------------------------------------------
1784
a7bdee11 1785are roughly equivalent.
4c63ff45 1786
e34caace 1787[[submitting-patches]]
d19fbc3c 1788Submitting patches to a project
b684f830 1789-------------------------------
d19fbc3c
BF
1790
1791If you just have a few changes, the simplest way to submit them may
1792just be to send them as patches in email:
1793
5162e697 1794First, use linkgit:git-format-patch[1]; for example:
d19fbc3c
BF
1795
1796-------------------------------------------------
eb6ae7f4 1797$ git format-patch origin
d19fbc3c
BF
1798-------------------------------------------------
1799
1800will produce a numbered series of files in the current directory, one
1249d8ad 1801for each patch in the current branch but not in `origin/HEAD`.
d19fbc3c 1802
d84cef18
PO
1803`git format-patch` can include an initial "cover letter". You can insert
1804commentary on individual patches after the three dash line which
1805`format-patch` places after the commit message but before the patch
1806itself. If you use `git notes` to track your cover letter material,
1807`git format-patch --notes` will include the commit's notes in a similar
1808manner.
1809
d19fbc3c
BF
1810You can then import these into your mail client and send them by
1811hand. However, if you have a lot to send at once, you may prefer to
5162e697 1812use the linkgit:git-send-email[1] script to automate the process.
3c735e07
JM
1813Consult the mailing list for your project first to determine
1814their requirements for submitting patches.
d19fbc3c 1815
e34caace 1816[[importing-patches]]
d19fbc3c 1817Importing patches to a project
b684f830 1818------------------------------
d19fbc3c 1819
5162e697 1820Git also provides a tool called linkgit:git-am[1] (am stands for
d19fbc3c
BF
1821"apply mailbox"), for importing such an emailed series of patches.
1822Just save all of the patch-containing messages, in order, into a
1249d8ad 1823single mailbox file, say `patches.mbox`, then run
d19fbc3c
BF
1824
1825-------------------------------------------------
eb6ae7f4 1826$ git am -3 patches.mbox
d19fbc3c
BF
1827-------------------------------------------------
1828
1829Git will apply each patch in order; if any conflicts are found, it
1830will stop, and you can fix the conflicts as described in
1249d8ad 1831"<<resolving-a-merge,Resolving a merge>>". (The `-3` option tells
2de9b711 1832Git to perform a merge; if you would prefer it just to abort and
01997b4a
BF
1833leave your tree and index untouched, you may omit that option.)
1834
1835Once the index is updated with the results of the conflict
1836resolution, instead of creating a new commit, just run
d19fbc3c
BF
1837
1838-------------------------------------------------
8ceb6fbd 1839$ git am --continue
d19fbc3c
BF
1840-------------------------------------------------
1841
2de9b711 1842and Git will create the commit for you and continue applying the
d19fbc3c
BF
1843remaining patches from the mailbox.
1844
1845The final result will be a series of commits, one for each patch in
1846the original mailbox, with authorship and commit log message each
1847taken from the message containing each patch.
1848
eda69449 1849[[public-repositories]]
2de9b711 1850Public Git repositories
eda69449 1851-----------------------
d19fbc3c 1852
6e30fb0c
DK
1853Another way to submit changes to a project is to tell the maintainer
1854of that project to pull the changes from your repository using
aa971cb9 1855linkgit:git-pull[1]. In the section "<<getting-updates-With-git-pull,
6127c086 1856Getting updates with `git pull`>>" we described this as a way to get
6e30fb0c
DK
1857updates from the "main" repository, but it works just as well in the
1858other direction.
d19fbc3c 1859
eda69449
BF
1860If you and the maintainer both have accounts on the same machine, then
1861you can just pull changes from each other's repositories directly;
11d51533 1862commands that accept repository URLs as arguments will also accept a
eda69449 1863local directory name:
d19fbc3c
BF
1864
1865-------------------------------------------------
1866$ git clone /path/to/repository
1867$ git pull /path/to/other/repository
1868-------------------------------------------------
1869
c9016158 1870or an ssh URL:
11d51533
BF
1871
1872-------------------------------------------------
1873$ git clone ssh://yourhost/~you/repository
1874-------------------------------------------------
1875
1876For projects with few developers, or for synchronizing a few private
1877repositories, this may be all you need.
1878
eda69449
BF
1879However, the more common way to do this is to maintain a separate public
1880repository (usually on a different host) for others to pull changes
1881from. This is usually more convenient, and allows you to cleanly
1882separate private work in progress from publicly visible work.
d19fbc3c
BF
1883
1884You will continue to do your day-to-day work in your personal
1885repository, but periodically "push" changes from your personal
1886repository into your public repository, allowing other developers to
1887pull from that repository. So the flow of changes, in a situation
1888where there is one other developer with a public repository, looks
1889like this:
1890
1891 you push
1892 your personal repo ------------------> your public repo
a6080a0a 1893 ^ |
d19fbc3c
BF
1894 | |
1895 | you pull | they pull
1896 | |
1897 | |
1898 | they push V
1899 their public repo <------------------- their repo
1900
11d51533
BF
1901We explain how to do this in the following sections.
1902
eda69449
BF
1903[[setting-up-a-public-repository]]
1904Setting up a public repository
1905~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1906
1249d8ad 1907Assume your personal repository is in the directory `~/proj`. We
6127c086 1908first create a new clone of the repository and tell `git daemon` that it
eda69449 1909is meant to be public:
d19fbc3c
BF
1910
1911-------------------------------------------------
52c80037 1912$ git clone --bare ~/proj proj.git
eda69449 1913$ touch proj.git/git-daemon-export-ok
d19fbc3c
BF
1914-------------------------------------------------
1915
52c80037 1916The resulting directory proj.git contains a "bare" git repository--it is
1249d8ad 1917just the contents of the `.git` directory, without any files checked out
eda69449 1918around it.
d19fbc3c 1919
1249d8ad 1920Next, copy `proj.git` to the server where you plan to host the
d19fbc3c
BF
1921public repository. You can use scp, rsync, or whatever is most
1922convenient.
1923
eda69449 1924[[exporting-via-git]]
2de9b711 1925Exporting a Git repository via the Git protocol
eda69449
BF
1926~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1927
1928This is the preferred method.
1929
1930If someone else administers the server, they should tell you what
1249d8ad
TK
1931directory to put the repository in, and what `git://` URL it will
1932appear at. You can then skip to the section
d19fbc3c
BF
1933"<<pushing-changes-to-a-public-repository,Pushing changes to a public
1934repository>>", below.
1935
5162e697 1936Otherwise, all you need to do is start linkgit:git-daemon[1]; it will
eda69449 1937listen on port 9418. By default, it will allow access to any directory
2de9b711 1938that looks like a Git directory and contains the magic file
6127c086 1939git-daemon-export-ok. Passing some directory paths as `git daemon`
eda69449
BF
1940arguments will further restrict the exports to those paths.
1941
6127c086 1942You can also run `git daemon` as an inetd service; see the
5162e697 1943linkgit:git-daemon[1] man page for details. (See especially the
eda69449 1944examples section.)
d19fbc3c
BF
1945
1946[[exporting-via-http]]
de3f2c7b 1947Exporting a git repository via HTTP
eda69449 1948~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
d19fbc3c 1949
2de9b711 1950The Git protocol gives better performance and reliability, but on a
de3f2c7b 1951host with a web server set up, HTTP exports may be simpler to set up.
d19fbc3c 1952
2de9b711 1953All you need to do is place the newly created bare Git repository in
d19fbc3c
BF
1954a directory that is exported by the web server, and make some
1955adjustments to give web clients some extra information they need:
1956
1957-------------------------------------------------
1958$ mv proj.git /home/you/public_html/proj.git
1959$ cd proj.git
c64415e2 1960$ git --bare update-server-info
7dce9918 1961$ mv hooks/post-update.sample hooks/post-update
d19fbc3c
BF
1962-------------------------------------------------
1963
1964(For an explanation of the last two lines, see
6998e4db 1965linkgit:git-update-server-info[1] and linkgit:githooks[5].)
d19fbc3c 1966
1249d8ad 1967Advertise the URL of `proj.git`. Anybody else should then be able to
c9016158 1968clone or pull from that URL, for example with a command line like:
d19fbc3c
BF
1969
1970-------------------------------------------------
1971$ git clone http://yourserver.com/~you/proj.git
1972-------------------------------------------------
1973
1974(See also
d5ff3b4b 1975link:howto/setup-git-server-over-http.html[setup-git-server-over-http]
d19fbc3c 1976for a slightly more sophisticated setup using WebDAV which also
de3f2c7b 1977allows pushing over HTTP.)
d19fbc3c 1978
d19fbc3c
BF
1979[[pushing-changes-to-a-public-repository]]
1980Pushing changes to a public repository
eda69449 1981~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
d19fbc3c 1982
eda69449 1983Note that the two techniques outlined above (exporting via
d19fbc3c
BF
1984<<exporting-via-http,http>> or <<exporting-via-git,git>>) allow other
1985maintainers to fetch your latest changes, but they do not allow write
1986access, which you will need to update the public repository with the
1987latest changes created in your private repository.
1988
5162e697 1989The simplest way to do this is using linkgit:git-push[1] and ssh; to
1249d8ad
TK
1990update the remote branch named `master` with the latest state of your
1991branch named `master`, run
d19fbc3c
BF
1992
1993-------------------------------------------------
1994$ git push ssh://yourserver.com/~you/proj.git master:master
1995-------------------------------------------------
1996
1997or just
1998
1999-------------------------------------------------
2000$ git push ssh://yourserver.com/~you/proj.git master
2001-------------------------------------------------
2002
6127c086 2003As with `git fetch`, `git push` will complain if this does not result in a
a75d7b54 2004<<fast-forwards,fast-forward>>; see the following section for details on
81eb417a 2005handling this case.
d19fbc3c 2006
1249d8ad 2007Note that the target of a `push` is normally a
11d51533 2008<<def_bare_repository,bare>> repository. You can also push to a
d9be2485
TK
2009repository that has a checked-out working tree, but a push to update the
2010currently checked-out branch is denied by default to prevent confusion.
50995edd 2011See the description of the receive.denyCurrentBranch option
d9be2485 2012in linkgit:git-config[1] for details.
11d51533 2013
6127c086 2014As with `git fetch`, you may also set up configuration options to
e9b49083
TK
2015save typing; so, for example:
2016
2017-------------------------------------------------
2018$ git remote add public-repo ssh://yourserver.com/~you/proj.git
2019-------------------------------------------------
2020
2021adds the following to `.git/config`:
d19fbc3c
BF
2022
2023-------------------------------------------------
d19fbc3c 2024[remote "public-repo"]
e9b49083
TK
2025 url = yourserver.com:proj.git
2026 fetch = +refs/heads/*:refs/remotes/example/*
d19fbc3c
BF
2027-------------------------------------------------
2028
e9b49083 2029which lets you do the same push with just
d19fbc3c
BF
2030
2031-------------------------------------------------
2032$ git push public-repo master
2033-------------------------------------------------
2034
1249d8ad
TK
2035See the explanations of the `remote.<name>.url`,
2036`branch.<name>.remote`, and `remote.<name>.push` options in
2037linkgit:git-config[1] for details.
d19fbc3c 2038
81eb417a
BF
2039[[forcing-push]]
2040What to do when a push fails
2041~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2042
a75d7b54 2043If a push would not result in a <<fast-forwards,fast-forward>> of the
81eb417a
BF
2044remote branch, then it will fail with an error like:
2045
2046-------------------------------------------------
2047error: remote 'refs/heads/master' is not an ancestor of
2048 local 'refs/heads/master'.
2049 Maybe you are not up-to-date and need to pull first?
2050error: failed to push to 'ssh://yourserver.com/~you/proj.git'
2051-------------------------------------------------
2052
2053This can happen, for example, if you:
2054
6127c086
FC
2055 - use `git reset --hard` to remove already-published commits, or
2056 - use `git commit --amend` to replace already-published commits
7cb192ea 2057 (as in <<fixing-a-mistake-by-rewriting-history>>), or
6127c086 2058 - use `git rebase` to rebase any already-published commits (as
81eb417a
BF
2059 in <<using-git-rebase>>).
2060
6127c086 2061You may force `git push` to perform the update anyway by preceding the
81eb417a
BF
2062branch name with a plus sign:
2063
2064-------------------------------------------------
2065$ git push ssh://yourserver.com/~you/proj.git +master
2066-------------------------------------------------
2067
d1471e06
TK
2068Note the addition of the `+` sign. Alternatively, you can use the
2069`-f` flag to force the remote update, as in:
2070
2071-------------------------------------------------
2072$ git push -f ssh://yourserver.com/~you/proj.git master
2073-------------------------------------------------
2074
81eb417a 2075Normally whenever a branch head in a public repository is modified, it
9e5d87d4 2076is modified to point to a descendant of the commit that it pointed to
81eb417a 2077before. By forcing a push in this situation, you break that convention.
aa971cb9 2078(See <<problems-With-rewriting-history>>.)
81eb417a
BF
2079
2080Nevertheless, this is a common practice for people that need a simple
2081way to publish a work-in-progress patch series, and it is an acceptable
2082compromise as long as you warn other developers that this is how you
2083intend to manage the branch.
2084
2085It's also possible for a push to fail in this way when other people have
2086the right to push to the same repository. In that case, the correct
843c81dc
EH
2087solution is to retry the push after first updating your work: either by a
2088pull, or by a fetch followed by a rebase; see the
81eb417a 2089<<setting-up-a-shared-repository,next section>> and
6998e4db 2090linkgit:gitcvs-migration[7] for more.
81eb417a 2091
e34caace 2092[[setting-up-a-shared-repository]]
d19fbc3c 2093Setting up a shared repository
eda69449 2094~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
d19fbc3c
BF
2095
2096Another way to collaborate is by using a model similar to that
2097commonly used in CVS, where several developers with special rights
2098all push to and pull from a single shared repository. See
6998e4db 2099linkgit:gitcvs-migration[7] for instructions on how to
d19fbc3c
BF
2100set this up.
2101
2de9b711 2102However, while there is nothing wrong with Git's support for shared
8fae2225 2103repositories, this mode of operation is not generally recommended,
2de9b711 2104simply because the mode of collaboration that Git supports--by
8fae2225
BF
2105exchanging patches and pulling from public repositories--has so many
2106advantages over the central shared repository:
2107
2108 - Git's ability to quickly import and merge patches allows a
2109 single maintainer to process incoming changes even at very
6127c086 2110 high rates. And when that becomes too much, `git pull` provides
8fae2225
BF
2111 an easy way for that maintainer to delegate this job to other
2112 maintainers while still allowing optional review of incoming
2113 changes.
2114 - Since every developer's repository has the same complete copy
2115 of the project history, no repository is special, and it is
2116 trivial for another developer to take over maintenance of a
2117 project, either by mutual agreement, or because a maintainer
2118 becomes unresponsive or difficult to work with.
2119 - The lack of a central group of "committers" means there is
2120 less need for formal decisions about who is "in" and who is
2121 "out".
2122
e34caace 2123[[setting-up-gitweb]]
eda69449
BF
2124Allowing web browsing of a repository
2125~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
d19fbc3c 2126
a8cd1402 2127The gitweb cgi script provides users an easy way to browse your
2de9b711
TA
2128project's files and history without having to install Git; see the file
2129gitweb/INSTALL in the Git source tree for instructions on setting it up.
d19fbc3c 2130
9cfde9ee
SS
2131[[how-to-get-a-git-repository-with-minimal-history]]
2132How to get a Git repository with minimal history
2133------------------------------------------------
2134
2135A <<def_shallow_clone,shallow clone>>, with its truncated
2136history, is useful when one is interested only in recent history
2137of a project and getting full history from the upstream is
2138expensive.
2139
2140A <<def_shallow_clone,shallow clone>> is created by specifying
2141the linkgit:git-clone[1] `--depth` switch. The depth can later be
2142changed with the linkgit:git-fetch[1] `--depth` switch, or full
2143history restored with `--unshallow`.
2144
2145Merging inside a <<def_shallow_clone,shallow clone>> will work as long
2146as a merge base is in the recent history.
2147Otherwise, it will be like merging unrelated histories and may
2148have to result in huge conflicts. This limitation may make such
2149a repository unsuitable to be used in merge based workflows.
2150
e34caace 2151[[sharing-development-examples]]
b684f830
BF
2152Examples
2153--------
d19fbc3c 2154
9e2163ea
BF
2155[[maintaining-topic-branches]]
2156Maintaining topic branches for a Linux subsystem maintainer
2157~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2158
2de9b711 2159This describes how Tony Luck uses Git in his role as maintainer of the
9e2163ea
BF
2160IA64 architecture for the Linux kernel.
2161
2162He uses two public branches:
2163
2164 - A "test" tree into which patches are initially placed so that they
2165 can get some exposure when integrated with other ongoing development.
2166 This tree is available to Andrew for pulling into -mm whenever he
2167 wants.
2168
2169 - A "release" tree into which tested patches are moved for final sanity
2170 checking, and as a vehicle to send them upstream to Linus (by sending
2171 him a "please pull" request.)
2172
2173He also uses a set of temporary branches ("topic branches"), each
2174containing a logical grouping of patches.
2175
2176To set this up, first create your work tree by cloning Linus's public
2177tree:
2178
2179-------------------------------------------------
283efb01 2180$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git work
9e2163ea
BF
2181$ cd work
2182-------------------------------------------------
2183
29b9a66f 2184Linus's tree will be stored in the remote-tracking branch named origin/master,
5162e697
DM
2185and can be updated using linkgit:git-fetch[1]; you can track other
2186public trees using linkgit:git-remote[1] to set up a "remote" and
2187linkgit:git-fetch[1] to keep them up-to-date; see
6e30fb0c 2188<<repositories-and-branches>>.
9e2163ea
BF
2189
2190Now create the branches in which you are going to work; these start out
2191at the current tip of origin/master branch, and should be set up (using
1249d8ad 2192the `--track` option to linkgit:git-branch[1]) to merge changes in from
9e2163ea
BF
2193Linus by default.
2194
2195-------------------------------------------------
2196$ git branch --track test origin/master
2197$ git branch --track release origin/master
2198-------------------------------------------------
2199
5162e697 2200These can be easily kept up to date using linkgit:git-pull[1].
9e2163ea
BF
2201
2202-------------------------------------------------
2203$ git checkout test && git pull
2204$ git checkout release && git pull
2205-------------------------------------------------
2206
2207Important note! If you have any local changes in these branches, then
2208this merge will create a commit object in the history (with no local
2de9b711 2209changes Git will simply do a "fast-forward" merge). Many people dislike
9e2163ea 2210the "noise" that this creates in the Linux history, so you should avoid
1249d8ad 2211doing this capriciously in the `release` branch, as these noisy commits
9e2163ea
BF
2212will become part of the permanent history when you ask Linus to pull
2213from the release branch.
2214
5162e697 2215A few configuration variables (see linkgit:git-config[1]) can
9e2163ea
BF
2216make it easy to push both branches to your public tree. (See
2217<<setting-up-a-public-repository>>.)
2218
2219-------------------------------------------------
2220$ cat >> .git/config <<EOF
2221[remote "mytree"]
283efb01 2222 url = master.kernel.org:/pub/scm/linux/kernel/git/aegl/linux.git
9e2163ea
BF
2223 push = release
2224 push = test
2225EOF
2226-------------------------------------------------
2227
2228Then you can push both the test and release trees using
5162e697 2229linkgit:git-push[1]:
9e2163ea
BF
2230
2231-------------------------------------------------
2232$ git push mytree
2233-------------------------------------------------
2234
2235or push just one of the test and release branches using:
2236
2237-------------------------------------------------
2238$ git push mytree test
2239-------------------------------------------------
2240
2241or
2242
2243-------------------------------------------------
2244$ git push mytree release
2245-------------------------------------------------
2246
2247Now to apply some patches from the community. Think of a short
2248snappy name for a branch to hold this patch (or related group of
352953a5
TL
2249patches), and create a new branch from a recent stable tag of
2250Linus's branch. Picking a stable base for your branch will:
22511) help you: by avoiding inclusion of unrelated and perhaps lightly
2252tested changes
1249d8ad 22532) help future bug hunters that use `git bisect` to find problems
9e2163ea
BF
2254
2255-------------------------------------------------
352953a5 2256$ git checkout -b speed-up-spinlocks v2.6.35
9e2163ea
BF
2257-------------------------------------------------
2258
2259Now you apply the patch(es), run some tests, and commit the change(s). If
2260the patch is a multi-part series, then you should apply each as a separate
2261commit to this branch.
2262
2263-------------------------------------------------
2264$ ... patch ... test ... commit [ ... patch ... test ... commit ]*
2265-------------------------------------------------
2266
a7bdee11 2267When you are happy with the state of this change, you can merge it into the
9e2163ea
BF
2268"test" branch in preparation to make it public:
2269
2270-------------------------------------------------
a7bdee11 2271$ git checkout test && git merge speed-up-spinlocks
9e2163ea
BF
2272-------------------------------------------------
2273
2274It is unlikely that you would have any conflicts here ... but you might if you
2275spent a while on this step and had also pulled new versions from upstream.
2276
3c735e07 2277Sometime later when enough time has passed and testing done, you can pull the
1249d8ad 2278same branch into the `release` tree ready to go upstream. This is where you
9e2163ea 2279see the value of keeping each patch (or patch series) in its own branch. It
1249d8ad 2280means that the patches can be moved into the `release` tree in any order.
9e2163ea
BF
2281
2282-------------------------------------------------
a7bdee11 2283$ git checkout release && git merge speed-up-spinlocks
9e2163ea
BF
2284-------------------------------------------------
2285
2286After a while, you will have a number of branches, and despite the
2287well chosen names you picked for each of them, you may forget what
2288they are for, or what status they are in. To get a reminder of what
2289changes are in a specific branch, use:
2290
2291-------------------------------------------------
467c0197 2292$ git log linux..branchname | git shortlog
9e2163ea
BF
2293-------------------------------------------------
2294
06ada152 2295To see whether it has already been merged into the test or release branches,
9e2163ea
BF
2296use:
2297
2298-------------------------------------------------
2299$ git log test..branchname
2300-------------------------------------------------
2301
2302or
2303
2304-------------------------------------------------
2305$ git log release..branchname
2306-------------------------------------------------
2307
06ada152 2308(If this branch has not yet been merged, you will see some log entries.
9e2163ea
BF
2309If it has been merged, then there will be no output.)
2310
2311Once a patch completes the great cycle (moving from test to release,
2312then pulled by Linus, and finally coming back into your local
1249d8ad 2313`origin/master` branch), the branch for this change is no longer needed.
9e2163ea
BF
2314You detect this when the output from:
2315
2316-------------------------------------------------
2317$ git log origin..branchname
2318-------------------------------------------------
2319
2320is empty. At this point the branch can be deleted:
2321
2322-------------------------------------------------
2323$ git branch -d branchname
2324-------------------------------------------------
2325
2326Some changes are so trivial that it is not necessary to create a separate
2327branch and then merge into each of the test and release branches. For
1249d8ad
TK
2328these changes, just apply directly to the `release` branch, and then
2329merge that into the `test` branch.
9e2163ea 2330
ae6ef554
TK
2331After pushing your work to `mytree`, you can use
2332linkgit:git-request-pull[1] to prepare a "please pull" request message
2333to send to Linus:
9e2163ea
BF
2334
2335-------------------------------------------------
ae6ef554
TK
2336$ git push mytree
2337$ git request-pull origin mytree release
9e2163ea
BF
2338-------------------------------------------------
2339
2340Here are some of the scripts that simplify all this even further.
2341
2342-------------------------------------------------
2343==== update script ====
48a8c26c 2344# Update a branch in my Git tree. If the branch to be updated
9e2163ea
BF
2345# is origin, then pull from kernel.org. Otherwise merge
2346# origin/master branch into test|release branch
2347
2348case "$1" in
2349test|release)
2350 git checkout $1 && git pull . origin
2351 ;;
2352origin)
fc74ecc1 2353 before=$(git rev-parse refs/remotes/origin/master)
9e2163ea 2354 git fetch origin
fc74ecc1 2355 after=$(git rev-parse refs/remotes/origin/master)
9e2163ea
BF
2356 if [ $before != $after ]
2357 then
2358 git log $before..$after | git shortlog
2359 fi
2360 ;;
2361*)
1a2ba8b9 2362 echo "usage: $0 origin|test|release" 1>&2
9e2163ea
BF
2363 exit 1
2364 ;;
2365esac
2366-------------------------------------------------
2367
2368-------------------------------------------------
2369==== merge script ====
2370# Merge a branch into either the test or release branch
2371
2372pname=$0
2373
2374usage()
2375{
1a2ba8b9 2376 echo "usage: $pname branch test|release" 1>&2
9e2163ea
BF
2377 exit 1
2378}
2379
fc74ecc1 2380git show-ref -q --verify -- refs/heads/"$1" || {
9e2163ea
BF
2381 echo "Can't see branch <$1>" 1>&2
2382 usage
fc74ecc1 2383}
9e2163ea
BF
2384
2385case "$2" in
2386test|release)
2387 if [ $(git log $2..$1 | wc -c) -eq 0 ]
2388 then
2389 echo $1 already merged into $2 1>&2
2390 exit 1
2391 fi
2392 git checkout $2 && git pull . $1
2393 ;;
2394*)
2395 usage
2396 ;;
2397esac
2398-------------------------------------------------
2399
2400-------------------------------------------------
2401==== status script ====
48a8c26c 2402# report on status of my ia64 Git tree
9e2163ea
BF
2403
2404gb=$(tput setab 2)
2405rb=$(tput setab 1)
2406restore=$(tput setab 9)
2407
2408if [ `git rev-list test..release | wc -c` -gt 0 ]
2409then
2410 echo $rb Warning: commits in release that are not in test $restore
2411 git log test..release
2412fi
2413
fc74ecc1 2414for branch in `git show-ref --heads | sed 's|^.*/||'`
9e2163ea
BF
2415do
2416 if [ $branch = test -o $branch = release ]
2417 then
2418 continue
2419 fi
2420
2421 echo -n $gb ======= $branch ====== $restore " "
2422 status=
2423 for ref in test release origin/master
2424 do
2425 if [ `git rev-list $ref..$branch | wc -c` -gt 0 ]
2426 then
2427 status=$status${ref:0:1}
2428 fi
2429 done
2430 case $status in
2431 trl)
2432 echo $rb Need to pull into test $restore
2433 ;;
2434 rl)
2435 echo "In test"
2436 ;;
2437 l)
2438 echo "Waiting for linus"
2439 ;;
2440 "")
2441 echo $rb All done $restore
2442 ;;
2443 *)
2444 echo $rb "<$status>" $restore
2445 ;;
2446 esac
2447 git log origin/master..$branch | git shortlog
2448done
2449-------------------------------------------------
d19fbc3c 2450
d19fbc3c 2451
d19fbc3c 2452[[cleaning-up-history]]
4c63ff45
BF
2453Rewriting history and maintaining patch series
2454==============================================
2455
2456Normally commits are only added to a project, never taken away or
2457replaced. Git is designed with this assumption, and violating it will
2de9b711 2458cause Git's merge machinery (for example) to do the wrong thing.
4c63ff45
BF
2459
2460However, there is a situation in which it can be useful to violate this
2461assumption.
2462
e34caace 2463[[patch-series]]
4c63ff45
BF
2464Creating the perfect patch series
2465---------------------------------
2466
2467Suppose you are a contributor to a large project, and you want to add a
2468complicated feature, and to present it to the other developers in a way
2469that makes it easy for them to read your changes, verify that they are
2470correct, and understand why you made each change.
2471
b181d57f 2472If you present all of your changes as a single patch (or commit), they
79c96c57 2473may find that it is too much to digest all at once.
4c63ff45
BF
2474
2475If you present them with the entire history of your work, complete with
2476mistakes, corrections, and dead ends, they may be overwhelmed.
2477
2478So the ideal is usually to produce a series of patches such that:
2479
2480 1. Each patch can be applied in order.
2481
2482 2. Each patch includes a single logical change, together with a
2483 message explaining the change.
2484
2485 3. No patch introduces a regression: after applying any initial
2486 part of the series, the resulting project still compiles and
2487 works, and has no bugs that it didn't have before.
2488
2489 4. The complete series produces the same end result as your own
2490 (probably much messier!) development process did.
2491
b181d57f
BF
2492We will introduce some tools that can help you do this, explain how to
2493use them, and then explain some of the problems that can arise because
2494you are rewriting history.
4c63ff45 2495
e34caace 2496[[using-git-rebase]]
6127c086 2497Keeping a patch series up to date using git rebase
4c63ff45
BF
2498--------------------------------------------------
2499
1249d8ad
TK
2500Suppose that you create a branch `mywork` on a remote-tracking branch
2501`origin`, and create some commits on top of it:
4c63ff45
BF
2502
2503-------------------------------------------------
2504$ git checkout -b mywork origin
2505$ vi file.txt
2506$ git commit
2507$ vi otherfile.txt
2508$ git commit
2509...
2510-------------------------------------------------
2511
2512You have performed no merges into mywork, so it is just a simple linear
1249d8ad 2513sequence of patches on top of `origin`:
4c63ff45 2514
1dc71a91 2515................................................
fa8347b8 2516 o--o--O <-- origin
4c63ff45 2517 \
fa8347b8 2518 a--b--c <-- mywork
1dc71a91 2519................................................
4c63ff45
BF
2520
2521Some more interesting work has been done in the upstream project, and
1249d8ad 2522`origin` has advanced:
4c63ff45 2523
1dc71a91 2524................................................
4c63ff45
BF
2525 o--o--O--o--o--o <-- origin
2526 \
2527 a--b--c <-- mywork
1dc71a91 2528................................................
4c63ff45 2529
1249d8ad 2530At this point, you could use `pull` to merge your changes back in;
4c63ff45
BF
2531the result would create a new merge commit, like this:
2532
1dc71a91 2533................................................
4c63ff45
BF
2534 o--o--O--o--o--o <-- origin
2535 \ \
2536 a--b--c--m <-- mywork
1dc71a91 2537................................................
a6080a0a 2538
4c63ff45
BF
2539However, if you prefer to keep the history in mywork a simple series of
2540commits without any merges, you may instead choose to use
5162e697 2541linkgit:git-rebase[1]:
4c63ff45
BF
2542
2543-------------------------------------------------
2544$ git checkout mywork
2545$ git rebase origin
2546-------------------------------------------------
2547
b181d57f 2548This will remove each of your commits from mywork, temporarily saving
1249d8ad 2549them as patches (in a directory named `.git/rebase-apply`), update mywork to
b181d57f
BF
2550point at the latest version of origin, then apply each of the saved
2551patches to the new mywork. The result will look like:
4c63ff45
BF
2552
2553
1dc71a91 2554................................................
4c63ff45
BF
2555 o--o--O--o--o--o <-- origin
2556 \
2557 a'--b'--c' <-- mywork
1dc71a91 2558................................................
4c63ff45 2559
b181d57f 2560In the process, it may discover conflicts. In that case it will stop
6127c086 2561and allow you to fix the conflicts; after fixing conflicts, use `git add`
7a7d4ef6 2562to update the index with those contents, and then, instead of
6127c086 2563running `git commit`, just run
4c63ff45
BF
2564
2565-------------------------------------------------
2566$ git rebase --continue
2567-------------------------------------------------
2568
2de9b711 2569and Git will continue applying the rest of the patches.
4c63ff45 2570
b6cbca38 2571At any point you may use the `--abort` option to abort this process and
4c63ff45
BF
2572return mywork to the state it had before you started the rebase:
2573
2574-------------------------------------------------
2575$ git rebase --abort
2576-------------------------------------------------
2577
6c26bf4d
TK
2578If you need to reorder or edit a number of commits in a branch, it may
2579be easier to use `git rebase -i`, which allows you to reorder and
2580squash commits, as well as marking them for individual editing during
2581the rebase. See <<interactive-rebase>> for details, and
2582<<reordering-patch-series>> for alternatives.
2583
7cb192ea
BF
2584[[rewriting-one-commit]]
2585Rewriting a single commit
365aa199
BF
2586-------------------------
2587
7cb192ea 2588We saw in <<fixing-a-mistake-by-rewriting-history>> that you can replace the
365aa199
BF
2589most recent commit using
2590
2591-------------------------------------------------
2592$ git commit --amend
2593-------------------------------------------------
2594
2595which will replace the old commit by a new commit incorporating your
2596changes, giving you a chance to edit the old commit message first.
6c26bf4d
TK
2597This is useful for fixing typos in your last commit, or for adjusting
2598the patch contents of a poorly staged commit.
365aa199 2599
6c26bf4d
TK
2600If you need to amend commits from deeper in your history, you can
2601use <<interactive-rebase,interactive rebase's `edit` instruction>>.
365aa199 2602
6c26bf4d
TK
2603[[reordering-patch-series]]
2604Reordering or selecting from a patch series
2605-------------------------------------------
365aa199 2606
6c26bf4d
TK
2607Sometimes you want to edit a commit deeper in your history. One
2608approach is to use `git format-patch` to create a series of patches
2609and then reset the state to before the patches:
365aa199
BF
2610
2611-------------------------------------------------
6c26bf4d
TK
2612$ git format-patch origin
2613$ git reset --hard origin
365aa199
BF
2614-------------------------------------------------
2615
6c26bf4d
TK
2616Then modify, reorder, or eliminate patches as needed before applying
2617them again with linkgit:git-am[1]:
365aa199
BF
2618
2619-------------------------------------------------
6c26bf4d 2620$ git am *.patch
365aa199
BF
2621-------------------------------------------------
2622
6c26bf4d
TK
2623[[interactive-rebase]]
2624Using interactive rebases
2625-------------------------
365aa199 2626
6c26bf4d
TK
2627You can also edit a patch series with an interactive rebase. This is
2628the same as <<reordering-patch-series,reordering a patch series using
2629`format-patch`>>, so use whichever interface you like best.
4c63ff45 2630
6c26bf4d
TK
2631Rebase your current HEAD on the last commit you want to retain as-is.
2632For example, if you want to reorder the last 5 commits, use:
b181d57f
BF
2633
2634-------------------------------------------------
6c26bf4d 2635$ git rebase -i HEAD~5
b181d57f
BF
2636-------------------------------------------------
2637
6c26bf4d
TK
2638This will open your editor with a list of steps to be taken to perform
2639your rebase.
4c63ff45 2640
b181d57f 2641-------------------------------------------------
6c26bf4d
TK
2642pick deadbee The oneline of this commit
2643pick fa1afe1 The oneline of the next commit
2644...
4c63ff45 2645
6c26bf4d
TK
2646# Rebase c0ffeee..deadbee onto c0ffeee
2647#
2648# Commands:
2649# p, pick = use commit
2650# r, reword = use commit, but edit the commit message
2651# e, edit = use commit, but stop for amending
2652# s, squash = use commit, but meld into previous commit
2653# f, fixup = like "squash", but discard this commit's log message
2654# x, exec = run command (the rest of the line) using shell
2655#
2656# These lines can be re-ordered; they are executed from top to bottom.
2657#
2658# If you remove a line here THAT COMMIT WILL BE LOST.
2659#
2660# However, if you remove everything, the rebase will be aborted.
2661#
2662# Note that empty commits are commented out
2663-------------------------------------------------
2664
2665As explained in the comments, you can reorder commits, squash them
2666together, edit commit messages, etc. by editing the list. Once you
2667are satisfied, save the list and close your editor, and the rebase
2668will begin.
2669
2670The rebase will stop where `pick` has been replaced with `edit` or
2671when a step in the list fails to mechanically resolve conflicts and
2672needs your help. When you are done editing and/or resolving conflicts
2673you can continue with `git rebase --continue`. If you decide that
2674things are getting too hairy, you can always bail out with `git rebase
2675--abort`. Even after the rebase is complete, you can still recover
2676the original branch by using the <<reflogs,reflog>>.
2677
2678For a more detailed discussion of the procedure and additional tips,
2679see the "INTERACTIVE MODE" section of linkgit:git-rebase[1].
4c63ff45 2680
e34caace 2681[[patch-series-tools]]
4c63ff45
BF
2682Other tools
2683-----------
2684
73a1d050 2685There are numerous other tools, such as StGit, which exist for the
79c96c57 2686purpose of maintaining a patch series. These are outside of the scope of
b181d57f 2687this manual.
4c63ff45 2688
aa971cb9 2689[[problems-With-rewriting-history]]
4c63ff45
BF
2690Problems with rewriting history
2691-------------------------------
2692
b181d57f
BF
2693The primary problem with rewriting the history of a branch has to do
2694with merging. Suppose somebody fetches your branch and merges it into
2695their branch, with a result something like this:
2696
1dc71a91 2697................................................
b181d57f
BF
2698 o--o--O--o--o--o <-- origin
2699 \ \
2700 t--t--t--m <-- their branch:
1dc71a91 2701................................................
b181d57f
BF
2702
2703Then suppose you modify the last three commits:
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
1dc71a91 2709................................................
b181d57f
BF
2710
2711If we examined all this history together in one repository, it will
2712look like:
2713
1dc71a91 2714................................................
b181d57f
BF
2715 o--o--o <-- new head of origin
2716 /
2717 o--o--O--o--o--o <-- old head of origin
2718 \ \
2719 t--t--t--m <-- their branch:
1dc71a91 2720................................................
b181d57f
BF
2721
2722Git has no way of knowing that the new head is an updated version of
2723the old head; it treats this situation exactly the same as it would if
2724two developers had independently done the work on the old and new heads
2725in parallel. At this point, if someone attempts to merge the new head
2de9b711 2726in to their branch, Git will attempt to merge together the two (old and
b181d57f
BF
2727new) lines of development, instead of trying to replace the old by the
2728new. The results are likely to be unexpected.
2729
2730You may still choose to publish branches whose history is rewritten,
2731and it may be useful for others to be able to fetch those branches in
2732order to examine or test them, but they should not attempt to pull such
2733branches into their own work.
2734
2735For true distributed development that supports proper merging,
2736published branches should never be rewritten.
2737
3fb00282
SP
2738[[bisect-merges]]
2739Why bisecting merge commits can be harder than bisecting linear history
2740-----------------------------------------------------------------------
2741
5162e697 2742The linkgit:git-bisect[1] command correctly handles history that
3fb00282
SP
2743includes merge commits. However, when the commit that it finds is a
2744merge commit, the user may need to work harder than usual to figure out
2745why that commit introduced a problem.
2746
2747Imagine this history:
2748
2749................................................
2750 ---Z---o---X---...---o---A---C---D
2751 \ /
2752 o---o---Y---...---o---B
2753................................................
2754
2755Suppose that on the upper line of development, the meaning of one
2756of the functions that exists at Z is changed at commit X. The
2757commits from Z leading to A change both the function's
2758implementation and all calling sites that exist at Z, as well
2759as new calling sites they add, to be consistent. There is no
2760bug at A.
2761
2762Suppose that in the meantime on the lower line of development somebody
2763adds a new calling site for that function at commit Y. The
2764commits from Z leading to B all assume the old semantics of that
2765function and the callers and the callee are consistent with each
2766other. There is no bug at B, either.
2767
2768Suppose further that the two development lines merge cleanly at C,
2769so no conflict resolution is required.
2770
2771Nevertheless, the code at C is broken, because the callers added
2772on the lower line of development have not been converted to the new
2773semantics introduced on the upper line of development. So if all
2774you know is that D is bad, that Z is good, and that
5162e697 2775linkgit:git-bisect[1] identifies C as the culprit, how will you
3fb00282
SP
2776figure out that the problem is due to this change in semantics?
2777
6127c086 2778When the result of a `git bisect` is a non-merge commit, you should
3fb00282
SP
2779normally be able to discover the problem by examining just that commit.
2780Developers can make this easy by breaking their changes into small
2781self-contained commits. That won't help in the case above, however,
2782because the problem isn't obvious from examination of any single
2783commit; instead, a global view of the development is required. To
2784make matters worse, the change in semantics in the problematic
2785function may be just one small part of the changes in the upper
2786line of development.
2787
2788On the other hand, if instead of merging at C you had rebased the
2789history between Z to B on top of A, you would have gotten this
2790linear history:
2791
2792................................................................
2793 ---Z---o---X--...---o---A---o---o---Y*--...---o---B*--D*
2794................................................................
2795
2796Bisecting between Z and D* would hit a single culprit commit Y*,
2797and understanding why Y* was broken would probably be easier.
2798
2de9b711 2799Partly for this reason, many experienced Git users, even when
3fb00282
SP
2800working on an otherwise merge-heavy project, keep the history
2801linear by rebasing against the latest upstream version before
2802publishing.
2803
e34caace 2804[[advanced-branch-management]]
b181d57f
BF
2805Advanced branch management
2806==========================
4c63ff45 2807
e34caace 2808[[fetching-individual-branches]]
b181d57f
BF
2809Fetching individual branches
2810----------------------------
2811
5162e697 2812Instead of using linkgit:git-remote[1], you can also choose just
b181d57f
BF
2813to update one branch at a time, and to store it locally under an
2814arbitrary name:
2815
2816-------------------------------------------------
2817$ git fetch origin todo:my-todo-work
2818-------------------------------------------------
2819
1249d8ad 2820The first argument, `origin`, just tells Git to fetch from the
2de9b711 2821repository you originally cloned from. The second argument tells Git
1249d8ad
TK
2822to fetch the branch named `todo` from the remote repository, and to
2823store it locally under the name `refs/heads/my-todo-work`.
b181d57f
BF
2824
2825You can also fetch branches from other repositories; so
2826
2827-------------------------------------------------
2828$ git fetch git://example.com/proj.git master:example-master
2829-------------------------------------------------
2830
1249d8ad
TK
2831will create a new branch named `example-master` and store in it the
2832branch named `master` from the repository at the given URL. If you
b181d57f 2833already have a branch named example-master, it will attempt to
59723040
BF
2834<<fast-forwards,fast-forward>> to the commit given by example.com's
2835master branch. In more detail:
b181d57f 2836
59723040
BF
2837[[fetch-fast-forwards]]
2838git fetch and fast-forwards
2839---------------------------
b181d57f 2840
1249d8ad 2841In the previous example, when updating an existing branch, `git fetch`
7a7d4ef6 2842checks to make sure that the most recent commit on the remote
b181d57f
BF
2843branch is a descendant of the most recent commit on your copy of the
2844branch before updating your copy of the branch to point at the new
a75d7b54 2845commit. Git calls this process a <<fast-forwards,fast-forward>>.
b181d57f 2846
a75d7b54 2847A fast-forward looks something like this:
b181d57f 2848
1dc71a91 2849................................................
b181d57f
BF
2850 o--o--o--o <-- old head of the branch
2851 \
2852 o--o--o <-- new head of the branch
1dc71a91 2853................................................
b181d57f
BF
2854
2855
2856In some cases it is possible that the new head will *not* actually be
2857a descendant of the old head. For example, the developer may have
2858realized she made a serious mistake, and decided to backtrack,
2859resulting in a situation like:
2860
1dc71a91 2861................................................
b181d57f
BF
2862 o--o--o--o--a--b <-- old head of the branch
2863 \
2864 o--o--o <-- new head of the branch
1dc71a91 2865................................................
b181d57f 2866
1249d8ad 2867In this case, `git fetch` will fail, and print out a warning.
b181d57f 2868
2de9b711 2869In that case, you can still force Git to update to the new head, as
b181d57f 2870described in the following section. However, note that in the
1249d8ad 2871situation above this may mean losing the commits labeled `a` and `b`,
b181d57f
BF
2872unless you've already created a reference of your own pointing to
2873them.
2874
e34caace 2875[[forcing-fetch]]
6127c086 2876Forcing git fetch to do non-fast-forward updates
b181d57f
BF
2877------------------------------------------------
2878
2879If git fetch fails because the new head of a branch is not a
2880descendant of the old head, you may force the update with:
2881
2882-------------------------------------------------
2883$ git fetch git://example.com/proj.git +master:refs/remotes/example/master
2884-------------------------------------------------
2885
1249d8ad 2886Note the addition of the `+` sign. Alternatively, you can use the `-f`
c64415e2
BF
2887flag to force updates of all the fetched branches, as in:
2888
2889-------------------------------------------------
2890$ git fetch -f origin
2891-------------------------------------------------
2892
2893Be aware that commits that the old version of example/master pointed at
2894may be lost, as we saw in the previous section.
b181d57f 2895
e34caace 2896[[remote-branch-configuration]]
29b9a66f
MM
2897Configuring remote-tracking branches
2898------------------------------------
b181d57f 2899
1249d8ad 2900We saw above that `origin` is just a shortcut to refer to the
79c96c57 2901repository that you originally cloned from. This information is
2de9b711 2902stored in Git configuration variables, which you can see using
5162e697 2903linkgit:git-config[1]:
b181d57f
BF
2904
2905-------------------------------------------------
9d13bda3 2906$ git config -l
b181d57f
BF
2907core.repositoryformatversion=0
2908core.filemode=true
2909core.logallrefupdates=true
2910remote.origin.url=git://git.kernel.org/pub/scm/git/git.git
2911remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
2912branch.master.remote=origin
2913branch.master.merge=refs/heads/master
2914-------------------------------------------------
2915
2916If there are other repositories that you also use frequently, you can
2917create similar configuration options to save typing; for example,
b181d57f
BF
2918
2919-------------------------------------------------
47adb8ac 2920$ git remote add example git://example.com/proj.git
b181d57f
BF
2921-------------------------------------------------
2922
47adb8ac 2923adds the following to `.git/config`:
b181d57f
BF
2924
2925-------------------------------------------------
47adb8ac
TK
2926[remote "example"]
2927 url = git://example.com/proj.git
2928 fetch = +refs/heads/*:refs/remotes/example/*
b181d57f
BF
2929-------------------------------------------------
2930
47adb8ac
TK
2931Also note that the above configuration can be performed by directly
2932editing the file `.git/config` instead of using linkgit:git-remote[1].
b181d57f 2933
47adb8ac
TK
2934After configuring the remote, the following three commands will do the
2935same thing:
b181d57f
BF
2936
2937-------------------------------------------------
47adb8ac
TK
2938$ git fetch git://example.com/proj.git +refs/heads/*:refs/remotes/example/*
2939$ git fetch example +refs/heads/*:refs/remotes/example/*
b181d57f
BF
2940$ git fetch example
2941-------------------------------------------------
2942
5162e697 2943See linkgit:git-config[1] for more details on the configuration
47adb8ac
TK
2944options mentioned above and linkgit:git-fetch[1] for more details on
2945the refspec syntax.
d19fbc3c 2946
d19fbc3c 2947
036f8199
BF
2948[[git-concepts]]
2949Git concepts
2950============
d19fbc3c 2951
036f8199
BF
2952Git is built on a small number of simple but powerful ideas. While it
2953is possible to get things done without understanding them, you will find
2de9b711 2954Git much more intuitive if you do.
036f8199
BF
2955
2956We start with the most important, the <<def_object_database,object
2957database>> and the <<def_index,index>>.
b181d57f 2958
e34caace 2959[[the-object-database]]
b181d57f
BF
2960The Object Database
2961-------------------
2962
1bbf1c79
BF
2963
2964We already saw in <<understanding-commits>> that all commits are stored
2965under a 40-digit "object name". In fact, all the information needed to
2966represent the history of a project is stored in objects with such names.
a6e5ef7d
FC
2967In each case the name is calculated by taking the SHA-1 hash of the
2968contents of the object. The SHA-1 hash is a cryptographic hash function.
1bbf1c79
BF
2969What that means to us is that it is impossible to find two different
2970objects with the same name. This has a number of advantages; among
2971others:
2972
2973- Git can quickly determine whether two objects are identical or not,
2974 just by comparing names.
06ada152 2975- Since object names are computed the same way in every repository, the
1bbf1c79
BF
2976 same content stored in two repositories will always be stored under
2977 the same name.
2978- Git can detect errors when it reads an object, by checking that the
a6e5ef7d 2979 object's name is still the SHA-1 hash of its contents.
1bbf1c79
BF
2980
2981(See <<object-details>> for the details of the object formatting and
a6e5ef7d 2982SHA-1 calculation.)
1bbf1c79
BF
2983
2984There are four different types of objects: "blob", "tree", "commit", and
2985"tag".
2986
2987- A <<def_blob_object,"blob" object>> is used to store file data.
843c81dc 2988- A <<def_tree_object,"tree" object>> ties one or more
1bbf1c79
BF
2989 "blob" objects into a directory structure. In addition, a tree object
2990 can refer to other tree objects, thus creating a directory hierarchy.
2991- A <<def_commit_object,"commit" object>> ties such directory hierarchies
2ef8ac1b 2992 together into a <<def_DAG,directed acyclic graph>> of revisions--each
1bbf1c79
BF
2993 commit contains the object name of exactly one tree designating the
2994 directory hierarchy at the time of the commit. In addition, a commit
2995 refers to "parent" commit objects that describe the history of how we
2996 arrived at that directory hierarchy.
2997- A <<def_tag_object,"tag" object>> symbolically identifies and can be
2998 used to sign other objects. It contains the object name and type of
2999 another object, a symbolic name (of course!) and, optionally, a
3000 signature.
b181d57f 3001
b181d57f
BF
3002The object types in some more detail:
3003
513d419c
BF
3004[[commit-object]]
3005Commit Object
3006~~~~~~~~~~~~~
b181d57f 3007
1bbf1c79 3008The "commit" object links a physical state of a tree with a description
1249d8ad 3009of how we got there and why. Use the `--pretty=raw` option to
5162e697 3010linkgit:git-show[1] or linkgit:git-log[1] to examine your favorite
1bbf1c79
BF
3011commit:
3012
3013------------------------------------------------
3014$ git show -s --pretty=raw 2be7fcb476
3015commit 2be7fcb4764f2dbcee52635b91fedb1b3dcf7ab4
3016tree fb3a8bdd0ceddd019615af4d57a53f43d8cee2bf
3017parent 257a84d9d02e90447b149af58b271c19405edb6a
3018author Dave Watson <dwatson@mimvista.com> 1187576872 -0400
3019committer Junio C Hamano <gitster@pobox.com> 1187591163 -0700
3020
3021 Fix misspelling of 'suppress' in docs
3022
3023 Signed-off-by: Junio C Hamano <gitster@pobox.com>
3024------------------------------------------------
3025
3026As you can see, a commit is defined by:
3027
a6e5ef7d 3028- a tree: The SHA-1 name of a tree object (as defined below), representing
1bbf1c79 3029 the contents of a directory at a certain point in time.
edfbbf7e 3030- parent(s): The SHA-1 name(s) of some number of commits which represent the
9e5d87d4 3031 immediately previous step(s) in the history of the project. The
1bbf1c79
BF
3032 example above has one parent; merge commits may have more than
3033 one. A commit with no parents is called a "root" commit, and
3034 represents the initial revision of a project. Each project must have
3035 at least one root. A project can also have multiple roots, though
3036 that isn't common (or necessarily a good idea).
3037- an author: The name of the person responsible for this change, together
3038 with its date.
3039- a committer: The name of the person who actually created the commit,
3040 with the date it was done. This may be different from the author, for
3041 example, if the author was someone who wrote a patch and emailed it
3042 to the person who used it to create the commit.
3043- a comment describing this commit.
3044
3045Note that a commit does not itself contain any information about what
3046actually changed; all changes are calculated by comparing the contents
3047of the tree referred to by this commit with the trees associated with
2de9b711 3048its parents. In particular, Git does not attempt to record file renames
1bbf1c79
BF
3049explicitly, though it can identify cases where the existence of the same
3050file data at changing paths suggests a rename. (See, for example, the
1249d8ad 3051`-M` option to linkgit:git-diff[1]).
1bbf1c79 3052
5162e697 3053A commit is usually created by linkgit:git-commit[1], which creates a
1bbf1c79
BF
3054commit whose parent is normally the current HEAD, and whose tree is
3055taken from the content currently stored in the index.
b181d57f 3056
e34caace 3057[[tree-object]]
b181d57f 3058Tree Object
971aa71f 3059~~~~~~~~~~~
b181d57f 3060
5162e697
DM
3061The ever-versatile linkgit:git-show[1] command can also be used to
3062examine tree objects, but linkgit:git-ls-tree[1] will give you more
1bbf1c79
BF
3063details:
3064
3065------------------------------------------------
3066$ git ls-tree fb3a8bdd0ce
3067100644 blob 63c918c667fa005ff12ad89437f2fdc80926e21c .gitignore
3068100644 blob 5529b198e8d14decbe4ad99db3f7fb632de0439d .mailmap
3069100644 blob 6ff87c4664981e4397625791c8ea3bbb5f2279a3 COPYING
3070040000 tree 2fb783e477100ce076f6bf57e4a6f026013dc745 Documentation
3071100755 blob 3c0032cec592a765692234f1cba47dfdcc3a9200 GIT-VERSION-GEN
3072100644 blob 289b046a443c0647624607d471289b2c7dcd470b INSTALL
3073100644 blob 4eb463797adc693dc168b926b6932ff53f17d0b1 Makefile
3074100644 blob 548142c327a6790ff8821d67c2ee1eff7a656b52 README
3075...
3076------------------------------------------------
3077
3078As you can see, a tree object contains a list of entries, each with a
a6e5ef7d 3079mode, object type, SHA-1 name, and name, sorted by name. It represents
1bbf1c79
BF
3080the contents of a single directory tree.
3081
3082The object type may be a blob, representing the contents of a file, or
3083another tree, representing the contents of a subdirectory. Since trees
a6e5ef7d
FC
3084and blobs, like all other objects, are named by the SHA-1 hash of their
3085contents, two trees have the same SHA-1 name if and only if their
1bbf1c79 3086contents (including, recursively, the contents of all subdirectories)
2de9b711 3087are identical. This allows Git to quickly determine the differences
1bbf1c79
BF
3088between two related tree objects, since it can ignore any entries with
3089identical object names.
3090
3091(Note: in the presence of submodules, trees may also have commits as
6dd14366 3092entries. See <<submodules>> for documentation.)
1bbf1c79 3093
2de9b711 3094Note that the files all have mode 644 or 755: Git actually only pays
1bbf1c79 3095attention to the executable bit.
b181d57f 3096
513d419c
BF
3097[[blob-object]]
3098Blob Object
3099~~~~~~~~~~~
b181d57f 3100
5162e697 3101You can use linkgit:git-show[1] to examine the contents of a blob; take,
1249d8ad 3102for example, the blob in the entry for `COPYING` from the tree above:
b181d57f 3103
1bbf1c79
BF
3104------------------------------------------------
3105$ git show 6ff87c4664
3106
3107 Note that the only valid version of the GPL as far as this project
3108 is concerned is _this_ particular version of the license (ie v2, not
3109 v2.2 or v3.x or whatever), unless explicitly otherwise stated.
3110...
3111------------------------------------------------
b181d57f 3112
1bbf1c79
BF
3113A "blob" object is nothing but a binary blob of data. It doesn't refer
3114to anything else or have attributes of any kind.
3115
3116Since the blob is entirely defined by its data, if two files in a
3117directory tree (or in multiple different versions of the repository)
3118have the same contents, they will share the same blob object. The object
3119is totally independent of its location in the directory tree, and
3120renaming a file does not change the object that file is associated with.
3121
3122Note that any tree or blob object can be examined using
5162e697 3123linkgit:git-show[1] with the <revision>:<path> syntax. This can
1bbf1c79
BF
3124sometimes be useful for browsing the contents of a tree that is not
3125currently checked out.
b181d57f 3126
e34caace 3127[[trust]]
b181d57f 3128Trust
971aa71f 3129~~~~~
b181d57f 3130
a6e5ef7d 3131If you receive the SHA-1 name of a blob from one source, and its contents
1bbf1c79 3132from another (possibly untrusted) source, you can still trust that those
a6e5ef7d
FC
3133contents are correct as long as the SHA-1 name agrees. This is because
3134the SHA-1 is designed so that it is infeasible to find different contents
1bbf1c79 3135that produce the same hash.
b181d57f 3136
a6e5ef7d 3137Similarly, you need only trust the SHA-1 name of a top-level tree object
1bbf1c79 3138to trust the contents of the entire directory that it refers to, and if
a6e5ef7d 3139you receive the SHA-1 name of a commit from a trusted source, then you
1bbf1c79
BF
3140can easily verify the entire history of commits reachable through
3141parents of that commit, and all of those contents of the trees referred
3142to by those commits.