3 .\" Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
4 .\" Generator: DocBook XSL Stylesheets v1.79.2 <http://docbook.sf.net/>
7 .\" Source: Git 2.47.0.336.g6ea2d9d271
10 .TH "GITTUTORIAL" "7" "2024-11-25" "Git 2\&.47\&.0\&.336\&.g6ea2d9" "Git Manual"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 gittutorial \- A tutorial introduction to Git
39 This tutorial explains how to import a new project into Git, make changes to it, and share changes with other developers\&.
41 If you are instead primarily interested in using Git to fetch a project, for example, to test the latest version, you may prefer to start with the first two chapters of \m[blue]\fBThe Git User\(cqs Manual\fR\m[]\&\s-2\u[1]\d\s+2\&.
43 First, note that you can get documentation for a command such as \fBgit\fR \fBlog\fR \fB\-\-graph\fR with:
67 With the latter, you can use the manual viewer of your choice; see \fBgit-help\fR(1) for more information\&.
69 It is a good idea to introduce yourself to Git with your name and public email address before doing any operation\&. The easiest way to do so is:
75 $ git config \-\-global user\&.name "Your Name Comes Here"
76 $ git config \-\-global user\&.email you@yourdomain\&.example\&.com
81 .SH "IMPORTING A NEW PROJECT"
83 Assume you have a tarball \fBproject\&.tar\&.gz\fR with your initial work\&. You can place it under Git revision control as follows\&.
89 $ tar xzf project\&.tar\&.gz
103 Initialized empty Git repository in \&.git/
109 You\(cqve now initialized the working directory\(em\:you may notice a new directory created, named \&.\fBgit\fR\&.
111 Next, tell Git to take a snapshot of the contents of all files under the current directory (note the \&.), with \fBgit\fR \fBadd\fR:
123 This snapshot is now stored in a temporary staging area which Git calls the "index"\&. You can permanently store the contents of the index in the repository with \fBgit\fR \fBcommit\fR:
135 This will prompt you for a commit message\&. You\(cqve now stored the first version of your project in Git\&.
138 Modify some files, then add their updated contents to the index:
144 $ git add file1 file2 file3
150 You are now ready to commit\&. You can see what is about to be committed using \fBgit\fR \fBdiff\fR with the \fB\-\-cached\fR option:
156 $ git diff \-\-cached
162 (Without \fB\-\-cached\fR, \fBgit\fR \fBdiff\fR will show you any changes that you\(cqve made but not yet added to the index\&.) You can also get a brief summary of the situation with \fBgit\fR \fBstatus\fR:
170 Changes to be committed:
171 (use "git restore \-\-staged <file>\&.\&.\&." to unstage)
181 If you need to make any further adjustments, do so now, and then add any newly modified content to the index\&. Finally, commit your changes with:
193 This will again prompt you for a message describing the change, and then record a new version of the project\&.
195 Alternatively, instead of running \fBgit\fR \fBadd\fR beforehand, you can use
207 which will automatically notice any modified (but not new) files, add them to the index, and commit, all in one step\&.
209 A note on commit messages: Though not required, it\(cqs a good idea to begin the commit message with a single short (no more than 50 characters) line summarizing the change, followed by a blank line and then a more thorough description\&. The text up to the first blank line in a commit message is treated as the commit title, and that title is used throughout Git\&. For example, \fBgit-format-patch\fR(1) turns a commit into email, and it uses the title on the Subject line and the rest of the commit in the body\&.
210 .SH "GIT TRACKS CONTENT NOT FILES"
212 Many revision control systems provide an \fBadd\fR command that tells the system to start tracking changes to a new file\&. Git\(cqs \fBadd\fR command does something simpler and more powerful: \fBgit\fR \fBadd\fR is used both for new and newly modified files, and in both cases it takes a snapshot of the given files and stages that content in the index, ready for inclusion in the next commit\&.
213 .SH "VIEWING PROJECT HISTORY"
215 At any point you can view the history of your changes using
227 If you also want to see complete diffs at each step, use
239 Often the overview of the change is useful to get a feel of each step
245 $ git log \-\-stat \-\-summary
250 .SH "MANAGING BRANCHES"
252 A single Git repository can maintain multiple branches of development\&. To create a new branch named \fBexperimental\fR, use
258 $ git branch experimental
276 you\(cqll get a list of all existing branches:
289 The \fBexperimental\fR branch is the one you just created, and the \fBmaster\fR branch is a default branch that was created for you automatically\&. The asterisk marks the branch you are currently on; type
295 $ git switch experimental
301 to switch to the \fBexperimental\fR branch\&. Now edit a file, commit the change, and switch back to the \fBmaster\fR branch:
315 Check that the change you made is no longer visible, since it was made on the \fBexperimental\fR branch and you\(cqre back on the \fBmaster\fR branch\&.
317 You can make a different change on the \fBmaster\fR branch:
330 at this point the two branches have diverged, with different changes made in each\&. To merge the changes made in \fBexperimental\fR into \fBmaster\fR, run
336 $ git merge experimental
342 If the changes don\(cqt conflict, you\(cqre done\&. If there are conflicts, markers will be left in the problematic files showing the conflict;
354 will show this\&. Once you\(cqve edited the files to resolve the conflicts,
366 will commit the result of the merge\&. Finally,
378 will show a nice graphical representation of the resulting history\&.
380 At this point you could delete the \fBexperimental\fR branch with
386 $ git branch \-d experimental
392 This command ensures that the changes in the \fBexperimental\fR branch are already in the current branch\&.
394 If you develop on a branch \fBcrazy\-idea\fR, then regret it, you can always delete the branch with
400 $ git branch \-D crazy\-idea
406 Branches are cheap and easy, so this is a good way to try something out\&.
407 .SH "USING GIT FOR COLLABORATION"
409 Suppose that Alice has started a new project with a Git repository in \fB/home/alice/project\fR, and that Bob, who has a home directory on the same machine, wants to contribute\&.
417 bob$ git clone /home/alice/project myrepo
423 This creates a new directory \fBmyrepo\fR containing a clone of Alice\(cqs repository\&. The clone is on an equal footing with the original project, possessing its own copy of the original project\(cqs history\&.
425 Bob then makes some changes and commits them:
433 (repeat as necessary)
439 When he\(cqs ready, he tells Alice to pull changes from the repository at \fB/home/bob/myrepo\fR\&. She does this with:
445 alice$ cd /home/alice/project
446 alice$ git pull /home/bob/myrepo master
452 This merges the changes from Bob\(cqs \fBmaster\fR branch into Alice\(cqs current branch\&. If Alice has made her own changes in the meantime, then she may need to manually fix any conflicts\&.
454 The \fBpull\fR command thus performs two operations: it fetches changes from a remote branch, then merges them into the current branch\&.
456 Note that in general, Alice would want her local changes committed before initiating this \fBpull\fR\&. If Bob\(cqs work conflicts with what Alice did since their histories forked, Alice will use her working tree and the index to resolve conflicts, and existing local changes will interfere with the conflict resolution process (Git will still perform the fetch but will refuse to merge \(em Alice will have to get rid of her local changes in some way and pull again when this happens)\&.
458 Alice can peek at what Bob did without merging first, using the \fBfetch\fR command; this allows Alice to inspect what Bob did, using a special symbol \fBFETCH_HEAD\fR, in order to determine if he has anything worth pulling, like this:
464 alice$ git fetch /home/bob/myrepo master
465 alice$ git log \-p HEAD\&.\&.FETCH_HEAD
471 This operation is safe even if Alice has uncommitted local changes\&. The range notation \fBHEAD\fR\fB\&.\&.\fR\fBFETCH_HEAD\fR means "show everything that is reachable from the \fBFETCH_HEAD\fR but exclude anything that is reachable from \fBHEAD\fR"\&. Alice already knows everything that leads to her current state (\fBHEAD\fR), and reviews what Bob has in his state (\fBFETCH_HEAD\fR) that she has not seen with this command\&.
473 If Alice wants to visualize what Bob did since their histories forked she can issue the following command:
479 $ gitk HEAD\&.\&.FETCH_HEAD
485 This uses the same two\-dot range notation we saw earlier with \fBgit\fR \fBlog\fR\&.
487 Alice may want to view what both of them did since they forked\&. She can use three\-dot form instead of the two\-dot form:
493 $ gitk HEAD\&.\&.\&.FETCH_HEAD
499 This means "show everything that is reachable from either one, but exclude anything that is reachable from both of them"\&.
501 Please note that these range notations can be used with both \fBgitk\fR and \fBgit\fR \fBlog\fR\&.
503 After inspecting what Bob did, if there is nothing urgent, Alice may decide to continue working without pulling from Bob\&. If Bob\(cqs history does have something Alice would immediately need, Alice may choose to stash her work\-in\-progress first, do a \fBpull\fR, and then finally unstash her work\-in\-progress on top of the resulting history\&.
505 When you are working in a small closely knit group, it is not unusual to interact with the same repository over and over again\&. By defining \fIremote\fR repository shorthand, you can make it easier:
511 alice$ git remote add bob /home/bob/myrepo
517 With this, Alice can perform the first part of the \fBpull\fR operation alone using the \fBgit\fR \fBfetch\fR command without merging them with her own branch, using:
529 Unlike the longhand form, when Alice fetches from Bob using a remote repository shorthand set up with \fBgit\fR \fBremote\fR, what was fetched is stored in a remote\-tracking branch, in this case \fBbob/master\fR\&. So after this:
535 alice$ git log \-p master\&.\&.bob/master
541 shows a list of all the changes that Bob made since he branched from Alice\(cqs \fBmaster\fR branch\&.
543 After examining those changes, Alice could merge the changes into her \fBmaster\fR branch:
549 alice$ git merge bob/master
555 This \fBmerge\fR can also be done by \fIpulling from her own remote\-tracking branch\fR, like this:
561 alice$ git pull \&. remotes/bob/master
567 Note that git pull always merges into the current branch, regardless of what else is given on the command line\&.
569 Later, Bob can update his repo with Alice\(cqs latest changes using
581 Note that he doesn\(cqt need to give the path to Alice\(cqs repository; when Bob cloned Alice\(cqs repository, Git stored the location of her repository in the repository configuration, and that location is used for pulls:
587 bob$ git config \-\-get remote\&.origin\&.url
594 (The complete configuration created by \fBgit\fR \fBclone\fR is visible using \fBgit\fR \fBconfig\fR \fB\-l\fR, and the \fBgit-config\fR(1) man page explains the meaning of each option\&.)
596 Git also keeps a pristine copy of Alice\(cqs \fBmaster\fR branch under the name \fBorigin/master\fR:
609 If Bob later decides to work from a different host, he can still perform clones and pulls using the ssh protocol:
615 bob$ git clone alice\&.org:/home/alice/project myrepo
621 Alternatively, Git has a native protocol, or can use http; see \fBgit-pull\fR(1) for details\&.
623 Git can also be used in a CVS\-like mode, with a central repository that various users push changes to; see \fBgit-push\fR(1) and \fBgitcvs-migration\fR(7)\&.
624 .SH "EXPLORING HISTORY"
626 Git history is represented as a series of interrelated commits\&. We have already seen that the \fBgit\fR \fBlog\fR command can list those commits\&. Note that first line of each \fBgit\fR \fBlog\fR entry also gives a name for the commit:
633 commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
634 Author: Junio C Hamano <junkio@cox\&.net>
635 Date: Tue May 16 17:18:22 2006 \-0700
637 merge\-base: Clarify the comments on post processing\&.
643 We can give this name to \fBgit\fR \fBshow\fR to see the details about this commit\&.
649 $ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
655 But there are other ways to refer to commits\&. You can use any initial part of the name that is long enough to uniquely identify the commit:
661 $ git show c82a22c39c # the first few characters of the name are
663 $ git show HEAD # the tip of the current branch
664 $ git show experimental # the tip of the "experimental" branch
670 Every commit usually has one "parent" commit which points to the previous state of the project:
676 $ git show HEAD^ # to see the parent of HEAD
677 $ git show HEAD^^ # to see the grandparent of HEAD
678 $ git show HEAD~4 # to see the great\-great grandparent of HEAD
684 Note that merge commits may have more than one parent:
690 $ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
691 $ git show HEAD^2 # show the second parent of HEAD
697 You can also give commits names of your own; after running
703 $ git tag v2\&.5 1b2e1d63ff
709 you can refer to \fB1b2e1d63ff\fR by the name \fBv2\&.5\fR\&. If you intend to share this name with other people (for example, to identify a release version), you should create a "tag" object, and perhaps sign it; see \fBgit-tag\fR(1) for details\&.
711 Any Git command that needs to know a commit can take any of these names\&. For example:
717 $ git diff v2\&.5 HEAD # compare the current HEAD to v2\&.5
718 $ git branch stable v2\&.5 # start a new branch named "stable" based
720 $ git reset \-\-hard HEAD^ # reset your current branch and working
721 # directory to its state at HEAD^
727 Be careful with that last command: in addition to losing any changes in the working directory, it will also remove all later commits from this branch\&. If this branch is the only branch containing those commits, they will be lost\&. Also, don\(cqt use \fBgit\fR \fBreset\fR on a publicly\-visible branch that other developers pull from, as it will force needless merges on other developers to clean up the history\&. If you need to undo changes that you have pushed, use \fBgit\fR \fBrevert\fR instead\&.
729 The \fBgit\fR \fBgrep\fR command can search for strings in any version of your project, so
735 $ git grep "hello" v2\&.5
741 searches for all occurrences of "hello" in \fBv2\&.5\fR\&.
743 If you leave out the commit name, \fBgit\fR \fBgrep\fR will search any of the files it manages in your current directory\&. So
755 is a quick way to search just the files that are tracked by Git\&.
757 Many Git commands also take sets of commits, which can be specified in a number of ways\&. Here are some examples with \fBgit\fR \fBlog\fR:
763 $ git log v2\&.5\&.\&.v2\&.6 # commits between v2\&.5 and v2\&.6
764 $ git log v2\&.5\&.\&. # commits since v2\&.5
765 $ git log \-\-since="2 weeks ago" # commits from the last 2 weeks
766 $ git log v2\&.5\&.\&. Makefile # commits since v2\&.5 which modify
773 You can also give \fBgit\fR \fBlog\fR a "range" of commits where the first is not necessarily an ancestor of the second; for example, if the tips of the branches \fBstable\fR and \fBmaster\fR diverged from a common commit some time ago, then
779 $ git log stable\&.\&.master
785 will list commits made in the \fBmaster\fR branch but not in the stable branch, while
791 $ git log master\&.\&.stable
797 will show the list of commits made on the stable branch but not the \fBmaster\fR branch\&.
799 The \fBgit\fR \fBlog\fR command has a weakness: it must present commits in a list\&. When the history has lines of development that diverged and then merged back together, the order in which \fBgit\fR \fBlog\fR presents those commits is meaningless\&.
801 Most projects with multiple contributors (such as the Linux kernel, or Git itself) have frequent merges, and \fBgitk\fR does a better job of visualizing their history\&. For example,
807 $ gitk \-\-since="2 weeks ago" drivers/
813 allows you to browse any commits from the last 2 weeks of commits that modified files under the \fBdrivers\fR directory\&. (Note: you can adjust gitk\(cqs fonts by holding down the control key while pressing "\-" or "+"\&.)
815 Finally, most commands that take filenames will optionally allow you to precede any filename by a commit, to specify a particular version of the file:
821 $ git diff v2\&.5:Makefile HEAD:Makefile\&.in
827 You can also use \fBgit\fR \fBshow\fR to see any such file:
833 $ git show v2\&.5:Makefile
840 This tutorial should be enough to perform basic distributed revision control for your projects\&. However, to fully understand the depth and power of Git you need to understand two simple ideas on which it is based:
850 The object database is the rather elegant system used to store the history of your project\(em\:files, directories, and commits\&.
861 The index file is a cache of the state of a directory tree, used to create commits, check out working directories, and hold the various trees involved in a merge\&.
864 Part two of this tutorial explains the object database, the index file, and a few other odds and ends that you\(cqll need to make the most of Git\&. You can find it at \fBgittutorial-2\fR(7)\&.
866 If you don\(cqt want to continue with that right away, a few other digressions that may be interesting at this point are:
876 \fBgit-format-patch\fR(1),
877 \fBgit-am\fR(1): These convert series of git commits into emailed patches, and vice versa, useful for projects such as the Linux kernel which rely heavily on emailed patches\&.
888 \fBgit-bisect\fR(1): When there is a regression in your project, one way to track down the bug is by searching through the history to find the exact commit that\(cqs to blame\&.
891 can help you perform a binary search for that commit\&. It is smart enough to perform a close\-to\-optimal search even in the case of complex non\-linear history with lots of merged branches\&.
902 \fBgitworkflows\fR(7): Gives an overview of recommended workflows\&.
913 \fBgiteveryday\fR(7): Everyday Git with 20 Commands Or So\&.
924 \fBgitcvs-migration\fR(7): Git for CVS users\&.
928 \fBgittutorial-2\fR(7), \fBgitcvs-migration\fR(7), \fBgitcore-tutorial\fR(7), \fBgitglossary\fR(7), \fBgit-help\fR(1), \fBgitworkflows\fR(7), \fBgiteveryday\fR(7), \m[blue]\fBThe Git User\(cqs Manual\fR\m[]\&\s-2\u[1]\d\s+2
931 Part of the \fBgit\fR(1) suite
934 The Git User\(cqs Manual
936 \%git-htmldocs/user-manual.html