1GITTUTORIAL(7)                    Git Manual                    GITTUTORIAL(7)
2
3
4

NAME

6       gittutorial - A tutorial introduction to Git (for version 1.5.1 or
7       newer)
8

SYNOPSIS

10       git *
11
12

DESCRIPTION

14       This tutorial explains how to import a new project into Git, make
15       changes to it, and share changes with other developers.
16
17       If you are instead primarily interested in using Git to fetch a
18       project, for example, to test the latest version, you may prefer to
19       start with the first two chapters of The Git User’s Manual[1].
20
21       First, note that you can get documentation for a command such as git
22       log --graph with:
23
24           $ man git-log
25
26
27       or:
28
29           $ git help log
30
31
32       With the latter, you can use the manual viewer of your choice; see git-
33       help(1) for more information.
34
35       It is a good idea to introduce yourself to Git with your name and
36       public email address before doing any operation. The easiest way to do
37       so is:
38
39           $ git config --global user.name "Your Name Comes Here"
40           $ git config --global user.email you@yourdomain.example.com
41
42

IMPORTING A NEW PROJECT

44       Assume you have a tarball project.tar.gz with your initial work. You
45       can place it under Git revision control as follows.
46
47           $ tar xzf project.tar.gz
48           $ cd project
49           $ git init
50
51
52       Git will reply
53
54           Initialized empty Git repository in .git/
55
56
57       You’ve now initialized the working directory—you may notice a new
58       directory created, named ".git".
59
60       Next, tell Git to take a snapshot of the contents of all files under
61       the current directory (note the .), with git add:
62
63           $ git add .
64
65
66       This snapshot is now stored in a temporary staging area which Git calls
67       the "index". You can permanently store the contents of the index in the
68       repository with git commit:
69
70           $ git commit
71
72
73       This will prompt you for a commit message. You’ve now stored the first
74       version of your project in Git.
75

MAKING CHANGES

77       Modify some files, then add their updated contents to the index:
78
79           $ git add file1 file2 file3
80
81
82       You are now ready to commit. You can see what is about to be committed
83       using git diff with the --cached option:
84
85           $ git diff --cached
86
87
88       (Without --cached, git diff will show you any changes that you’ve made
89       but not yet added to the index.) You can also get a brief summary of
90       the situation with git status:
91
92           $ git status
93           # On branch master
94           # Changes to be committed:
95           #   (use "git reset HEAD <file>..." to unstage)
96           #
97           #       modified:   file1
98           #       modified:   file2
99           #       modified:   file3
100           #
101
102
103       If you need to make any further adjustments, do so now, and then add
104       any newly modified content to the index. Finally, commit your changes
105       with:
106
107           $ git commit
108
109
110       This will again prompt you for a message describing the change, and
111       then record a new version of the project.
112
113       Alternatively, instead of running git add beforehand, you can use
114
115           $ git commit -a
116
117
118       which will automatically notice any modified (but not new) files, add
119       them to the index, and commit, all in one step.
120
121       A note on commit messages: Though not required, it’s a good idea to
122       begin the commit message with a single short (less than 50 character)
123       line summarizing the change, followed by a blank line and then a more
124       thorough description. The text up to the first blank line in a commit
125       message is treated as the commit title, and that title is used
126       throughout Git. For example, git-format-patch(1) turns a commit into
127       email, and it uses the title on the Subject line and the rest of the
128       commit in the body.
129

GIT TRACKS CONTENT NOT FILES

131       Many revision control systems provide an add command that tells the
132       system to start tracking changes to a new file. Git’s add command does
133       something simpler and more powerful: git add is used both for new and
134       newly modified files, and in both cases it takes a snapshot of the
135       given files and stages that content in the index, ready for inclusion
136       in the next commit.
137

VIEWING PROJECT HISTORY

139       At any point you can view the history of your changes using
140
141           $ git log
142
143
144       If you also want to see complete diffs at each step, use
145
146           $ git log -p
147
148
149       Often the overview of the change is useful to get a feel of each step
150
151           $ git log --stat --summary
152
153

MANAGING BRANCHES

