Quick Introduction to Using Git

To see git in action, let’s create a new repository, add some content, and manage a few revisions.

There are two fundamental techniques for establishing a Git repository. You can either create it from scratch, populating it with an existing body of work, or you can copy, or clone, an existing repository. It’s simpler to start with an empty repository, so let’s start there.

Creating an Initial Repository

To model a typical situation, let’s create a repository for your personal website from the directory ~/public_html and place it in a Git repository.

If you don’t have content for your personal website in ~/public_html, create the directory and place some simple content in a file called index.html:

$ mkdir ~/public_html
$ cd ~/public_html
$ echo 'My website is alive!' > index.html

To turn ~/public_html or any directory into a Git repository, run git init:

$ git init

Initialized empty Git repository in .git/

Git doesn’t care whether you start with a completely empty directory or with a directory full of files. In either case, the process of converting the directory into a Git repository is the same.

To signify that your directory is a Git repository, the git init command creates a hidden directory, called .git, at the top level of your project. Whereas CVS and Subversion place revision information in CVS and .svn subdirectories within each of your project’s directories, Git places all its revision information in this one top-level .git directory. ...

Get Version Control with Git now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.