Git - gitcore-tutorial Documentation
English ▾ English Topics ▾ Setup and Config git config help bugreport Credential helpers Getting and Creating Projects init clone Basic Snapshotting add status diff commit notes restore reset rm mv Branching and Merging branch checkout switch merge mergetool log stash tag worktree Sharing and Updating Projects fetch pull push remote submodule Inspection and Comparison show log diff difftool range-diff shortlog describe Patching apply cherry-pick diff rebase revert Debugging bisect blame grep Email am apply imap-send format-patch send-email request-pull External Systems svn fast-import Server Admin daemon update-server-info Guides gitattributes Command-line interface conventions Everyday Git Frequently Asked Questions (FAQ) Glossary Hooks gitignore gitmodules Revisions Submodules Tutorial Workflows All guides...
Administration clean gc fsck reflog filter-branch instaweb archive bundle Plumbing Commands cat-file check-ignore checkout-index commit-tree count-objects diff-index for-each-ref hash-object ls-files ls-tree merge-base read-tree rev-list rev-parse show-ref symbolic-ref update-index update-ref verify-pack write-tree Latest version ▾ gitcore-tutorial last updated in 2.43.1 2.43.2 → 2.56.1 no changes 2.43.1 2024-02-09 2.25.2 → 2.43.0 no changes 2.25.1 2020-02-17 2.23.1 → 2.25.0 no changes 2.23.0 2019-08-16 2.16.6 → 2.22.5 no changes 2.15.4 2019-12-06 2.14.6 no changes 2.13.7 2018-05-22 2.12.5 2017-09-22 2.11.4 2017-09-22 2.10.5 no changes 2.9.5 2017-07-30 2.8.6 2017-07-30 2.5.6 → 2.7.6 no changes 2.4.12 2017-05-05 2.3.10 no changes 2.2.3 2015-09-04 2.1.4 no changes 2.0.5 2014-12-17 NAME gitcore-tutorial - A Git core tutorial for developers SYNOPSIS git * DESCRIPTION This tutorial explains how to use the "core" Git commands to set up and work with a Git repository.
If you just need to use Git as a revision control system you may prefer to start with "A Tutorial Introduction to Git" ( gittutorial[7] ) or the Git User Manual . However, an understanding of these low-level tools can be helpful if you want to understand Git’s internals. The core Git is often called "plumbing", with the prettier user interfaces on top of it called "porcelain". You may not want to use the plumbing directly very often, but it can be good to know what the plumbing does when the porcelain isn’t flushing.
Back when this document was originally written, many porcelain commands were shell scripts. For simplicity, it still uses them as examples to illustrate how plumbing is fit together to form the porcelain commands. The source tree includes some of these scripts in contrib/examples/ for reference. Although these are not implemented as shell scripts anymore, the description of what the plumbing layer commands do is still valid.
Note Deeper technical details are often marked as Notes, which you can skip on your first reading. Creating a Git repository Creating a new Git repository couldn’t be easier: all Git repositories start out empty, and the only thing you need to do is find yourself a subdirectory that you want to use as a working tree - either an empty one for a totally new project, or an existing working tree that you want to import into Git.
For our first example, we’re going to start a totally new repository from scratch, with no pre-existing files, and we’ll call it git-tutorial . To start up, create a subdirectory for it, change into that subdirectory, and initialize the Git infrastructure with git init : $ mkdir git-tutorial $ cd git-tutorial $ git init to which Git will reply Initialized empty Git repository in .git/ which is just Git’s way of saying that you haven’t been doing anything strange, and that it will have created a local .git directory setup for your new project.
You will now have a .git directory, and you can inspect that with ls . For your new empty project, it should show you three entries, among other things: a file called HEAD , that has ref: refs/heads/master in it. This is similar to a symbolic link and points at refs/heads/master relative to the HEAD file. Don’t worry about the fact that the file that the HEAD link points to doesn’t even exist yet — you haven’t created the commit that will start your HEAD development branch yet. a subdirectory called objects , which will contain all the objects of your project.
You should never have any real reason to look at the objects directly, but you might want to know that these objects are what contains all the real data in your repository. a subdirectory called refs , which contains references to objects. In particular, the refs subdirectory will contain two other subdirectories, named heads and tags respectively. They do exactly what their names imply: they contain references to any number of different heads of development (aka branches ), and to any tags that you have created to name specific versions in your repository.
One note: the special master head is the default branch, which is why the .git/HEAD file was created points to it even if it doesn’t yet exist. Basically, the HEAD link is supposed to always point to the branch you are working on right now, and you always start out expecting to work on the master branch. However, this is only a convention, and you can name your branches anything you want, and don’t have to ever even have a master branch.
A number of the Git tools will assume that .git/HEAD is valid, though. Note An object is identified by its 160-bit SHA-1 hash, aka object name , and a reference to an object is always the 40-byte hex representation of that SHA-1 name. The files in the refs subdirectory are expected to contain these hex references (usually with a final \n at the end), and you should thus expect to see a number of 41-byte files containing these references in these refs subdirectories when you actually start populating your tree.
Note An advanced user may want to take a look at gitrepository-layout[5] after finishing this tutorial. You have now created your first Git repository. Of course, since it’s empty, that’s not very useful, so let’s start populating it with data. Populating a Git repository We’ll keep this simple and stupid, so we’ll start off with populating a few trivial files just to get a feel for it. Start off with just creating any random files that you want to maintain in your Git repository.
We’ll start off with a few bad examples, just to get a feel for how this works: $ echo "Hello World" >hello $ echo "Silly example" >example you have now created two files in your working tree (aka working directory ), but to actually check in your hard work, you will have to go through two steps: fill in the index file (aka cache ) with the information about your working tree state. commit that index file as an object.
The first step is trivial: when you want to tell Git about any changes to your working tree, you use the git update-index program. That program normally just takes a list of filenames you want to update, but to avoid trivial mistakes, it refuses to add new entries to the index (or remove existing ones) unless you explicitly tell it that you’re adding a new entry with the --add flag (or removing an entry with the --remove ) flag.
So to populate the index with the two files you just created, you can do $ git update-index --add hello example and you have now told Git to track those two files. In fact, as you did that, if you now look into your object directory, you’ll notice that Git will have added two new objects to the object database. If you did exactly the steps above, you should now be able to do $ ls .git/objects/??/* and see two files: .git/objects/55/7db03de997c86a4a028e1ebd3a1ceb225be238 .git/objects/f2/4c74a2e500f5ee1332c86b94199f52b1d1d962 which correspond with the objects with names of 557db... and f24c7... respectively.
If you want to, you can use git cat-file to look at those objects, but you’ll have to use the object name, not the filename of the object: $ git cat-file -t 557db03de
The rest of this page is on the live site.