github cheat sheet

Git Cheat Sheet Chinese

索引


建立

複製一個已建立的倉庫:git

$ git clone ssh://user@domain.com/repo.git

建立一個新的本地倉庫:緩存

$ git init

本地修改

顯示工做路徑下已修改的文件:dom

$ git status

顯示與上次提交版本文件的不一樣:ssh

$ git diff

把當前全部修改添加到下次提交中:編輯器

$ git add .

把對某個文件的修改添加到下次提交中:fetch

$ git add -p <file>

提交本地的全部修改:url

$ git commit -a

提交以前已標記的變化:code

$ git commit

附加消息提交:索引

$ git commit -m 'message here'

Commit to some previous date:rem

git commit --date="`date --date='n day ago'`" -am "Commit Message"

修改上次提交

Don't amend published commits!

$ git commit --amend

把當前分支中未提交的修改移動到其餘分支

git stash
git checkout branch2
git stash pop

搜索

從當前目錄的全部文件中查找文本內容:

$ git grep "Hello"

在某一版本中搜索文本:

$ git grep "Hello" v2.5

提交歷史

從最新提交開始,顯示全部的提交記錄(顯示hash, 做者信息,提交的標題和時間):

$ git log

顯示全部提交(僅顯示提交的hash和message):

$ git log --oneline

顯示某個用戶的全部提交:

$ git log --author="username"

顯示某個文件的全部修改:

$ git log -p <file>

誰,在什麼時間,修改了文件的什麼內容:

$ git blame <file>

分支與標籤

列出全部的分支:

$ git branch

切換分支:

$ git checkout <branch>

基於當前分支建立新分支:

$ git branch <new-branch>

基於遠程分支建立新的可追溯的分支:

$ git branch --track <new-branch> <remote-branch>

刪除本地分支:

$ git branch -d <branch>

給當前版本打標籤:

$ git tag <tag-name>

更新與發佈

列出對當前遠程端的操做:

$ git remote -v

顯示遠程端的信息:

$ git remote show <remote>

添加新的遠程端:

$ git remote add <remote> <url>

下載遠程端版本,但不合併到HEAD中:

$ git fetch <remote>

下載遠程端版本,並自動與HEAD版本合併:

$ git remote pull <remote> <url>

將遠程端版本合併到本地版本中:

$ git pull origin master

將本地版本發佈到遠程端:

$ git push remote <remote> <branch>

刪除遠程端分支:

$ git push <remote> :<branch> (since Git v1.5.0)
or
git push <remote> --delete <branch> (since Git v1.7.0)

發佈標籤:

$ git push --tags

合併與重置

將分支合併到當前HEAD中:

$ git merge <branch>

將當前HEAD版本重置到分支中:

Don't rebase published commit!

$ git rebase <branch>

退出重置:

$ git rebase --abort

解決衝突後繼續重置:

$ git rebase --continue

使用配置好的merge tool 解決衝突:

$ git mergetool

在編輯器中手動解決衝突後,標記文件爲已解決衝突

$ git add <resolved-file>
$ git rm <resolved-file>

撤銷

放棄工做目錄下的全部修改:

$ git reset --hard HEAD

移除緩存區的全部文件(i.e. 撤銷上次git add):

$ git reset HEAD

放棄某個文件的全部本地修改:

$ git checkout HEAD <file>

重置一個提交(經過建立一個大相徑庭的新提交)

$ git revert <commit>

將HEAD重置到上一次提交的版本,並放棄以後的全部修改:

$ git reset --hard <commit>

將HEAD重置到上一次提交的版本,並將以後的修改標記爲未添加到緩存區的修改:

$ git reset <commit>

將HEAD重置到上一次提交的版本,並保留未提交的本地修改:

$ git reset --keep <commit>