Git Tutorial: Practical Git in 1 Hour

, , …,

This page is beginner tutorial of using the git. Git is a version control system. This tutorial focus on the most important commands that you need to use every day. I assume you have basic experience with Linux shell/bash. 〔☛ Linux Tutorial by Example: Most Frequently Used Shell Commands

git-logo

If you don't have git installed, on Ubuntu Linux, do sudo apt-get install git.

Then, you should setup a default name and email. It'll be used as info when you commit.

# set up default name and email
git config --global user.name "Mary Lee"
git config --global user.email "mary@example.com"

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

Basic Git Commands

What commands are available?

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

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 (like svn “checkout”) 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.

Creating Project

How to create a project?

cd to your source code dir, then:

  1. git init → create a 〔.git〕 dir in the current dir. This dir holds all git's data.
  2. git add . → add current dir (and subdir) files into git staging area.
  3. git commit -m"first commit" → commit to local repository.

Example:

# 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 (similar to svn's “checkout”)

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://git.kernel.org/pub/scm/git/git.git
# get a copy of ErgoEmacs source code (≈18MB)
git clone https://code.google.com/p/ergoemacs/

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. @ stackoverflow.com…

Git Ignore File

You can set git to ignore some files, such as emacs backup files, compiled files.

Each git project dir top level should have a ignore file file named .gitignore. This file contains file name patterns that git should ignore (⁖ emacs backup files, compiled files, ….)

Here's a sample content of git ignore file:

#-------------------
# emacs
*~
*.elc
[#]*[#]

#-------------------
# compiled files
*.com
*.class
*.dll
*.exe
*.o
*.so

#-------------------
# packed files
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

#-------------------
# Logs and databases
*.log
*.sql
*.sqlite

#-------------------
# OS junk
.DS_Store
._*
.Spotlight-V100
.Trashes
Thumbs.db

For detail, see git help gitignore.

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 <xah@xahlee.org>
Date:   Sat Dec 22 04:14:11 2012 -0800

    fixed bug 324

commit c40dc81c7d6ca3d8ad3a4b9996a5b4ed4b0c6d08
Author: Xah Lee <xah@xahlee.org>
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

Reference

see also Git from the Bottom Up

blog comments powered by Disqus