GitHub祕籍,爲你解讀Git與Github酷而少知的功能

GitHub

忽略空白字符變化

在任意diff頁面的URL後加上?w=1,能夠去掉那些只是空白字符的變化,使你能更專一於代碼的變化。html

Diff without whitespace

詳見 GitHub secrets.jquery

調整Tab字符所表明的空格數

在diff或者file頁面的URL後面加上?ts=4,這樣當顯示tab字符的長度時就會是4個空格的長度,再也不是默認的8個空格。ts後面的數字還能夠根據你我的的偏好進行修改。不過,這個小訣竅在Gists頁面和raw file頁面不起做用。git

下面是咱們在Go語言的source file頁面URL後加?ts=4的例子:github

Before, tab space example

而後是咱們添加?ts=4的例子:web

After, tab space example

查看某個用戶的Commit歷史

查看某個用戶的全部提交歷史,只需在commits頁面URL後加上?author=username。c#

DHH commit history

深刻了解提交視圖之間的區別api

克隆某個倉庫

當咱們克隆某一資源時,能夠不要那個.git後綴。瀏覽器

更多對 Git clone 命令的介紹.緩存

分支

將某個分支與其餘全部分支進行對比

當你點擊某個倉庫的分支(Branches)選項卡時ruby


1
https://github.com/{user}/{repo}/branches

你會看到一個包含全部未合併的分支的列表。

你能夠在這裏查看比較(Compare)頁面或點擊刪除某個分支。

Compare branches not merged into master in jquery/jquery repo - https://github.com/jquery/jquery/branches

有的時候咱們須要將多個分支與一個非主分支(master)進行對比,此時能夠經過在URL後加入要比較的分支名來實現:


1
https://github.com/{user}/{repo}/branches/{branch}

Compare branches not merged into

能夠在URL後加上?merged=1來查看已經合併了的分支。

Compare branches merged in to

你可使用這個界面來替代命令行直接刪除分支。

 

比較分支

若是咱們想要比較兩個分支,能夠像下面同樣修改URL:

其中{range} = master…4-1-stable

例如:

Rails branch compare example

{range}還可使用下面的形式:

日期格式 YYYY-DD-MM

Another compare example

…這樣你就能查看master分支上一段時間或者指定日期內的改動。

瞭解更多關於比較跨時間段的提交記錄.

 

比較不一樣派生庫的分支

想要對派生倉庫(Forked Repository)之間的分支進行比較,能夠像下面這樣修改URL實現:


1
https://github.com/user/repo/compare/{foreign-user}:{branch}...{own-branch}

例如:

Forked branch compare

Gists

Gists 給咱們提供了一種不須要建立一個完整的倉庫,使小段代碼也能夠工做的簡單方式。

Gist

Gist的URL後加上.pibb,能夠獲得更適合嵌入到其餘網站的HTML版本。

Gists還能夠像任何標準倉庫同樣被克隆。

Gists

This means you can also modify and update Gists:


1
2
3
$ git commit
$ Username for ' https://gist.github.com':

進一步瞭解如何建立 gists.

Git.io

Git.io是Github的短網址服務。

Git.io

你能夠經過Curl命令以普通HTTP協議使用它:


1
2
3
4
5
6
7
$ 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.

鍵盤快捷鍵

