# GitHub秘籍
本秘籍收錄了一些Git和Github非常酷同時又少有人知的功能。靈感來自于[Zach Holman](https://github.com/holman)在2012年Aloha Ruby Conference和2013年WDCNZ上所做的演講:[Git and GitHub Secrets](http://www.confreaks.com/videos/1229-aloharuby2012-git-and-github-secrets)([slides](https://speakerdeck.com/holman/git-and-github-secrets))和[More Git and GitHub Secrets](https://vimeo.com/72955426)([slides](https://speakerdeck.com/holman/more-git-and-github-secrets))。
*Read this in other languages: [English](#), [???](#), [日本語](#), [簡體中文](#).*
# 目錄
- [GitHub](#)
- [忽略空白字符變化](#)
- [調整Tab字符所代表的空格數](#)
- [查看某個用戶的Commit歷史](#)
- [克隆某個倉庫](#)
- [分支](#)
- [將某個分支與其他所有分支進行對比](#)
- [比較分支](#)
- [比較不同派生庫的分支](#)
- [Gists](#)
- [Git.io](#)
- [鍵盤快捷鍵](#)
- [整行高亮](#)
- [用commit信息關閉Issue](#)
- [鏈接其他倉庫的Issue](#)
- [設置CI對每條Pull Request都進行構建](#)
- [Markdown文件高亮語法](#)
- [表情符](#)
- [靜態與動態圖片](#)
- [在GitHub Wiki中嵌入圖片](#)
- [快速引用](#)
- [快速添加許可證](#)
- [任務列表](#)
- [Markdown文件中的任務列表](#)
- [相對鏈接](#)
- [GitHub Pages的元數據與插件支持](#)
- [查看YAML格式的元數據](#)
- [渲染表格數據](#)
- [撤銷Pull Request](#)
- [Diffs](#)
- [可渲染文檔的Diffs](#)
- [可變化地圖](#)
- [在diff中折疊與擴展代碼](#)
- [查看Pull Request的diff和patch](#)
- [渲染圖像發生的變動](#)
- [Hub](#)
- [貢獻者指南](#)
- [GitHub資源](#)
- [GitHub討論](#)
- [Git](#)
- [前一個分支](#)
- [Stripspace命令](#)
- [檢出Pull Requests](#)
- [提交空改動 :trollface:](#)
- [更直觀的Git Status](#)
- [更直觀的Git Log](#)
- [Git查詢](#)
- [合并分支](#)
- [使用網頁查看本地倉庫](#)
- [Git配置](#)
- [Git命令自定義別名](#)
- [自動更正](#)
- [帶顏色輸出](#)
- [Git資源](#)
- [Git參考書籍](#)
### GitHub
### 忽略空白字符變化
在任意diff頁面的URL后加上`?w=1`,可以去掉那些只是空白字符的變化,使你能更專注于代碼的變化。

[*詳見 GitHub secrets.*](https://github.com/blog/967-github-secrets)
### 調整Tab字符所代表的空格數
在diff或者file頁面的URL后面加上`?ts=4`,這樣當顯示tab字符的長度時就會是4個空格的長度,不再是默認的8個空格。`ts`后面的數字還可以根據你個人的偏好進行修改。不過,這個小訣竅在Gists頁面和raw file頁面不起作用。
下面是我們在Go語言的source file頁面URL后加`?ts=4`[前](https://github.com/pengwynn/flint/blob/master/flint/flint.go)的例子:

然后是我們添加`?ts=4`[后](https://github.com/pengwynn/flint/blob/master/flint/flint.go?ts=4)的例子:

### 查看某個用戶的Commit歷史
查看某個用戶的所有提交歷史,只需在commits頁面URL后加上`?author=username`。
~~~
https://github.com/rails/rails/commits/master?author=dhh
~~~

[*深入了解提交視圖之間的區別*](https://help.github.com/articles/differences-between-commit-views)
### 克隆某個倉庫
當我們克隆某一資源時,可以不要那個`.git`后綴。
~~~
$ git clone https://github.com/tiimgreen/github-cheat-sheet
~~~
[*更多對 Git `clone` 命令的介紹.*](http://git-scm.com/docs/git-clone)
### 分支
#### 將某個分支與其他所有分支進行對比
當你點擊某個倉庫的分支(Branches)選項卡時
~~~
https://github.com/{user}/{repo}/branches
~~~
你會看到一個包含所有未合并的分支的列表。
你可以在這里查看比較(Compare)頁面或點擊刪除某個分支。

有的時候我們需要將多個分支與一個非主分支(master)進行對比,此時可以通過在URL后加入要比較的分支名來實現:
~~~
https://github.com/{user}/{repo}/branches/{branch}
~~~

可以在URL后加上`?merged=1`來查看已經合并了的分支。

你可以使用這個界面來替代命令行直接刪除分支。
#### 比較分支
如果我們想要比較兩個分支,可以像下面一樣修改URL:
~~~
https://github.com/user/repo/compare/{range}
~~~
其中`{range} = master...4-1-stable`
例如:
~~~
https://github.com/rails/rails/compare/master...4-1-stable
~~~

`{range}`還可以使用下面的形式:
~~~
https://github.com/rails/rails/compare/master@{1.day.ago}...master
https://github.com/rails/rails/compare/master@{2014-10-04}...master
~~~
*日期格式 `YYYY-DD-MM`*

...這樣你就能查看master分支上一段時間或者指定日期內的改動。
[*了解更多關于比較跨時間段的提交記錄.*](https://help.github.com/articles/comparing-commits-across-time)
#### 比較不同派生庫的分支
想要對派生倉庫(Forked Repository)之間的分支進行比較,可以像下面這樣修改URL實現:
~~~
https://github.com/user/repo/compare/{foreign-user}:{branch}...{own-branch}
~~~
例如:
~~~
https://github.com/rails/rails/compare/byroot:master...master
~~~

### Gists
[Gists](https://gist.github.com/) 給我們提供了一種不需要創建一個完整的倉庫,使小段代碼也可以工作的簡單方式。

Gist的URL后加上`.pibb`,可以得到更適合嵌入到其他網站的HTML版本。
Gists還可以像任何標準倉庫一樣被克隆。
~~~
$ git clone https://gist.github.com/tiimgreen/10545817
~~~

這意味著你可以像 Github 倉庫一樣修改和更新 Gists :
~~~
$ git commit
$ Username for 'https://gist.github.com':
$ Password for 'https://tiimgreen@gist.github.com':
~~~
[*進一步了解如何創建 gists.*](https://help.github.com/articles/creating-gists)
### Git.io
[Git.io](http://git.io)是Github的短網址服務。

你可以通過Curl命令以普通HTTP協議使用它:
~~~
$ curl -i http://git.io -F "url=https://github.com/..."
HTTP/1.1 201 Created
Location: http://git.io/abc123
$ curl -i http://git.io/abc123
HTTP/1.1 302 Found
Location: https://github.com/...
~~~
[*進一步了解 Git.io.*](https://github.com/blog/985-git-io-github-url-shortener)
### 鍵盤快捷鍵
在倉庫主頁上提供了快捷鍵方便快速導航。
- 按 `t` 鍵會打開一個文件瀏覽器。
- 按 `w` 鍵會打開分支選擇菜單。
- 按 `s` 鍵會激活頂端的命令欄 (Command Bar)。
- 按 `l` 鍵編輯Issue列表頁的標簽。
- **查看文件內容時**(如:`https://github.com/tiimgreen/github-cheat-sheet/blob/master/README.md`),按 `y` 鍵將會凍結這個頁面,這樣就算代碼被修改了也不會影響你當前看到的。
按`?`查看當前頁面支持的快捷鍵列表:

[*進一步了解如何使用 Command Bar.*](https://help.github.com/articles/using-the-command-bar)
### 整行高亮
在代碼文件地址后加上`#L52`或者單擊行號52都會將第52行代碼高亮顯示。
多行高亮也可以,比如用`#L53-L60`選擇范圍,或者按住 `shift`鍵,然后再點擊選擇的兩行。
~~~
https://github.com/rails/rails/blob/master/activemodel/lib/active_model.rb#L53-L60
~~~

### 用commit信息關閉Issue
如果某個提交修復了一個Issue,當提交到master分支時,提交信息里可以使用`fix/fixes/fixed`, `close/closes/closed` 或者 `resolve/resolves/resolved`等關鍵詞,后面再跟上Issue號,這樣就會關閉這個Issue。
~~~
$ git commit -m "Fix screwup, fixes #12"
~~~
這將會關閉Issue #12,并且在Issue討論列表里關聯引用這次提交。

[*進一步了解通過提交信息關閉Issue.*](https://help.github.com/articles/closing-issues-via-commit-messages)
### 鏈接其他倉庫的Issue
如果你想引用到同一個倉庫中的一個Issue,只需使用井號 `#` 加上Issue號,這樣就會自動創建到此Issue的鏈接。
要鏈接到其他倉庫的Issue,就使用`user_name/repo_name#ISSUE_NUMBER`的方式,例如`tiimgreen/toc#12`。

### 設置CI對每條Pull Request都進行構建
如果配置正確,[Travis CI](https://travis-ci.org/)會為每個你收到的Pull Request執行構建,就像每次提交也會觸發構建一樣。想了解更多關于Travis CI的信息,請看 [Travis CI入門](http://docs.travis-ci.com/user/getting-started/)。
[](https://github.com/octokit/octokit.rb/pull/452)
[*進一步了解 Commit status API.*](https://github.com/blog/1227-commit-status-api)
### Markdown文件高亮語法
例如,可以像下面這樣在你的Markdown文件里為Ruby代碼添加語法高亮:
~~~
```ruby
require 'tabbit'
table = Tabbit.new('Name', 'Email')
table.add_row('Tim Green', 'tiimgreen@gmail.com')
puts table.to_s
```
~~~
效果像下面這樣:
~~~
require 'tabbit'
table = Tabbit.new('Name', 'Email')
table.add_row('Tim Green', 'tiimgreen@gmail.com')
puts table.to_s
~~~
Github使用 [Linguist](https://github.com/github/linguist) 做語言識別和語法高亮。你可以仔細閱讀 [languages YAML file](https://github.com/github/linguist/blob/master/lib/linguist/languages.yml),了解有哪些可用的關鍵字。
[*進一步了解 GitHub Flavored Markdown.*](https://help.github.com/articles/github-flavored-markdown)
### 表情符
可以在Pull Requests, Issues, 提交消息, Markdown文件里加入表情符。使用方法`:name_of_emoji:`
~~~
:smile:
~~~
將輸出一個笑臉:
:smile:
Github支持的完整表情符號列表詳見[emoji-cheat-sheet.com](http://www.emoji-cheat-sheet.com/) 或 [scotch-io/All-Github-Emoji-Icons](https://github.com/scotch-io/All-Github-Emoji-Icons)。
Github上使用最多的5個表情符號是:
1. :shipit: - `:shipit:`
1. :sparkles: - `:sparkles:`
1. :-1: - `:-1:`
1. :+1: - `:+1:`
1. :clap: - `:clap:`
### 靜態與動態圖片
注釋和README等文件里也可以使用圖片和GIF動畫:
~~~

~~~
倉庫中的原始圖片可以被直接調用:
~~~
/(repo)/raw/master/path/to/image.gif)
~~~

所有圖片都緩存在Github,不用擔心你的站點不能訪問時就看不到圖片了。
#### 在GitHub Wiki中嵌入圖片
有多種方法可以在Wiki頁面里嵌入圖片。既可以像上一條里那樣使用標準的Markdown語法,也可以像下面這樣指定圖片的高度或寬度:
~~~
[[ http://www.sheawong.com/wp-content/uploads/2013/08/keephatin.gif | height = 100px ]]
~~~
結果:

### 快速引用
在主題評論中引用之前某個人所說的,只需選中文本,然后按 `r`鍵,想要的就會以引用的形式復制到你的輸入框里。

[*進一步了解快速引用.*](https://github.com/blog/1399-quick-quotes)
### 快速添加許可證
創建一個倉庫時,Github會為你提供一個預置的軟件許可列表:

對于已有的倉庫,可以通過web界面創建文件來添加軟件許可。輸入`LICENSE`作為文件名后,同樣可以從預置的列表中選擇一個作為模板。

這個技巧也適用于 `.gitignore` 文件。
[*進一步了解 open source licensing.*](https://help.github.com/articles/open-source-licensing)
### 任務列表
Issues和Pull requests里可以添加復選框,語法如下(注意空白符):
~~~
- [ ] Be awesome
- [ ] Prepare dinner
- [ ] Research recipe
- [ ] Buy ingredients
- [ ] Cook recipe
- [ ] Sleep
~~~

當項目被選中時,它對應的Markdown源碼也被更新了:
~~~
- [x] Be awesome
- [ ] Prepare dinner
- [x] Research recipe
- [x] Buy ingredients
- [ ] Cook recipe
- [ ] Sleep
~~~
[*進一步了解任務列表.*](https://help.github.com/articles/writing-on-github#task-lists)
#### Markdown文件中的任務列表
在完全適配Markdown語法的文件中可以使用以下語法加入一個**只讀**的任務列表
~~~
- [ ] Mercury
- [x] Venus
- [x] Earth
- [x] Moon
- [x] Mars
- [ ] Deimos
- [ ] Phobos
~~~
- [ ] Mercury
- [x] Venus
- [x] Earth
- [x] Moon
- [x] Mars
- [ ] Deimos
- [ ] Phobos
[*進一步了解Markdown文件中的任務列表*](https://github.com/blog/1825-task-lists-in-all-markdown-documents)
### 相對鏈接
Markdown文件里鏈接到內部內容時推薦使用相對鏈接。
~~~
[Link to a header](#awesome-section)
[Link to a file](docs/readme)
~~~
絕對鏈接會在URL改變時(例如重命名倉庫、用戶名改變,建立分支項目)被更新。使用相對鏈接能夠保證你的文檔不受此影響。
[*進一步了解相對鏈接.*](https://help.github.com/articles/relative-links-in-readmes)
### GitHub Pages的元數據與插件支持
在Jekyll頁面和文章里,倉庫信息可在 `site.github` 命名空間下找到,也可以顯示出來,例如,使用 `{{ site.github.project_title }}`顯示項目標題。
Jemoji和jekyll-mentions插件為你的Jekyll文章和頁面增加了[emoji](#)和[@mentions](https://github.com/blog/821)功能。
[*了解更多 GitHub Pages的元數據和插件支持.*](https://github.com/blog/1797-repository-metadata-and-plugin-support-for-github-pages)
### 查看YAML格式的元數據
許多博客站點,比如基于[Jekyll](http://jekyllrb.com/)的[GitHub Pages](http://pages.github.com/),都依賴于一些文章頭部的YAML格式的元數據。Github會將其渲染成一個水平表格,方便閱讀。

[*進一步了解 在文檔里查看YAML元數據.*](https://github.com/blog/1647-viewing-yaml-metadata-in-your-documents)
### 渲染表格數據
GitHub支持將 `.csv` (逗號分隔)和`.tsv` (制表符分隔)格式的文件渲染成表格數據。

[*進一步了解渲染表格數據.*](https://github.com/blog/1601-see-your-csvs)
### 撤銷Pull Request
可以通過Pull Request中的Revert按鈕來撤銷一個已合并的Pull Request中的commit。按下按鈕后會自動生成一個進行逆向操作的Pull Request。

[*進一步了解“撤銷”按鈕](https://github.com/blog/1857-introducing-the-revert-button)
### Diffs
#### 可渲染文檔的Diffs
提交和Pull Requests里包含有Github支持的可渲染文檔(比如Markdown)會提供*source* 和 *rendered* 兩個視圖功能。

點擊 "rendered" 按鈕,看看改動在渲染后的顯示效果。當你添加、刪除或修改文本時,渲染純文本視圖非常方便。

[*進一步了解渲染純文本視圖Diffs.*](https://github.com/blog/1784-rendered-prose-diffs)
#### 可變化地圖
當你在GitHub上查看一個包含地理數據的提交或pull request時,Github 將以可視化的方式對比版本之間的差異。
[](https://github.com/benbalter/congressional-districts/commit/2233c76ca5bb059582d796f053775d8859198ec5)
[*進一步了解可比較地圖.*](https://github.com/blog/1772-diffable-more-customizable-maps)
#### 在diff中折疊與擴展代碼
你可以通過點擊diff邊欄里的 *unfold* 按鈕來多顯示幾行上下文。你可以一直點擊 *unfold* 按鈕直到顯示了文件的全部內容。這個功能在所有GitHub產生的diff界面都可以使用。

[*進一步了解擴展Diff上下文.*](https://github.com/blog/1705-expanding-context-in-diffs)
#### 查看Pull Request的diff和patch
在Pull Request的URL后面加上 `.diff` 或 `.patch` 的擴展名就可以得到它的diff或patch文件,例如:
~~~
https://github.com/tiimgreen/github-cheat-sheet/pull/15
https://github.com/tiimgreen/github-cheat-sheet/pull/15.diff
https://github.com/tiimgreen/github-cheat-sheet/pull/15.patch
~~~
`.diff` 擴展會使用普通文本格式顯示如下內容:
~~~
diff --git a/README.md b/README.md
index 88fcf69..8614873 100644
--- a/README.md
+++ b/README.md
@@ -28,6 +28,7 @@ All the hidden and not hidden features of Git and GitHub. This cheat sheet was i
- [Merged Branches](#merged-branches)
- [Quick Licensing](#quick-licensing)
- [TODO Lists](#todo-lists)
+- [Relative Links](#relative-links)
- [.gitconfig Recommendations](#gitconfig-recommendations)
- [Aliases](#aliases)
- [Auto-correct](#auto-correct)
@@ -381,6 +382,19 @@ When they are clicked, they will be updated in the pure Markdown:
- [ ] Sleep
(...)
~~~
#### 渲染圖像發生的變動
GitHub可以顯示包括PNG、JPG、GIF、PSD在內的多種圖片格式并提供了幾種方式來比較這些格式的圖片文件版本間的不同。
[](https://github.com/blog/1845-psd-viewing-diffing)
[*查看更多關于渲染圖像變動的內容*](https://help.github.com/articles/rendering-and-diffing-images)
### Hub
[Hub](https://github.com/github/hub)是一個對Git進行了封裝的命令行工具,可以幫助你更方便的使用Github。
這使得你可以像下面這樣進行克隆:
~~~
$ hub clone tiimgreen/toc
~~~
[*查看更多Hub提供的超酷命令.*](https://github.com/github/hub#commands)
### 貢獻者指南
在你的倉庫的根目錄添加一個名為 `CONTRIBUTING` 的文件后,貢獻者在新建Issue或Pull Request時會看到這個文件的鏈接。

[*進一步了解貢獻者指南.*](https://github.com/blog/1184-contributing-guidelines)
### GitHub資源
| Title | Link |
|-----|-----|
| GitHub Explore | [https://github.com/explore](https://github.com/explore) |
| GitHub Blog | [https://github.com/blog](https://github.com/blog) |
| GitHub Help | [https://help.github.com/](https://help.github.com/) |
| GitHub Training | [http://training.github.com/](http://training.github.com/) |
| GitHub Developer | [https://developer.github.com/](https://developer.github.com/) |
#### GitHub討論
| Title | Link |
|-----|-----|
| How GitHub Uses GitHub to Build GitHub | [https://www.youtube.com/watch?v=qyz3jkOBbQY](https://www.youtube.com/watch?v=qyz3jkOBbQY) |
| Introduction to Git with Scott Chacon of GitHub | [https://www.youtube.com/watch?v=ZDR433b0HJY](https://www.youtube.com/watch?v=ZDR433b0HJY) |
| How GitHub No Longer Works | [https://www.youtube.com/watch?v=gXD1ITW7iZI](https://www.youtube.com/watch?v=gXD1ITW7iZI) |
| Git and GitHub Secrets | [https://www.youtube.com/watch?v=Foz9yvMkvlA](https://www.youtube.com/watch?v=Foz9yvMkvlA) |
| More Git and GitHub Secrets | [https://www.youtube.com/watch?v=p50xsL-iVgU](https://www.youtube.com/watch?v=p50xsL-iVgU) |
### Git
### 前一個分支
快速檢出上一個分支:
~~~
$ git checkout -
# Switched to branch 'master'
$ git checkout -
# Switched to branch 'next'
$ git checkout -
# Switched to branch 'master'
~~~
[*進一步了解 Git 分支.*](http://git-scm.com/book/en/Git-Branching-Basic-Branching-and-Merging)
### Stripspace命令
Git Stripspace命令可以:
- 去掉行尾空白符
- 多個空行壓縮成一行
- 必要時在文件末尾增加一個空行
使用此命令時必須傳入一個文件,像這樣:
~~~
$ git stripspace < README.md
~~~
[*進一步了解 Git `stripspace` 命令.*](http://git-scm.com/docs/git-stripspace)
### 檢出Pull Requests
Pull Request是一種GitHub上可以通過以下多種方式在本地被檢索的特別分支:
檢索某個分支并臨時儲存在本地的`FETCH_HEAD`中以便快速查看更改(diff)以及合并(merge):
~~~
$ git fetch origin refs/pull/[PR-Number]/head
~~~
通過refspec獲取所有的Pull Request為本地分支:
~~~
$ git fetch origin '+refs/pull/*/head:refs/remotes/origin/pr/*'
~~~
或在倉庫的`.git/config`中加入下列設置來自動獲取遠程倉庫中的Pull Request
~~~
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = git@github.com:tiimgreen/github-cheat-sheet.git
~~~
~~~
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = git@github.com:tiimgreen/github-cheat-sheet.git
fetch = +refs/pull/*/head:refs/remotes/origin/pr/*
~~~
對基于派生庫的Pull Request,可以通過先`checkout`代表此Pull Request的遠端分支再由此分支建立一個本地分支:
~~~
$ git checkout pr/42 pr-42
~~~
操作多個倉庫的時候,可以在Git中設置獲取Pull Request的全局選項。
~~~
git config --global --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*"
~~~
此時可以在任意倉庫中使用以下命令:
~~~
git fetch origin
~~~
~~~
git checkout pr/42
~~~
[*進一步了解如何檢出pull request到本地.*](https://help.github.com/articles/checking-out-pull-requests-locally)
### 提交空改動 :trollface:
可以使用`--allow-empty`選項強制創建一個沒有任何改動的提交:
~~~
$ git commit -m "Big-ass commit" --allow-empty
~~~
這樣做在如下幾種情況下是有意義的:
- 標記新的工作或一個新功能的開始。
- 記錄對項目的跟代碼無關的改動。
- 跟使用你倉庫的其他人交流。
- 作為倉庫的第一次提交,因為第一次提交后不能被rebase: `git commit -m "init repo" --allow-empty`.
### 更直觀的Git Status
在命令行輸入如下命令:
~~~
$ git status
~~~
可以看到:

加上`-sb`選項:
~~~
$ git status -sb
~~~
這會得到:

[*進一步了解 Git `status` 命令.*](http://git-scm.com/docs/git-status)
### 更直觀的Git Log
輸入如下命令:
~~~
$ git log --all --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
~~~
可以看到:

這要歸功于[Palesz](http://stackoverflow.com/users/88355/palesz)在stackoverflow的回答。
*這個命令可以被用作別名,詳細做法見[這里](https://github.com/tiimgreen/github-cheat-sheet#aliases)。*
[*進一步了解 Git `log` 命令.*](http://git-scm.com/docs/git-log)
### Git查詢
Git查詢運行你在之前的所有提交信息里進行搜索,找到其中和搜索條件相匹配的最近的一條。
~~~
$ git show :/query
~~~
這里 `query` (區別大小寫)是你想要搜索的詞語, 這條命令會找到包含這個詞語的最后那個提交并顯示變動詳情。
~~~
$ git show :/typo
~~~

- 按 `q` 鍵退出命令。*
### 合并分支
輸入命令:
~~~
$ git branch --merged
~~~
這會顯示所有已經合并到你當前分支的分支列表。
相反地:
~~~
$ git branch --no-merged
~~~
會顯示所有還沒有合并到你當前分支的分支列表。
[*進一步了解 Git `branch` 命令.*](http://git-scm.com/docs/git-branch)
### 使用網頁查看本地倉庫
使用Git的 `instaweb` 可以立即在 `gitweb`中瀏覽你的工作倉庫。這個命令是個簡單的腳本,配置了`gitweb`和用來瀏覽本地倉庫的Web服務器。*(譯者注:默認需要lighttpd支持)*
~~~
$ git instaweb
~~~
執行后打開:

[*進一步了解 Git `instaweb` 命令.*](http://git-scm.com/docs/git-instaweb)
### Git配置
所有Git配置都保存在你的`.gitconfig` 文件中。
#### Git命令自定義別名
別名用來幫助你定義自己的git命令。比如你可以定義 `git a` 來運行 `git add --all`。
要添加一個別名, 一種方法是打開 `~/.gitconfig` 文件并添加如下內容:
~~~
[alias]
co = checkout
cm = commit
p = push
# Show verbose output about tags, branches or remotes
tags = tag -l
branches = branch -a
remotes = remote -v
~~~
...或者在命令行里鍵入:
~~~
$ git config --global alias.new_alias git_function
~~~
例如:
~~~
$ git config --global alias.cm commit
~~~
指向多個命令的別名可以用引號來定義:
~~~
$ git config --global alias.ac 'add -A . && commit'
~~~
下面列出了一些有用的別名:
| 別名 Alias | 命令 Command | 如何設置 What to Type |
|-----|-----|-----|
| `git cm` | `git commit` | `git config --global alias.cm commit` |
| `git co` | `git checkout` | `git config --global alias.co checkout` |
| `git ac` | `git add . -A``git commit` | `git config --global alias.ac '!git add -A && git commit'` |
| `git st` | `git status -sb` | `git config --global alias.st 'status -sb'` |
| `git tags` | `git tag -l` | `git config --global alias.tags 'tag -l'` |
| `git branches` | `git branch -a` | `git config --global alias.branches 'branch -a'` |
| `git remotes` | `git remote -v` | `git config --global alias.remotes 'remote -v'` |
| `git lg` | `git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --` | `git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --"` |
#### 自動更正
如果鍵入 `git comit` 你會看到如下輸出:
~~~
$ git comit -m "Message"
# git: 'comit' is not a git command. See 'git --help'.
# Did you mean this?
# commit
~~~
為了在鍵入 `comit` 調用 `commit`命令,只需啟用自動糾錯功能:
~~~
$ git config --global help.autocorrect 1
~~~
現在你就會看到:
~~~
$ git comit -m "Message"
# WARNING: You called a Git command named 'comit', which does not exist.
# Continuing under the assumption that you meant 'commit'
# in 0.1 seconds automatically...
~~~
#### 帶顏色輸出
要在你的Git命令輸出里加上顏色的話,可以用如下命令:
~~~
$ git config --global color.ui 1
~~~
[*進一步了解 Git `config` 命令.*](http://git-scm.com/docs/git-config)
### Git資源
| Title | Link |
|-----|-----|
| Official Git Site | [http://git-scm.com/](http://git-scm.com/) |
| Official Git Video Tutorials | [http://git-scm.com/videos](http://git-scm.com/videos) |
| Code School Try Git | [http://try.github.com/](http://try.github.com/) |
| Introductory Reference & Tutorial for Git | [http://gitref.org/](http://gitref.org/) |
| Official Git Tutorial | [http://git-scm.com/docs/gittutorial](http://git-scm.com/docs/gittutorial) |
| Everyday Git | [http://git-scm.com/docs/everyday](http://git-scm.com/docs/everyday) |
| Git Immersion | [http://gitimmersion.com/](http://gitimmersion.com/) |
| Ry's Git Tutorial | [http://rypress.com/tutorials/git/index.html](http://rypress.com/tutorials/git/index.html) |
| Git for Designer | [http://hoth.entp.com/output/git_for_designers.html](http://hoth.entp.com/output/git_for_designers.html) |
| Git for Computer Scientists | [http://eagain.net/articles/git-for-computer-scientists/](http://eagain.net/articles/git-for-computer-scientists/) |
| Git Magic | [http://www-cs-students.stanford.edu/~blynn/gitmagic/](http://www-cs-students.stanford.edu/~blynn/gitmagic/) |
#### Git參考書籍
| Title | Link |
|-----|-----|
| Pragmatic Version Control Using Git | [http://www.pragprog.com/titles/tsgit/pragmatic-version-control-using-git](http://www.pragprog.com/titles/tsgit/pragmatic-version-control-using-git) |
| Pro Git | [http://git-scm.com/book](http://git-scm.com/book) |
| Git Internals Peepcode | [http://peepcode.com/products/git-internals-pdf](http://peepcode.com/products/git-internals-pdf) |
| Git in the Trenches | [http://cbx33.github.com/gitt/](http://cbx33.github.com/gitt/) |
| Version Control with Git | [http://www.amazon.com/Version-Control-Git-collaborative-development/dp/1449316387](http://www.amazon.com/Version-Control-Git-collaborative-development/dp/1449316387) |
| Pragmatic Guide to Git | [http://www.pragprog.com/titles/pg_git/pragmatic-guide-to-git](http://www.pragprog.com/titles/pg_git/pragmatic-guide-to-git) |
| Git: Version Control for Everyone | [http://www.packtpub.com/git-version-control-for-everyone/book](http://www.packtpub.com/git-version-control-for-everyone/book) |
- 介紹
- 程序員基礎知識
- 字符編碼
- 技術名詞
- 語義化版本
- 命名規范
- 書寫文檔
- 開源協議
- 目錄結構
- 正則表達式
- 平凡之路
- 數據結構與算法
- 堆和棧
- 浮點數類型
- XML和JSON
- 算法學習之路
- 排序算法
- 代碼架構
- 設計模式
- 常用的Javascript設計模式
- 面向對象編程
- 繼承
- 多態
- 封裝
- 面向接口編程
- 代碼評審
- 六種量化你代碼的方式
- 程序員必備的代碼審查(Code Review)清單
- 服務器部署
- AWS簡介
- 網絡知識
- HTTPS, SPDY和 HTTP/2性能的簡單對比
- HTTP狀態碼
- 懂點設計
- 佳作賞析
- 無縫平鋪
- Sketch學習
- 設計與實現的平衡
- 寫點東西
- 使用gitbook
- 合格的PM
- 一個好的產品經理
- 產品經理的技能
- 團隊合作
- 關于招聘
- 培訓新人
- 領導能力
- 獲取知識
- MOOC
- Podcasts
- 英語學習
- 設計學習
- 前端學習
- iOS學習
- 游戲開發
- 關注健康
- 過勞檢測
- 關于睡眠
- 提升效率
- 學會閱讀
- 學會提問
- 善用搜索
- 學會寫作
- 時間管理
- 知識管理
- 文件管理
- 密碼管理
- 制作視頻
- 制作PPT
- 論音樂對效率的影響
- 程序員效率指南
- SOHO
- 創業資源
- Hacker
- 保護隱私
- 關于工作
- 找工作前需要思考的問題
- 原則與技巧
- 關于簡歷
- 其他方面
- 硬件相關
- 常用軟件
- Windows
- 硬件配置
- 系統安裝
- 常用軟件
- Mac
- 通用設置
- 權限問題
- alias設置
- 常用軟件
- 開發環境
- 快捷鍵設置
- 常用終端命令
- dotfiles
- Android
- 常用軟件
- 如何登錄美國區GooglePlay
- 開發工具
- git
- EditorConfig
- node
- shadowsocks
- ST3--Windows篇
- ST3--Mac篇
- gulp
- 字體的選擇
- Emacs
- WebStorm
- tmux
- Sketch
- Sketch中文學習資料
- Trello
- 使用Trello管理項目的經驗
- git進階
- 15分鐘學會使用Git和遠程代碼庫
- GitHub秘籍
- JetBrains
- IDE設置
- 附錄
- 計算機科學與技術
- 網站
- 書籍
- 工具