10/28/2015 - 3:12 AM

Git Basics

Git Basics

Setting up and starting

  • Create a new local repo: git init
  • Clone a repo from github git clone [URL]

##Basic Snapshotting

check status: git status -s (-s is for short)

use git diff

1. `git diff` shows the patch (unstaged changes since last commit) 
2. `git diff --cached` only shows the staged change since last commit (staged changes)
3. `git diff HEAD` show both staged and unstaged changes
4. `git diff --stat [other opts]` show stats and summary of changes


1.`git commit -am "message"`  automatically stage all __tracked & modified__ files before commit 


git reset HEAD -- [files] to unstage files from cache and pointer to HEAD


$ git status -s
 M hello.rb
$ git add .
$ git status -s
M  hello.rb
$ git reset HEAD -- hello.rb 
Unstaged changes after reset:
M hello.rb
$ git status -s
 M hello.rb

  • Recipe for unstage:

If you want to be able to just run git unstage, you can easily setup an alias in Git. Just run git config --global alias.unstage "reset HEAD". Once you have run that, you can then just run git unstage [file] instead.

git reset --soft

The first thing git reset does is undo the last commit and put the touched files back onto the stage. for example, git reset --soft HEAD~ (the parent of the HEAD) the last commit will be undone and the files touched will be back on the stage again.

$ git status -s
M  hello.rb
$ git commit -am 'hello with a flower'
[master 5857ac1] hello with a flower
 1 files changed, 3 insertions(+), 1 deletions(-)
$ git status
# On branch master
nothing to commit (working directory clean)
$ git reset --soft HEAD~
$ git status -s
M  hello.rb

git reset --hard [HEAD or commit #]

it resets your staging area and working directory to the state they were in at the given commit. This is the most dangerous option and is not working directory safe. Any changes not committed will be lost.

git checkout -- <filename> used for syncing between working directory and staging area

replace your local changes; replace your local(working dir) changes to the last commit snapshot. Changes already added to the index are kept.

Summary: use git checkout -- <fn> for syncing with current cache, use git reset HEAD -- <fn> to unstage local changes, use git reset --hard HEAD~|commit# to wind back to a given commit.


git fetch origin
git reset --hard origin/master

在Git中,总是有后悔药可以吃的。当你用$ git reset --hard HEAD^回退到add distributed版本时,再想恢复到append GPL,就必须找到append GPL的commit id。Git提供了一个命令git reflog用来记录你的每一次命令:

$ git reflog
ea34578 HEAD@{0}: reset: moving to HEAD^
3628164 HEAD@{1}: commit: append GPL
ea34578 HEAD@{2}: commit: add distributed
cb926e7 HEAD@{3}: commit (initial): wrote a readme file

Branching (context switch) and Merging

git branch

see all branches

git branch [NAME]

creates a new branch by NAME

git branch -v

See the last commit of branches

git branch -d [NAME]

detele a branch

git checkout [-b] [BRANCH]

context switch, -b options creates a new branch and switch to it immediately


merge dev branch with master branch:

$git checkout master
$git merge dev 

git log --graph命令可以看到分支合并图。

Sharing and Remote Repos

In a nutshell you can update your project with git fetch and share your changes with git push. You can manage your remote repositories with git remote.

git remote -v

List your remote aliases

git remote add [alias] [url]

add a new repo to your project


git log --follow [file] # 显示某个文件的版本历史,包括文件改名