1 A tutorial introduction to git
2 ==============================
4 This tutorial explains how to import a new project into git, make
5 changes to it, and share changes with other developers.
7 First, note that you can get documentation for a command such as "git
10 ------------------------------------------------
12 ------------------------------------------------
14 It is a good idea to introduce yourself to git before doing any
15 operation. The easiest way to do so is:
17 ------------------------------------------------
18 $ cat >~/.gitconfig <<\EOF
20 name = Your Name Comes Here
21 email = you@yourdomain.example.com
23 ------------------------------------------------
26 Importing a new project
27 -----------------------
29 Assume you have a tarball project.tar.gz with your initial work. You
30 can place it under git revision control as follows.
32 ------------------------------------------------
33 $ tar xzf project.tar.gz
36 ------------------------------------------------
40 ------------------------------------------------
41 Initialized empty Git repository in .git/
42 ------------------------------------------------
44 You've now initialized the working directory--you may notice a new
45 directory created, named ".git". Tell git that you want it to track
46 every file under the current directory with (notice the dot '.'
47 that means the current directory):
49 ------------------------------------------------
51 ------------------------------------------------
55 ------------------------------------------------
57 ------------------------------------------------
59 will prompt you for a commit message, then record the current state
60 of all the files to the repository.
62 Try modifying some files, then run
64 ------------------------------------------------
66 ------------------------------------------------
68 to review your changes. When you're done, tell git that you
69 want the updated contents of these files in the commit and then
70 make a commit, like this:
72 ------------------------------------------------
73 $ git add file1 file...
75 ------------------------------------------------
77 This will again prompt your for a message describing the change, and then
78 record the new versions of the files you listed. It is cumbersome
79 to list all files and you can say `git commit -a` (which stands for 'all')
80 instead of running `git add` beforehand.
82 ------------------------------------------------
84 ------------------------------------------------
86 A note on commit messages: Though not required, it's a good idea to
87 begin the commit message with a single short (less than 50 character)
88 line summarizing the change, followed by a blank line and then a more
89 thorough description. Tools that turn commits into email, for
90 example, use the first line on the Subject: line and the rest of the
94 Git tracks content not files
95 ----------------------------
97 With git you have to explicitly "add" all the changed _content_ you
98 want to commit together. This can be done in a few different ways:
100 1) By using 'git add <file_spec>...'
102 This can be performed multiple times before a commit. Note that this
103 is not only for adding new files. Even modified files must be
104 added to the set of changes about to be committed. The "git status"
105 command gives you a summary of what is included so far for the
106 next commit. When done you should use the 'git commit' command to
109 Note: don't forget to 'add' a file again if you modified it after the
110 first 'add' and before 'commit'. Otherwise only the previous added
111 state of that file will be committed. This is because git tracks
112 content, so what you're really 'add'ing to the commit is the *content*
113 of the file in the state it is in when you 'add' it.
115 2) By using 'git commit -a' directly
117 This is a quick way to automatically 'add' the content from all files
118 that were modified since the previous commit, and perform the actual
119 commit without having to separately 'add' them beforehand. This will
120 not add content from new files i.e. files that were never added before.
121 Those files still have to be added explicitly before performing a
124 But here's a twist. If you do 'git commit <file1> <file2> ...' then only
125 the changes belonging to those explicitly specified files will be
126 committed, entirely bypassing the current "added" changes. Those "added"
127 changes will still remain available for a subsequent commit though.
129 However, for normal usage you only have to remember 'git add' + 'git commit'
130 and/or 'git commit -a'.
133 Viewing the changelog
134 ---------------------
136 At any point you can view the history of your changes using
138 ------------------------------------------------
140 ------------------------------------------------
142 If you also want to see complete diffs at each step, use
144 ------------------------------------------------
146 ------------------------------------------------
148 Often the overview of the change is useful to get a feel of
151 ------------------------------------------------
152 $ git log --stat --summary
153 ------------------------------------------------
158 A single git repository can maintain multiple branches of
159 development. To create a new branch named "experimental", use
161 ------------------------------------------------
162 $ git branch experimental
163 ------------------------------------------------
167 ------------------------------------------------
169 ------------------------------------------------
171 you'll get a list of all existing branches:
173 ------------------------------------------------
176 ------------------------------------------------
178 The "experimental" branch is the one you just created, and the
179 "master" branch is a default branch that was created for you
180 automatically. The asterisk marks the branch you are currently on;
183 ------------------------------------------------
184 $ git checkout experimental
185 ------------------------------------------------
187 to switch to the experimental branch. Now edit a file, commit the
188 change, and switch back to the master branch:
190 ------------------------------------------------
193 $ git checkout master
194 ------------------------------------------------
196 Check that the change you made is no longer visible, since it was
197 made on the experimental branch and you're back on the master branch.
199 You can make a different change on the master branch:
201 ------------------------------------------------
204 ------------------------------------------------
206 at this point the two branches have diverged, with different changes
207 made in each. To merge the changes made in experimental into master, run
209 ------------------------------------------------
210 $ git pull . experimental
211 ------------------------------------------------
213 If the changes don't conflict, you're done. If there are conflicts,
214 markers will be left in the problematic files showing the conflict;
216 ------------------------------------------------
218 ------------------------------------------------
220 will show this. Once you've edited the files to resolve the
223 ------------------------------------------------
225 ------------------------------------------------
227 will commit the result of the merge. Finally,
229 ------------------------------------------------
231 ------------------------------------------------
233 will show a nice graphical representation of the resulting history.
235 At this point you could delete the experimental branch with
237 ------------------------------------------------
238 $ git branch -d experimental
239 ------------------------------------------------
241 This command ensures that the changes in the experimental branch are
242 already in the current branch.
244 If you develop on a branch crazy-idea, then regret it, you can always
245 delete the branch with
247 -------------------------------------
248 $ git branch -D crazy-idea
249 -------------------------------------
251 Branches are cheap and easy, so this is a good way to try something
254 Using git for collaboration
255 ---------------------------
257 Suppose that Alice has started a new project with a git repository in
258 /home/alice/project, and that Bob, who has a home directory on the
259 same machine, wants to contribute.
263 ------------------------------------------------
264 $ git clone /home/alice/project myrepo
265 ------------------------------------------------
267 This creates a new directory "myrepo" containing a clone of Alice's
268 repository. The clone is on an equal footing with the original
269 project, possessing its own copy of the original project's history.
271 Bob then makes some changes and commits them:
273 ------------------------------------------------
276 (repeat as necessary)
277 ------------------------------------------------
279 When he's ready, he tells Alice to pull changes from the repository
280 at /home/bob/myrepo. She does this with:
282 ------------------------------------------------
283 $ cd /home/alice/project
284 $ git pull /home/bob/myrepo master
285 ------------------------------------------------
287 This merges the changes from Bob's "master" branch into Alice's
288 current branch. If Alice has made her own changes in the meantime,
289 then she may need to manually fix any conflicts. (Note that the
290 "master" argument in the above command is actually unnecessary, as it
293 The "pull" command thus performs two operations: it fetches changes
294 from a remote branch, then merges them into the current branch.
296 You can perform the first operation alone using the "git fetch"
297 command. For example, Alice could create a temporary branch just to
298 track Bob's changes, without merging them with her own, using:
300 -------------------------------------
301 $ git fetch /home/bob/myrepo master:bob-incoming
302 -------------------------------------
304 which fetches the changes from Bob's master branch into a new branch
305 named bob-incoming. Then
307 -------------------------------------
308 $ git log -p master..bob-incoming
309 -------------------------------------
311 shows a list of all the changes that Bob made since he branched from
312 Alice's master branch.
314 After examining those changes, and possibly fixing things, Alice
315 could pull the changes into her master branch:
317 -------------------------------------
318 $ git checkout master
319 $ git pull . bob-incoming
320 -------------------------------------
322 The last command is a pull from the "bob-incoming" branch in Alice's
325 Alice could also perform both steps at once with:
327 -------------------------------------
328 $ git pull /home/bob/myrepo master:bob-incoming
329 -------------------------------------
331 This is just like the "git pull /home/bob/myrepo master" that we saw
332 before, except that it also stores the unmerged changes from bob's
333 master branch in bob-incoming before merging them into Alice's
334 current branch. Note that git pull always merges into the current
335 branch, regardless of what else is given on the commandline.
337 Later, Bob can update his repo with Alice's latest changes using
339 -------------------------------------
341 -------------------------------------
343 Note that he doesn't need to give the path to Alice's repository;
344 when Bob cloned Alice's repository, git stored the location of her
345 repository in the repository configuration, and that location is
348 -------------------------------------
349 $ git repo-config --get remote.origin.url
351 -------------------------------------
353 (The complete configuration created by git-clone is visible using
354 "git repo-config -l", and the gitlink:git-repo-config[1] man page
355 explains the meaning of each option.)
357 Git also keeps a pristine copy of Alice's master branch under the
358 name "origin/master":
360 -------------------------------------
363 -------------------------------------
365 If Bob later decides to work from a different host, he can still
366 perform clones and pulls using the ssh protocol:
368 -------------------------------------
369 $ git clone alice.org:/home/alice/project myrepo
370 -------------------------------------
372 Alternatively, git has a native protocol, or can use rsync or http;
373 see gitlink:git-pull[1] for details.
375 Git can also be used in a CVS-like mode, with a central repository
376 that various users push changes to; see gitlink:git-push[1] and
377 link:cvs-migration.html[git for CVS users].
382 Git history is represented as a series of interrelated commits. We
383 have already seen that the git log command can list those commits.
384 Note that first line of each git log entry also gives a name for the
387 -------------------------------------
389 commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
390 Author: Junio C Hamano <junkio@cox.net>
391 Date: Tue May 16 17:18:22 2006 -0700
393 merge-base: Clarify the comments on post processing.
394 -------------------------------------
396 We can give this name to git show to see the details about this
399 -------------------------------------
400 $ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
401 -------------------------------------
403 But there are other ways to refer to commits. You can use any initial
404 part of the name that is long enough to uniquely identify the commit:
406 -------------------------------------
407 $ git show c82a22c39c # the first few characters of the name are
409 $ git show HEAD # the tip of the current branch
410 $ git show experimental # the tip of the "experimental" branch
411 -------------------------------------
413 Every commit usually has one "parent" commit
414 which points to the previous state of the project:
416 -------------------------------------
417 $ git show HEAD^ # to see the parent of HEAD
418 $ git show HEAD^^ # to see the grandparent of HEAD
419 $ git show HEAD~4 # to see the great-great grandparent of HEAD
420 -------------------------------------
422 Note that merge commits may have more than one parent:
424 -------------------------------------
425 $ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
426 $ git show HEAD^2 # show the second parent of HEAD
427 -------------------------------------
429 You can also give commits names of your own; after running
431 -------------------------------------
432 $ git-tag v2.5 1b2e1d63ff
433 -------------------------------------
435 you can refer to 1b2e1d63ff by the name "v2.5". If you intend to
436 share this name with other people (for example, to identify a release
437 version), you should create a "tag" object, and perhaps sign it; see
438 gitlink:git-tag[1] for details.
440 Any git command that needs to know a commit can take any of these
443 -------------------------------------
444 $ git diff v2.5 HEAD # compare the current HEAD to v2.5
445 $ git branch stable v2.5 # start a new branch named "stable" based
447 $ git reset --hard HEAD^ # reset your current branch and working
448 # directory to its state at HEAD^
449 -------------------------------------
451 Be careful with that last command: in addition to losing any changes
452 in the working directory, it will also remove all later commits from
453 this branch. If this branch is the only branch containing those
454 commits, they will be lost. (Also, don't use "git reset" on a
455 publicly-visible branch that other developers pull from, as git will
456 be confused by history that disappears in this way.)
458 The git grep command can search for strings in any version of your
461 -------------------------------------
462 $ git grep "hello" v2.5
463 -------------------------------------
465 searches for all occurrences of "hello" in v2.5.
467 If you leave out the commit name, git grep will search any of the
468 files it manages in your current directory. So
470 -------------------------------------
472 -------------------------------------
474 is a quick way to search just the files that are tracked by git.
476 Many git commands also take sets of commits, which can be specified
477 in a number of ways. Here are some examples with git log:
479 -------------------------------------
480 $ git log v2.5..v2.6 # commits between v2.5 and v2.6
481 $ git log v2.5.. # commits since v2.5
482 $ git log --since="2 weeks ago" # commits from the last 2 weeks
483 $ git log v2.5.. Makefile # commits since v2.5 which modify
485 -------------------------------------
487 You can also give git log a "range" of commits where the first is not
488 necessarily an ancestor of the second; for example, if the tips of
489 the branches "stable-release" and "master" diverged from a common
490 commit some time ago, then
492 -------------------------------------
493 $ git log stable..experimental
494 -------------------------------------
496 will list commits made in the experimental branch but not in the
499 -------------------------------------
500 $ git log experimental..stable
501 -------------------------------------
503 will show the list of commits made on the stable branch but not
504 the experimental branch.
506 The "git log" command has a weakness: it must present commits in a
507 list. When the history has lines of development that diverged and
508 then merged back together, the order in which "git log" presents
509 those commits is meaningless.
511 Most projects with multiple contributors (such as the linux kernel,
512 or git itself) have frequent merges, and gitk does a better job of
513 visualizing their history. For example,
515 -------------------------------------
516 $ gitk --since="2 weeks ago" drivers/
517 -------------------------------------
519 allows you to browse any commits from the last 2 weeks of commits
520 that modified files under the "drivers" directory. (Note: you can
521 adjust gitk's fonts by holding down the control key while pressing
524 Finally, most commands that take filenames will optionally allow you
525 to precede any filename by a commit, to specify a particular version
528 -------------------------------------
529 $ git diff v2.5:Makefile HEAD:Makefile.in
530 -------------------------------------
532 You can also use "git show" to see any such file:
534 -------------------------------------
535 $ git show v2.5:Makefile
536 -------------------------------------
541 This tutorial should be enough to perform basic distributed revision
542 control for your projects. However, to fully understand the depth
543 and power of git you need to understand two simple ideas on which it
546 * The object database is the rather elegant system used to
547 store the history of your project--files, directories, and
550 * The index file is a cache of the state of a directory tree,
551 used to create commits, check out working directories, and
552 hold the various trees involved in a merge.
554 link:tutorial-2.html[Part two of this tutorial] explains the object
555 database, the index file, and a few other odds and ends that you'll
556 need to make the most of git.
558 If you don't want to consider with that right away, a few other
559 digressions that may be interesting at this point are:
561 * gitlink:git-format-patch[1], gitlink:git-am[1]: These convert
562 series of git commits into emailed patches, and vice versa,
563 useful for projects such as the linux kernel which rely heavily
566 * gitlink:git-bisect[1]: When there is a regression in your
567 project, one way to track down the bug is by searching through
568 the history to find the exact commit that's to blame. Git bisect
569 can help you perform a binary search for that commit. It is
570 smart enough to perform a close-to-optimal search even in the
571 case of complex non-linear history with lots of merged branches.
573 * link:everyday.html[Everyday GIT with 20 Commands Or So]
575 * link:cvs-migration.html[git for CVS users].