155       A single Git repository can maintain multiple branches of development.
156       To create a new branch named "experimental", use
157
158           $ git branch experimental
159
160
161       If you now run
162
163           $ git branch
164
165
166       you’ll get a list of all existing branches:
167
168             experimental
169           * master
170
171
172       The "experimental" branch is the one you just created, and the "master"
173       branch is a default branch that was created for you automatically. The
174       asterisk marks the branch you are currently on; type
175
176           $ git checkout experimental
177
178
179       to switch to the experimental branch. Now edit a file, commit the
180       change, and switch back to the master branch:
181
182           (edit file)
183           $ git commit -a
184           $ git checkout master
185
186
187       Check that the change you made is no longer visible, since it was made
188       on the experimental branch and you’re back on the master branch.
189
190       You can make a different change on the master branch:
191
192           (edit file)
193           $ git commit -a
194
195
196       at this point the two branches have diverged, with different changes
197       made in each. To merge the changes made in experimental into master,
198       run
199
200           $ git merge experimental
201
202
203       If the changes don’t conflict, you’re done. If there are conflicts,
204       markers will be left in the problematic files showing the conflict;
205
206           $ git diff
207
208
209       will show this. Once you’ve edited the files to resolve the conflicts,
210
211           $ git commit -a
212
213
214       will commit the result of the merge. Finally,
215
216           $ gitk
217
218
219       will show a nice graphical representation of the resulting history.
220
221       At this point you could delete the experimental branch with
222
223           $ git branch -d experimental
224
225
226       This command ensures that the changes in the experimental branch are
227       already in the current branch.
228
229       If you develop on a branch crazy-idea, then regret it, you can always
230       delete the branch with
231
232           $ git branch -D crazy-idea
233
234
235       Branches are cheap and easy, so this is a good way to try something
236       out.
237

USING GIT FOR COLLABORATION

239       Suppose that Alice has started a new project with a Git repository in
240       /home/alice/project, and that Bob, who has a home directory on the same
241       machine, wants to contribute.
242
243       Bob begins with:
244
245           bob$ git clone /home/alice/project myrepo
246
247
248       This creates a new directory "myrepo" containing a clone of Alice’s
249       repository. The clone is on an equal footing with the original project,
250       possessing its own copy of the original project’s history.
251
252       Bob then makes some changes and commits them:
253
254           (edit files)
255           bob$ git commit -a
256           (repeat as necessary)
257
258
259       When he’s ready, he tells Alice to pull changes from the repository at
260       /home/bob/myrepo. She does this with:
261
262           alice$ cd /home/alice/project
263           alice$ git pull /home/bob/myrepo master
264
265
266       This merges the changes from Bob’s "master" branch into Alice’s current
267       branch. If Alice has made her own changes in the meantime, then she may
268       need to manually fix any conflicts.
269
270       The "pull" command thus performs two operations: it fetches changes
271       from a remote branch, then merges them into the current branch.
272
273       Note that in general, Alice would want her local changes committed
274       before initiating this "pull". If Bob’s work conflicts with what Alice
275       did since their histories forked, Alice will use her working tree and
276       the index to resolve conflicts, and existing local changes will
277       interfere with the conflict resolution process (Git will still perform
278       the fetch but will refuse to merge --- Alice will have to get rid of
279       her local changes in some way and pull again when this happens).
280
281       Alice can peek at what Bob did without merging first, using the "fetch"
282       command; this allows Alice to inspect what Bob did, using a special
283       symbol "FETCH_HEAD", in order to determine if he has anything worth
284       pulling, like this:
285
286           alice$ git fetch /home/bob/myrepo master
287           alice$ git log -p HEAD..FETCH_HEAD
288
289
290       This operation is safe even if Alice has uncommitted local changes. The
291       range notation "HEAD..FETCH_HEAD" means "show everything that is
292       reachable from the FETCH_HEAD but exclude anything that is reachable
293       from HEAD". Alice already knows everything that leads to her current
294       state (HEAD), and reviews what Bob has in his state (FETCH_HEAD) that
295       she has not seen with this command.
296
297       If Alice wants to visualize what Bob did since their histories forked
298       she can issue the following command:
299
300           $ gitk HEAD..FETCH_HEAD
301
302
303       This uses the same two-dot range notation we saw earlier with git log.
304
305       Alice may want to view what both of them did since they forked. She can
306       use three-dot form instead of the two-dot form:
307
308           $ gitk HEAD...FETCH_HEAD
309
310
311       This means "show everything that is reachable from either one, but
312       exclude anything that is reachable from both of them".
313
314       Please note that these range notation can be used with both gitk and
315       "git log".
316
317       After inspecting what Bob did, if there is nothing urgent, Alice may
318       decide to continue working without pulling from Bob. If Bob’s history
319       does have something Alice would immediately need, Alice may choose to
320       stash her work-in-progress first, do a "pull", and then finally unstash
321       her work-in-progress on top of the resulting history.
322
323       When you are working in a small closely knit group, it is not unusual
324       to interact with the same repository over and over again. By defining
325       remote repository shorthand, you can make it easier:
326
327           alice$ git remote add bob /home/bob/myrepo
328
329
330       With this, Alice can perform the first part of the "pull" operation
331       alone using the git fetch command without merging them with her own
332       branch, using:
333
334           alice$ git fetch bob
335
336
337       Unlike the longhand form, when Alice fetches from Bob using a remote
338       repository shorthand set up with git remote, what was fetched is stored
339       in a remote-tracking branch, in this case bob/master. So after this:
340
341           alice$ git log -p master..bob/master
342
343
344       shows a list of all the changes that Bob made since he branched from
345       Alice’s master branch.
346
347       After examining those changes, Alice could merge the changes into her
348       master branch:
349
350           alice$ git merge bob/master
351
352
353       This merge can also be done by pulling from her own remote-tracking
354       branch, like this:
355
356           alice$ git pull . remotes/bob/master
357
358
359       Note that git pull always merges into the current branch, regardless of
360       what else is given on the command line.
361
362       Later, Bob can update his repo with Alice’s latest changes using
363
364           bob$ git pull
365
366
367       Note that he doesn’t need to give the path to Alice’s repository; when
368       Bob cloned Alice’s repository, Git stored the location of her
369       repository in the repository configuration, and that location is used
370       for pulls:
371
372           bob$ git config --get remote.origin.url
373           /home/alice/project
374
375
376       (The complete configuration created by git clone is visible using git
377       config -l, and the git-config(1) man page explains the meaning of each
378       option.)
379
380       Git also keeps a pristine copy of Alice’s master branch under the name
381       "origin/master":
382
383           bob$ git branch -r
384             origin/master
385
386
387       If Bob later decides to work from a different host, he can still
388       perform clones and pulls using the ssh protocol:
389
390           bob$ git clone alice.org:/home/alice/project myrepo
391
392
393       Alternatively, Git has a native protocol, or can use rsync or http; see
394       git-pull(1) for details.
395
396       Git can also be used in a CVS-like mode, with a central repository that
397       various users push changes to; see git-push(1) and gitcvs-migration(7).
398

