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             (use "git restore --staged <file>..." to unstage)
85
86                   modified:   file1
87                   modified:   file2
88                   modified:   file3
89
90       If you need to make any further adjustments, do so now, and then add
91       any newly modified content to the index. Finally, commit your changes
92       with:
93
94           $ git commit
95
96       This will again prompt you for a message describing the change, and
97       then record a new version of the project.
98
99       Alternatively, instead of running git add beforehand, you can use
100
101           $ git commit -a
102
103       which will automatically notice any modified (but not new) files, add
104       them to the index, and commit, all in one step.
105
106       A note on commit messages: Though not required, it’s a good idea to
107       begin the commit message with a single short (no more than 50
108       characters) line summarizing the change, followed by a blank line and
109       then a more thorough description. The text up to the first blank line
110       in a commit message is treated as the commit title, and that title is
111       used throughout Git. For example, git-format-patch(1) turns a commit
112       into email, and it uses the title on the Subject line and the rest of
113       the commit in the body.
114

GIT TRACKS CONTENT NOT FILES

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

VIEWING PROJECT HISTORY

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

MANAGING BRANCHES

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

USING GIT FOR COLLABORATION

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

EXPLORING HISTORY

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

NEXT STEPS

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

SEE ALSO

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

GIT

523       Part of the git(1) suite
524

NOTES

526        1. The Git User’s Manual
527           file:///usr/share/doc/git/user-manual.html
528
529
530
531Git 2.43.0                        11/20/2023                    GITTUTORIAL(7)
Impressum