Git Basics

Git是Linus Torvalds领导开发的分布式版本管理系统,也是世界上最流行的分布式版本管理系统。关于Git,这里有一本非常好的学习资料[中文版][progit-chinese]和[英文版][progit-english]。下面是最常用的一些命令。

Init

1
2
3
4
5
# init a repository with working tree
git init

# init a repository without working tree
git init --bare

Clone

1
2
3
4
5
# clone from an existing repository
git clone <uri>

# clone from an existing repository and rename it to 'repo-name'
git clone <uri> <repo-name>

Add

1
2
3
4
5
6
7
8
# add a file 'file' to staging area
git add <file>

# add all files under directory 'dir' to staging area
git add <dir>/*

# recursively add all files under current diretory to staging area
git add .

Status

1
2
3

# get the status of current working tree
git status

Commit

1
2
3
4
5
# commit changes of the working tree in the staging area to local repository
git commit -m "<some comments>"

# reset the last commit and apply changes of the staging area and then commit it again
git commit --amend

Log

1
2
3
4
5
6
7
8
# show commit history
git log

# show last <num> history
git log -<num>

# show branching graph with history
git log --graph

Remove

1
2
# remove file from working tree and mark it as removed in staging area
git rm <file>

Reset

1
2
# reverse to 'git add <file>', move the changes of 'file' out of staging area
git reset <file>

Revert

1
2
3
4
5
# drop all changes of 'file' after last commit
git checkout -- <file>
```

## Branch

create a branch ‘new-branch’

git branch

switch to a branch ‘branch’

git checkout

create and switch to a branch ‘new-branch’

git checkout -b

1
2

## Merge

merge branch ‘other-branch’ to current branch

git merge

1
2

## Stash

push the current staging area to a stack

git stash

list the stack

git stash list

pop the top of stack

git stash pop

get the ‘num’th item in the stack and remove it from the stack

git stash pop stash@{}

1
2

## Tag

set a simple tag ‘tag-name’

git tag

set a annotated tag ‘tag-name’

git tag -a -m “

show all tags

git tag

delete a tag

git tag -d

1
2

## Remote

add a remote repository, and set its name ‘remote-repo’

git remote add

remove a remote repository ‘remote-repo’

git remote rm

show remote repositories

git remote

show detailed information of remote repositories

git remote -v

rename a remote name

git remote rename

set local branch ‘local-branch’ track remote branch ‘remote-branch’

git checkout –track /

create a new local branch and track remote branch ‘remote-branch’

git checkout -b /

1
2

## Push

push local repository branch ‘local-branch’ to remote repository ‘remote-repo’ branch ‘remote-branch’

git push :

if the local branch and remote branch are of the same name ‘branch-name’

git push

delete a remote branch ‘remote-branch’

git push :

1
2

## Pull

fetch content of ‘remote’ and merge it with current branch

git pull


[progit-chinese]: http://git-scm.com/book/zh/
[progit-english]: http://git-scm.com/book