EXPLORING HISTORY

400       Git history is represented as a series of interrelated commits. We have
401       already seen that the git log command can list those commits. Note that
402       first line of each git log entry also gives a name for the commit:
403
404           $ git log
405           commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
406           Author: Junio C Hamano <junkio@cox.net>
407           Date:   Tue May 16 17:18:22 2006 -0700
408
409               merge-base: Clarify the comments on post processing.
410
411
412       We can give this name to git show to see the details about this commit.
413
414           $ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
415
416
417       But there are other ways to refer to commits. You can use any initial
418       part of the name that is long enough to uniquely identify the commit:
419
420           $ git show c82a22c39c   # the first few characters of the name are
421                                   # usually enough
422           $ git show HEAD         # the tip of the current branch
423           $ git show experimental # the tip of the "experimental" branch
424
425
426       Every commit usually has one "parent" commit which points to the
427       previous state of the project:
428
429           $ git show HEAD^  # to see the parent of HEAD
430           $ git show HEAD^^ # to see the grandparent of HEAD
431           $ git show HEAD~4 # to see the great-great grandparent of HEAD
432
433
434       Note that merge commits may have more than one parent:
435
436           $ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
437           $ git show HEAD^2 # show the second parent of HEAD
438
439
440       You can also give commits names of your own; after running
441
442           $ git tag v2.5 1b2e1d63ff
443
444
445       you can refer to 1b2e1d63ff by the name "v2.5". If you intend to share
446       this name with other people (for example, to identify a release
447       version), you should create a "tag" object, and perhaps sign it; see
448       git-tag(1) for details.
449
450       Any Git command that needs to know a commit can take any of these
451       names. For example:
452
453           $ git diff v2.5 HEAD     # compare the current HEAD to v2.5
454           $ git branch stable v2.5 # start a new branch named "stable" based
455                                    # at v2.5
456           $ git reset --hard HEAD^ # reset your current branch and working
457                                    # directory to its state at HEAD^
458
459
460       Be careful with that last command: in addition to losing any changes in
461       the working directory, it will also remove all later commits from this
462       branch. If this branch is the only branch containing those commits,
463       they will be lost. Also, don’t use git reset on a publicly-visible
464       branch that other developers pull from, as it will force needless
465       merges on other developers to clean up the history. If you need to undo
466       changes that you have pushed, use git revert instead.
467
468       The git grep command can search for strings in any version of your
469       project, so
470
471           $ git grep "hello" v2.5
472
473
474       searches for all occurrences of "hello" in v2.5.
475
476       If you leave out the commit name, git grep will search any of the files
477       it manages in your current directory. So
478
479           $ git grep "hello"
480
481
482       is a quick way to search just the files that are tracked by Git.
483
484       Many Git commands also take sets of commits, which can be specified in
485       a number of ways. Here are some examples with git log:
486
487           $ git log v2.5..v2.6            # commits between v2.5 and v2.6
488           $ git log v2.5..                # commits since v2.5
489           $ git log --since="2 weeks ago" # commits from the last 2 weeks
490           $ git log v2.5.. Makefile       # commits since v2.5 which modify
491                                           # Makefile
492
493
494       You can also give git log a "range" of commits where the first is not
495       necessarily an ancestor of the second; for example, if the tips of the
496       branches "stable" and "master" diverged from a common commit some time
497       ago, then
498
499           $ git log stable..master
500
501
502       will list commits made in the master branch but not in the stable
503       branch, while
504
505           $ git log master..stable
506
507
508       will show the list of commits made on the stable branch but not the
509       master branch.
510
511       The git log command has a weakness: it must present commits in a list.
512       When the history has lines of development that diverged and then merged
513       back together, the order in which git log presents those commits is
514       meaningless.
515
516       Most projects with multiple contributors (such as the Linux kernel, or
517       Git itself) have frequent merges, and gitk does a better job of
518       visualizing their history. For example,
519
520           $ gitk --since="2 weeks ago" drivers/
521
522
523       allows you to browse any commits from the last 2 weeks of commits that
524       modified files under the "drivers" directory. (Note: you can adjust
525       gitk’s fonts by holding down the control key while pressing "-" or
526       "+".)
527
528       Finally, most commands that take filenames will optionally allow you to
529       precede any filename by a commit, to specify a particular version of
530       the file:
531
532           $ git diff v2.5:Makefile HEAD:Makefile.in
533
534
535       You can also use git show to see any such file:
536
537           $ git show v2.5:Makefile
538
539

