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

NAME

6       gittutorial - A tutorial introduction to Git
7

SYNOPSIS

9       git *
10
11

DESCRIPTION

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

IMPORTING A NEW PROJECT

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

MAKING CHANGES

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

GIT TRACKS CONTENT NOT FILES

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

VIEWING PROJECT HISTORY

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

MANAGING BRANCHES

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

USING GIT FOR COLLABORATION

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

EXPLORING HISTORY

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

NEXT STEPS

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

SEE ALSO

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

GIT

582       Part of the git(1) suite
583

NOTES

585        1. The Git User’s Manual
586           file:///usr/share/doc/git/user-manual.html
587
588
589
590Git 2.18.1                        05/14/2019                    GITTUTORIAL(7)
Impressum