在倉庫主頁上提供了快捷鍵方便快速導航。

  • 按 t 鍵會打開一個文件瀏覽器。
  • 按 w 鍵會打開分支選擇菜單。
  • 按 s 鍵會激活頂端的命令欄 (Command Bar)。
  • 按 l 鍵編輯Issue列表頁的標籤。
  • 查看文件內容時(如:https://github.com/tiimgreen/github-cheat-sheet/blob/master/README.md),按 y 鍵將會凍結這個頁面,這樣就算代碼被修改了也不會影響你當前看到的。

按?查看當前頁面支持的快捷鍵列表:

Keyboard shortcuts

進一步瞭解如何使用 Command Bar.

整行高亮

在代碼文件地址後加上#L52或者單擊行號52都會將第52行代碼高亮顯示。

多行高亮也能夠,好比用#L53-L60選擇範圍,或者按住 shift鍵,而後再點擊選擇的兩行。

整行高亮

用commit信息關閉Issue

若是某個提交修復了一個Issue,當提交到master分支時,提交信息裏可使用fix/fixes/fixed, close/closes/closed 或者 resolve/resolves/resolved等關鍵詞,後面再跟上Issue號,這樣就會關閉這個Issue。


1
$ git commit -m "Fix screwup, fixes #12"

這將會關閉Issue #12,而且在Issue討論列表裏關聯引用此次提交。

Closing Repo

進一步瞭解經過提交信息關閉Issue.

連接其餘倉庫的Issue

若是你想引用到同一個倉庫中的一個Issue,只需使用井號 # 加上Issue號,這樣就會自動建立到此Issue的連接。

要連接到其餘倉庫的Issue,就使用user_name/repo_name#ISSUE_NUMBER的方式,例如tiimgreen/toc#12。

Cross-Link Issues

設置CI對每條Pull Request都進行構建

若是配置正確,Travis CI會爲每一個你收到的Pull Request執行構建,就像每次提交也會觸發構建同樣。想了解更多關於Travis CI的信息,請看 Travis CI入門

Travis CI status

進一步瞭解 Commit status API.

Markdown文件高亮語法

例如,能夠像下面這樣在你的Markdown文件裏爲Ruby代碼添加語法高亮:


1
2
3
4
5
6
```ruby
require 'tabbit'
table = Tabbit.new('Name', 'Email')
table.add_row('Tim Green', 'tiimgreen@gmail.com')
puts table.to_s
```

效果像下面這樣:


1
2
3
4
require 'tabbit'
table = Tabbit.new('Name', 'Email')
table.add_row('Tim Green', 'tiimgreen@gmail.com')
puts table.to_s

Github使用 Linguist 作語言識別和語法高亮。你能夠仔細閱讀 languages YAML file,瞭解有哪些可用的關鍵字。

進一步瞭解 GitHub Flavored Markdown.

表情符

能夠在Pull Requests, Issues, 提交消息, Markdown文件里加入表情符。使用方法:name_of_emoji:


1
:smile:

將輸出一個笑臉::smile:

Github支持的完整表情符號列表詳見emoji-cheat-sheet.com 或 scotch-io/All-Github-Emoji-Icons

Github上使用最多的5個表情符號是:

2M@U{A$I6U~D6CJQP$I$XWL

靜態與動態圖片

註釋和README等文件裏也可使用圖片和GIF動畫:

倉庫中的原始圖片能夠被直接調用:


1
![Alt Text]( https://github.com/(user)/(repo)/raw/master/path/to/image.gif)

Peter don't care

全部圖片都緩存在Gitub,不用擔憂你的站點不能訪問時就看不到圖片了。

在GitHub Wiki中嵌入圖片

有多種方法能夠在Wiki頁面裏嵌入圖片。既能夠像上一條裏那樣使用標準的Markdown語法,也能夠像下面這樣指定圖片的高度或寬度:

結果:

Just a screenshot

快速引用

在註釋話題裏引用以前某我的所說的,只需選中文本,而後按 r鍵,想要的就會以引用的形式複製到你的輸入框裏。

Quick Quote

進一步瞭解快速引用.

快速添加許可證

建立一個倉庫時,Github會爲你提供一個預置的軟件許可列表:

License

對於已有的倉庫,能夠經過web界面建立文件來添加軟件許可。輸入LICENSE做爲文件名後,一樣能夠從預置的列表中選擇一個做爲模板。

License

這個技巧也適用於 .gitignore 文件。

進一步瞭解 open source licensing.

任務列表

Issues和Pull requests裏能夠添加複選框,語法以下(注意空白符):


1
2
3
4
5
6
- [ ] Be awesome
- [ ] Prepare dinner
  - [ ] Research recipe
  - [ ] Buy ingredients
  - [ ] Cook recipe
- [ ] Sleep

Task List

當項目被選中時,它對應的Markdown源碼也被更新了:


1
2
3
4
5
6
- [x] Be awesome
- [ ] Prepare dinner
  - [x] Research recipe
  - [x] Buy ingredients
  - [ ] Cook recipe
- [ ] Sleep

進一步瞭解任務列表.

Markdown文件中的任務列表

在徹底適配Markdown語法的文件中可使用如下語法加入一個只讀的任務列表


1
2
3
4
5
6
7
- [ ] Mercury
- [x] Venus
- [x] Earth
  - [x] Moon
- [x] Mars
  - [ ] Deimos
  - [ ] Phobos

 

  •  Mercury
  •  Venus
  •  Earth
    •  Moon
  •  Mars
    •  Deimos
    •  Phobos

進一步瞭解Markdown文件中的任務列表

相對連接

Markdown文件裏連接到內部內容時推薦使用相對連接。


1
2
[Link to a header](#awesome-section)
[Link to a file](docs/readme)

絕對連接會在URL改變時(例如重命名倉庫、用戶名改變,創建分支項目)被更新。使用相對連接可以保證你的文檔不受此影響。

進一步瞭解相對連接.

GitHub Pages的元數據與插件支持

在Jekyll頁面和文章裏,倉庫信息可在 site.github 命名空間下找到,也能夠顯示出來,例如,使用 {{ site.github.project_title }}顯示項目標題。

Jemoji和jekyll-mentions插件爲你的Jekyll文章和頁面增長了emoji@mentions功能。

瞭解更多 GitHub Pages的元數據和插件支持.

查看YAML格式的元數據

許多博客站點,好比基於JekyllGitHub Pages,都依賴於一些文章頭部的YAML格式的元數據。Github會將其渲染成一個水平表格,方便閱讀。

YAML metadata

進一步瞭解 在文檔裏查看YAML元數據.

渲染表格數據

GitHub支持將 .csv (comma分隔)和.tsv (tab分隔)格式的文件渲染成表格數據。

Tabular data

進一步瞭解渲染表格數據.

撤銷Pull Request

能夠經過Pull Request中的Revert按鈕來撤銷一個已合併的Pull Request中的commit。按下按鈕後會自動生成一個進行逆向操做的Pull Request。

Revert button

*進一步瞭解「撤銷」按鈕

Diffs

可渲染文檔的Diffs

提交和Pull Requests裏包含有Github支持的可渲染文檔(好比Markdown)會提供source 和 rendered兩個視圖功能。

Source / Rendered view

點擊 「rendered」 按鈕,看看改動在渲染後的顯示效果。當你添加、刪除或修改文本時,渲染純文本視圖很是方便。

Rendered Prose Diffs

進一步瞭解渲染純文本視圖Diffs.

可變化地圖

當你在GitHub上查看一個包含地理數據的提交或pull request時,Github能夠顯示數據變更的視覺表示。

Diffable Maps

進一步瞭解可比較地圖.

在diff中摺疊與擴展代碼

你能夠經過點擊diff邊欄裏的 unfold 按鈕來多顯示幾行上下文。你能夠一直點擊 unfold 按鈕直到顯示了文件的所有內容。這個功能在全部GitHub產生的diff界面均可以使用。

Expanding Context in Diffs

進一步瞭解擴展Diff上下文.

查看Pull Request的diff和patch

在Pull Request的URL後面加上 .diff 或 .patch 的擴展名就能夠獲得它的diff或patch文件,例如:

.diff 擴展會使用普通文本格式顯示以下內容:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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在內的多種圖片格式並提供了幾種方式來比較這些格式的圖片文件版本間的不一樣。

Diffable PSD

查看更多關於渲染圖像變更的內容

Hub

Hub是一個對Git進行了封裝的命令行工具,能夠幫助你更方便的使用Github。

這使得你能夠像下面這樣進行克隆:


1
$ hub clone tiimgreen/toc

查看更多Hub提供的超酷命令.

貢獻內容的自動檢查

假設你想人們使用你的項目並給你的項目作出貢獻,你每每須要回答他們常見問題。這個項目是幹什麼用的?我如何使用它?容許我怎樣使用?我如何爲項目出力?我怎樣配置開發環境?我怎麼能保證新功能不會破壞已有的功能?

Friction是一個命令行腳本,用來檢查你的項目是否回答了這些問題。下面是示例輸出:

Friction output

Friction 支持 MRI 2.1.0, MRI 2.0.0 和 MRI 1.9.3.

貢獻者指南

在你的倉庫的根目錄添加一個名爲 CONTRIBUTING 的文件後,貢獻者在新建Issue或Pull Request時會看到這個文件的連接。

Contributing Guidelines

進一步瞭解貢獻者指南.

GitHub資源

Title Link
GitHub Explore https://github.com/explore
GitHub Blog https://github.com/blog
GitHub Help https://help.github.com/
GitHub Training http://training.github.com/
GitHub Developer https://developer.github.com/

GitHub討論

Title Link
How GitHub Uses GitHub to Build GitHub https://www.youtube.com/watch?v=qyz3jkOBbQY
Introduction to Git with Scott Chacon of GitHub https://www.youtube.com/watch?v=ZDR433b0HJY
How GitHub No Longer Works https://www.youtube.com/watch?v=gXD1ITW7iZI
Git and GitHub Secrets https://www.youtube.com/watch?v=Foz9yvMkvlA
More Git and GitHub Secrets https://www.youtube.com/watch?v=p50xsL-iVgU

Git

前一個分支

快速檢出上一個分支:


1
2
3
4
5
6
7
8
$ git checkout -
# Switched to branch 'master'
 
$ git checkout -
# Switched to branch 'next'
 
$ git checkout -
# Switched to branch 'master'

進一步瞭解 Git 分支.

Stripspace命令

Git Stripspace命令能夠:

  • 去掉行尾空白符
  • 多個空行壓縮成一行
  • 必要時在文件末尾增長一個空行

使用此命令時必須傳入一個文件,像這樣:


1
$ git stripspace < README.md

進一步瞭解 Git stripspace 命令.

檢出Pull Requests

Pull Request是一種GitHub上能夠經過如下多種方式在本地被檢索的特別分支:

檢索某個分支並臨時儲存在本地的FETCH_HEAD中以便快速查看更改(diff)以及合併(merge):


1
$ git fetch origin refs/pull/[PR-Number]/head

經過refspec獲取全部的Pull Request爲本地分支:


1
$ git fetch origin '+refs/pull/*/head:refs/remotes/origin/pr/*'

或在倉庫的.git/config中加入下列設置來自動獲取遠程倉庫中的Pull Request


1
2
3
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git@github.com:tiimgreen/github-cheat-sheet.git

 


1
2
3
4
[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的遠端分支再由此分支創建一個本地分支:


1
$ git checkout pr/42 pr-42

操做多個倉庫的時候,能夠在Git中設置獲取Pull Request的全局選項。


1
git config --global --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*"

此時能夠在任意倉庫中使用如下命令:


1
git fetch origin

 


1
git checkout pr/42

進一步瞭解如何檢出pull request到本地.

提交空改動

可使用–allow-empty選項強制建立一個沒有任何改動的提交:


1
$ git commit -m "Big-ass commit" --allow-empty

這樣作在以下幾種狀況下是有意義的:

  • 標記一批工做或一個新功能的開始。
  • 記錄你對項目進行了跟代碼無關的改動。
  • 跟使用你倉庫的其餘人交流。
  • 做爲倉庫的第一次提交,由於第一次提交往後是不能被rebase的: git commit -m 「init repo」 –allow-empty.

更直觀的Git Status

在命令行輸入以下命令:


1
$ git status

能夠看到:

git status

加上-sb選項:


1
$ git status -sb

這回獲得:

git status -sb

進一步瞭解 Git status 命令.

更直觀的Git Log

輸入以下命令:


1
$ git log --all --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date<span class="o">=relative

能夠看到:

%Creset' --abbrev-commit --date=relative" data-canonical-src="http://i.imgur.com/R2z8l7c.png" style="margin: 0px auto; padding: 0px; font-family: inherit; font-size: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: middle; max-width: 100%; height: auto; text-align: center; box-sizing: border-box; box-shadow: rgba(0, 0, 0, 0.4) 0px 0px 10px; -webkit-transition: all 0.1s ease-in-out; transition: all 0.1s ease-in-out;">

這要歸功於Palesz在stackoverflow的回答。

這個命令能夠被用做別名,詳細作法見這裏

進一步瞭解 Git log 命令.

Git查詢

Git查詢運行你在以前的全部提交信息裏進行搜索,找到其中和搜索條件相匹配的最近的一條。


1
$ git show :/query

這裏 query (區別大小寫)是你想要搜索的詞語, 這條命令會找到包含這個詞語的最後那個提交併顯示變更詳情。


1
$ git show :/typo

git show :/query

  • 按 q 鍵退出命令。*

合併分支

輸入命令:


1
$ git branch --merged

這會顯示全部已經合併到你當前分支的分支列表。

相反地:


1
$ git branch --no-merged

會顯示全部尚未合併到你當前分支的分支列表。

進一步瞭解 Git branch 命令.

使用網頁查看本地倉庫

使用Git的 instaweb 能夠當即在 gitweb中瀏覽你的工做倉庫。這個命令是個簡單的腳步,配置了gitweb和用來瀏覽本地倉庫的Web服務器。(譯者注:默認須要lighttpd支持)


1
$ git instaweb

執行後打開:

Git instaweb

進一步瞭解 Git instaweb 命令.

Git配置

全部Git配置都保存在你的.gitconfig 文件中。

Git命令自定義別名

別名用來幫助你定義本身的git命令。好比你能夠定義 git a 來運行 git add –all。

要添加一個別名, 一種方法是打開 ~/.gitconfig 文件並添加以下內容:


1
2
3
4
5
6
7
8
[alias]
  co = checkout
  cm = commit
  p = push
  # Show verbose output about tags, branches or remotes
  tags = tag -l
  branches = branch -a
  remotes = remote -v

…或者在命令行裏鍵入:


1
$ git config --global alias.new_alias git_function

例如:


1
$ git config --global alias.cm commit

指向多個命令的別名能夠用引號來定義:


1
$ 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 你會看到以下輸出:


1
2
3
4
5
$ git comit -m "Message"
# git: 'comit' is not a git command. See 'git --help'.
 
# Did you mean this?
# git commit

爲了在鍵入 comit 調用 commit命令,只需啓用自動糾錯功能:


1
$ git config --global help.autocorrect 1

如今你就會看到:


1
2
3
4
$ 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命令輸出里加上顏色的話,能夠用以下命令:


1
$ git config --global color.ui 1

進一步瞭解 Git config 命令.

Git資源

Title Link
Official Git Site http://git-scm.com/
Official Git Video Tutorials http://git-scm.com/videos
Code School Try Git http://try.github.com/
Introductory Reference & Tutorial for Git http://gitref.org/
Official Git Tutorial http://git-scm.com/docs/gittutorial
Everyday Git http://git-scm.com/docs/everyday
Git Immersion http://gitimmersion.com/
Ry’s Git Tutorial http://rypress.com/tutorials/git/index.html
Git for Designer http://hoth.entp.com/output/git_for_designers.html
Git for Computer Scientists http://eagain.net/articles/git-for-computer-scientists/
Git Magic 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
Pro Git http://git-scm.com/book
Git Internals Peepcode http://peepcode.com/products/git-internals-pdf
Git in the Trenches http://cbx33.github.com/gitt/
Version Control with Git 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
Git: Version Control for Everyone http://www.packtpub.com/git-version-control-for-everyone/book
相關文章
相關標籤/搜索