Git Basics

, , …,

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, on Ubuntu Linux, 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.

git Commands for New Project
Command NameMost Common Use
git initCreate a empty git repository in current dir. This will create a .git dir in current dir.
git clone urlClone the repository from url to current dir.
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.
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
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.)

Creating Project

How to create a project?

  1. cd project_dir → cd to your source code directory. (when using git, you should always cd to your source code's directory first.)
  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.


# 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.

Adding 20k files takes ≈6 minutes. Commiting 20k files takes 6 minutes.

Clone a Project

How to clone a project?

cd to a directory where you want the files to be, then git clone path or url. Examples:

# 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 dir will have the same name as the project name of the remote repository at URL.

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.

Commit a File

See: Git Tutorial: Committing Files

Pull (Get Update) from Remote Repository

See: Get Update from Remote Repository: git pull

Revert from Local Repository

How to revert a file?

git checkout -- file name

This will update the file file name in working directory. It'll be from your local repository. Effectively, revert whatever you've done to the file.

How to revert a file to a particular version?

git checkout v1.2.3 -- file name         # tag v1.2.3
git checkout stable -- file name         # stable branch
git checkout origin/master -- file name  # upstream master
git checkout HEAD -- file name           # the version from the most recent commit
git checkout HEAD^ -- file name          # the version before the most recent commit

How do I revert one file to the last commit in git? By Brian Campbell. @

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.

See: Git Ignore File

Commit History/Log

How to see last changes?

# show diff of last changes
git log -p -2 --color | cat

How to get a history of commits?

Use git log. Example of git log output:

◆ git log
commit a6205ee19e5dd0c049b7fff35de0d09189077594
Author: Xah Lee <>
Date:   Sat Dec 22 04:14:11 2012 -0800

    fixed bug 324

commit c40dc81c7d6ca3d8ad3a4b9996a5b4ed4b0c6d08
Author: Xah Lee <>
Date:   Fri Dec 21 22:29:28 2012 -0800

    changed getinput to not take parameters

Use git log -3 to show just last 3 commits.

This command is important because it shows the ID for each commit. You'll need to use the ID when doing comparisons or diff or revert.

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

See: Git: Find Difference Between {Working Dir, Staged Area, Last Commit}, or 2 Commits

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

See: Emacs: magit-mode Tutorial


see also Git from the Bottom Up

blog comments powered by Disqus