NEXT STEPS

541       This tutorial should be enough to perform basic distributed revision
542       control for your projects. However, to fully understand the depth and
543       power of Git you need to understand two simple ideas on which it is
544       based:
545
546       ·   The object database is the rather elegant system used to store the
547           history of your project—files, directories, and commits.
548
549       ·   The index file is a cache of the state of a directory tree, used to
550           create commits, check out working directories, and hold the various
551           trees involved in a merge.
552
553       Part two of this tutorial explains the object database, the index file,
554       and a few other odds and ends that you’ll need to make the most of Git.
555       You can find it at gittutorial-2(7).
556
557       If you don’t want to continue with that right away, a few other
558       digressions that may be interesting at this point are:
559
560       ·   git-format-patch(1), git-am(1): These convert series of git commits
561           into emailed patches, and vice versa, useful for projects such as
562           the Linux kernel which rely heavily on emailed patches.
563
564       ·   git-bisect(1): When there is a regression in your project, one way
565           to track down the bug is by searching through the history to find
566           the exact commit that’s to blame. Git bisect can help you perform a
567           binary search for that commit. It is smart enough to perform a
568           close-to-optimal search even in the case of complex non-linear
569           history with lots of merged branches.
570
571       ·   gitworkflows(7): Gives an overview of recommended workflows.
572
573       ·   Everyday Git with 20 Commands Or So[2]
574
575       ·   gitcvs-migration(7): Git for CVS users.
576

SEE ALSO

578       gittutorial-2(7), gitcvs-migration(7), gitcore-tutorial(7),
579       gitglossary(7), git-help(1), gitworkflows(7), Everyday Git[2], The Git
580       User’s Manual[1]
581

GIT

583       Part of the git(1) suite.
584

NOTES

586        1. The Git User’s Manual
587           file:///usr/share/doc/git-1.8.3.1/user-manual.html
588
589        2. Everyday Git with 20 Commands Or So
590           file:///usr/share/doc/git-1.8.3.1/everyday.html
591
592
593
594Git 1.8.3.1                       11/19/2018                    GITTUTORIAL(7)
Impressum