Git Basics

By Xah Lee. Date: . Last updated: .

This page is beginner tutorial of using git. Git is a version control system. This tutorial focus on the most important commands that you need to use every day.

If you don't have git installed, see: Git: Install on {Linux, Mac, Windows}.

Basic Git Commands Overview

Everything you do is with git commands. Here's a overview.

What commands are available?

Type git help to get a list of commonly used git commands.

git help screen
git help output.

Here's the most commonly used commands. Those with a ★ are use daily. You can ignore others for now.

To begin, you either create a new project, or “clone” a project from a URL.

git Commands for New Project
Command NameMost Common Use
git initCreate a empty git repository in current dir. This will create a directory named “.git” in current dir.
git clone urlClone the repository from url to current dir.

Once you have a project, you edit to make changes, then you need to “add” to a staging area, then “commit” (to local disk).

Most Commonly Used git Commands for Changes in Local Repository
Command NameMost Common Use
git add .Add changes from current dir to local repository's staging area.
git commit -m"message"commit changes after add, to local repository. The message is a short description of changes you made.

Here's commands to get info about current state.

Most Commonly Used git Commands for Getting Info
Command NameMost Common Use
git statusShow the working tree status
git logShow commit logs
git diffShow changes between commits, commit and working tree, etc

To actually get update from a server, or put your changes to a server , you need to “pull/push”.

Most Commonly Used Git Commands for Sync with Remote Repository
Command NameMost Common Use
git pullPull from remote to your local repository.
git pushPush your local repository to remote.

Set Up Name and Email Info

First, you need to setup a default name and email. Else, git won't let you commit.

# set up default name and email
git config --global "Mary Lee"
git config --global ""

the above will create a file at ~/.gitconfig. You can run these commands again to change. (you can also edit the file directly.)

Create a Project

How to create a project?

  1. cd project_dir → cd to the directory of your source code.
  2. git init → create a .git dir in the current dir. This dir holds all git's data about the current project.
  3. git add . → add current dir (and subdir) files into git staging area.
  4. git commit -m"first commit" → commit to local repository.

Important: when using git, you should always cd the directory first. This is in contrast to traditional unix commands, where you can simply give the directory's full path.


# cd to the dir containing your code
cd my_project

# initialize. This creates a .git dir.
git init

# add all files to “staging area”
git add .

# commit to local repository.
git commit -m"my first commit"

Git will create a dir named .git in the same dir. This dir is used by git for all its info, including its database.

Note: as of , Adding 20k files takes about 6 minutes. Commiting 20k files takes 6 minutes.

Clone a Project

Clone a projet is similar to creating a git project, except it's for when there's a existing project in git on a server. You want to “copy” it and work with that project.

How to clone a project?

cd to a directory where you want the new project dir to be, then git clone path_or_url.


# get a copy of git source code (≈10MB)
git clone git://
# get a copy of the js jquery source code
git clone

git clone URL → creates a dir in the current dir with the remote repo's content. The newly created dir will have the same name as project name of the remote repository at URL. If a non-empty directory of the same name already exist, git will complain.

Note to svn users: git also has a “checkout” command. It's used for updating/reverting your current dir from a repository (a older version, or different branch, etc.). git checkout is similar to svn merge and svn revert. 〔►see svn Tutorial

Git Ignore File

Some files you want git to ignore, such as emacs backup files, compiled binary files, error logs, etc. You can set up git to ignore those files.

Git Ignore File

Commit a File

Git Tutorial: Committing Files

Push to Remote Repository

Git Tutorial: Push to Server

Pull (Get Update) from Remote Repository

Get Update from Remote Repository: git pull

View Commit History/Log

Git Tutorial: View History/Log

Revert from Local Repository

Git Tutorial: Revert Change

git diff Between {Working Dir, Staged Area, Last Commit}

Git: Diff Between {Working Dir, Staged Area, Last Commit}

git Frequently Asked Questions

git Frequently Asked Questions

How to Branch

Git Branching Tutorial

Save Working Dir in a Temp Storage

Git: Save Working Tree in a Temp Storage: git stash

Tagging Files

How to tag a version?

git tag "version number or any label"

This will tag the current state of your local repository. (not the index/“staging area”) You probably want to do a commit before you tag.

Setting up git on GitHub, Google Code, GitCafe

Git in Emacs

Emacs: magit-mode Tutorial