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

NAME

6       gittutorial - A tutorial introduction to Git
7

SYNOPSIS

9       git *
10

DESCRIPTION

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

IMPORTING A NEW PROJECT

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

MAKING CHANGES

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

GIT TRACKS CONTENT NOT FILES

117       Many revision control systems provide an add command that tells the
118       system to start tracking changes to a new file. Git’s add command does
119       something simpler and more powerful: git add is used both for new and
120       newly modified files, and in both cases it takes a snapshot of the
121       given files and stages that content in the index, ready for inclusion
122       in the next commit.
123

VIEWING PROJECT HISTORY

125       At any point you can view the history of your changes using
126
127           $ git log
128
129       If you also want to see complete diffs at each step, use
130
131           $ git log -p
132
133       Often the overview of the change is useful to get a feel of each step
134
135           $ git log --stat --summary
136

MANAGING BRANCHES

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

USING GIT FOR COLLABORATION

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

EXPLORING HISTORY

356       Git history is represented as a series of interrelated commits. We have
357       already seen that the git log command can list those commits. Note that
358       first line of each git log entry also gives a name for the commit:
359
360           $ git log
361           commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
362           Author: Junio C Hamano <junkio@cox.net>
363           Date:   Tue May 16 17:18:22 2006 -0700
364
365               merge-base: Clarify the comments on post processing.
366
367       We can give this name to git show to see the details about this commit.
368
369           $ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
370
371       But there are other ways to refer to commits. You can use any initial
372       part of the name that is long enough to uniquely identify the commit:
373
374           $ git show c82a22c39c   # the first few characters of the name are
375                                   # usually enough
376           $ git show HEAD         # the tip of the current branch
377           $ git show experimental # the tip of the "experimental" branch
378
379       Every commit usually has one "parent" commit which points to the
380       previous state of the project:
381
382           $ git show HEAD^  # to see the parent of HEAD
383           $ git show HEAD^^ # to see the grandparent of HEAD
384           $ git show HEAD~4 # to see the great-great grandparent of HEAD
385
386       Note that merge commits may have more than one parent:
387
388           $ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
389           $ git show HEAD^2 # show the second parent of HEAD
390
391       You can also give commits names of your own; after running
392
393           $ git tag v2.5 1b2e1d63ff
394
395       you can refer to 1b2e1d63ff by the name "v2.5". If you intend to share
396       this name with other people (for example, to identify a release
397       version), you should create a "tag" object, and perhaps sign it; see
398       git-tag(1) for details.
399
400       Any Git command that needs to know a commit can take any of these
401       names. For example:
402
403           $ git diff v2.5 HEAD     # compare the current HEAD to v2.5
404           $ git branch stable v2.5 # start a new branch named "stable" based
405                                    # at v2.5
406           $ git reset --hard HEAD^ # reset your current branch and working
407                                    # directory to its state at HEAD^
408
409       Be careful with that last command: in addition to losing any changes in
410       the working directory, it will also remove all later commits from this
411       branch. If this branch is the only branch containing those commits,
412       they will be lost. Also, don’t use git reset on a publicly-visible
413       branch that other developers pull from, as it will force needless
414       merges on other developers to clean up the history. If you need to undo
415       changes that you have pushed, use git revert instead.
416
417       The git grep command can search for strings in any version of your
418       project, so
419
420           $ git grep "hello" v2.5
421
422       searches for all occurrences of "hello" in v2.5.
423
424       If you leave out the commit name, git grep will search any of the files
425       it manages in your current directory. So
426
427           $ git grep "hello"
428
429       is a quick way to search just the files that are tracked by Git.
430
431       Many Git commands also take sets of commits, which can be specified in
432       a number of ways. Here are some examples with git log:
433
434           $ git log v2.5..v2.6            # commits between v2.5 and v2.6
435           $ git log v2.5..                # commits since v2.5
436           $ git log --since="2 weeks ago" # commits from the last 2 weeks
437           $ git log v2.5.. Makefile       # commits since v2.5 which modify
438                                           # Makefile
439
440       You can also give git log a "range" of commits where the first is not
441       necessarily an ancestor of the second; for example, if the tips of the
442       branches "stable" and "master" diverged from a common commit some time
443       ago, then
444
445           $ git log stable..master
446
447       will list commits made in the master branch but not in the stable
448       branch, while
449
450           $ git log master..stable
451
452       will show the list of commits made on the stable branch but not the
453       master branch.
454
455       The git log command has a weakness: it must present commits in a list.
456       When the history has lines of development that diverged and then merged
457       back together, the order in which git log presents those commits is
458       meaningless.
459
460       Most projects with multiple contributors (such as the Linux kernel, or
461       Git itself) have frequent merges, and gitk does a better job of
462       visualizing their history. For example,
463
464           $ gitk --since="2 weeks ago" drivers/
465
466       allows you to browse any commits from the last 2 weeks of commits that
467       modified files under the "drivers" directory. (Note: you can adjust
468       gitk’s fonts by holding down the control key while pressing "-" or
469       "+".)
470
471       Finally, most commands that take filenames will optionally allow you to
472       precede any filename by a commit, to specify a particular version of
473       the file:
474
475           $ git diff v2.5:Makefile HEAD:Makefile.in
476
477       You can also use git show to see any such file:
478
479           $ git show v2.5:Makefile
480

NEXT STEPS

482       This tutorial should be enough to perform basic distributed revision
483       control for your projects. However, to fully understand the depth and
484       power of Git you need to understand two simple ideas on which it is
485       based:
486
487       •   The object database is the rather elegant system used to store the
488           history of your project—files, directories, and commits.
489
490       •   The index file is a cache of the state of a directory tree, used to
491           create commits, check out working directories, and hold the various
492           trees involved in a merge.
493
494       Part two of this tutorial explains the object database, the index file,
495       and a few other odds and ends that you’ll need to make the most of Git.
496       You can find it at gittutorial-2(7).
497
498       If you don’t want to continue with that right away, a few other
499       digressions that may be interesting at this point are:
500
501git-format-patch(1), git-am(1): These convert series of git commits
502           into emailed patches, and vice versa, useful for projects such as
503           the Linux kernel which rely heavily on emailed patches.
504
505git-bisect(1): When there is a regression in your project, one way
506           to track down the bug is by searching through the history to find
507           the exact commit that’s to blame. Git bisect can help you perform a
508           binary search for that commit. It is smart enough to perform a
509           close-to-optimal search even in the case of complex non-linear
510           history with lots of merged branches.
511
512gitworkflows(7): Gives an overview of recommended workflows.
513
514giteveryday(7): Everyday Git with 20 Commands Or So.
515
516gitcvs-migration(7): Git for CVS users.
517

SEE ALSO

519       gittutorial-2(7), gitcvs-migration(7), gitcore-tutorial(7),
520       gitglossary(7), git-help(1), gitworkflows(7), giteveryday(7), The Git
521       User’s Manual[1]
522

GIT

524       Part of the git(1) suite
525

NOTES

527        1. The Git User’s Manual
528           file:///usr/share/doc/git/user-manual.html
529
530
531
532Git 2.36.1                        2022-05-05                    GITTUTORIAL(7)
Impressum