GitHub祕籍

本祕籍收錄了一些Git和Github非常酷同時又少有人知的功能。靈感來自於Zach Holman在2012年Aloha Ruby Conference和2013年WDCNZ上所做的演講:Git and GitHub Secrets(slides)和More Git and GitHub Secrets(slides)。

目錄

GitHub

忽略空白字符變化

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

Diff without whitespace

詳見 GitHub secrets.

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

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

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

Before, tab space example

然後是我們添加?ts=4的例子:

After, tab space example

查看某個用戶的Commit歷史

查看某個用戶的所有提交歷史,只需在commits頁面URL後加上?author=username

https://github.com/rails/rails/commits/master?author=dhh

DHH commit history

深入瞭解提交視圖之間的區別

克隆某個倉庫

當我們克隆某一資源時,可以不要那個.git後綴。

$ git clone https://github.com/tiimgreen/github-cheat-sheet

更多對 Git clone 命令的介紹.

將某個分支與其他所有分支進行對比

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

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後加入要比較的分支名來實現:

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

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

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

Compare branches merged in to `1.x-master` in jquery/jquery repo - https://github.com/jquery/jquery/branches/1.x-master?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

Rails branch compare example

{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

Another compare example

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

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

比較不同派生庫的分支

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

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

例如:

https://github.com/rails/rails/compare/byroot:master...master

Forked branch compare

Gists

Gists 給我們提供了一種不需要創建一個完整的倉庫,使小段代碼也可以工作的簡單方式。

Gist

Gist的URL後加上.pibb,可以得到更適合嵌入到其他網站的HTML版本。

Gists還可以像任何標準倉庫一樣被克隆。

$ git clone https://gist.github.com/tiimgreen/10545817

Gists

進一步瞭解如何創建 gists.

Git.io

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

Git.io

你可以通過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.

鍵盤快捷鍵

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

  • 按 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鍵,然後再點擊選擇的兩行。

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討論列表裏關聯引用這次提交。

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代碼添加語法高亮:

```ruby
require 'tabbit'
table = Tabbit.new('Name', 'Email')
table.add_row('Tim Green', '[email protected]')
puts table.to_s
```

效果像下面這樣:

require 'tabbit'
table = Tabbit.new('Name', 'Email')
table.add_row('Tim Green', '[email protected]')
puts table.to_s

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

進一步瞭解 GitHub Flavored Markdown.

表情符

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

:smile:

將輸出一個笑臉:

:smile:

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

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

  1. :shipit: - :shipit:
  2. :sparkles: - :sparkles:
  3. :-1: - :-1:
  4. :+1: - :+1:
  5. :clap: - :clap:

靜態與動態圖片

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

![Alt Text](http://www.sheawong.com/wp-content/uploads/2013/08/keephatin.gif)

Peter don't care

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

在GitHub Wiki中嵌入圖片

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

[[ http://www.sheawong.com/wp-content/uploads/2013/08/keephatin.gif | height = 100px ]]

結果:

Just a screenshot

快速引用

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

Quick Quote

進一步瞭解快速引用.

快速添加許可證

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

License

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

License

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

進一步瞭解 open source licensing.

任務列表

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

- [ ] Be awesome
- [ ] Prepare dinner
  - [ ] Research recipe
  - [ ] Buy ingredients
  - [ ] Cook recipe
- [ ] Sleep

Task List

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

- [x] Be awesome
- [ ] Prepare dinner
  - [x] Research recipe
  - [x] Buy ingredients
  - [ ] Cook recipe
- [ ] Sleep

進一步瞭解任務列表.

Markdown文件中的任務列表

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

- [ ] Mercury
- [x] Venus
- [x] Earth
  - [x] Moon
- [x] Mars
  - [ ] Deimos
  - [ ] Phobos

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

相對鏈接

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

[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

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

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文件,例如:

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在內的多種圖片格式並提供了幾種方式來比較這些格式的圖片文件版本間的不同。

Diffable PSD

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

Hub

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

這使得你可以像下面這樣進行克隆:

$ 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

前一個分支

快速檢出上一個分支:

$ git checkout -
# Switched to branch 'master'

$ git checkout -
# Switched to branch 'next'

$ git checkout -
# Switched to branch 'master'

進一步瞭解 Git 分支.

Stripspace命令

Git Stripspace命令可以:

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

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

$ git stripspace < README.md

進一步瞭解 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 = [email protected]:tiimgreen/github-cheat-sheet.git
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = [email protected]:tiimgreen/github-cheat-sheet.git
    fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

對基於派生庫的Pull Request,可以通過先checkout代表此Pull Request的遠端分支再由此分支建立一個本地分支:

$ git checkout pr/42 pr-42

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

提交空改動 :trollface:

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

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

這樣做在如下幾種情況下是有意義的:

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

更直觀的Git Status

在命令行輸入如下命令:

$ git status

可以看到:

git status

加上-sb選項:

$ git status -sb

這回得到:

git status -sb

進一步瞭解 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

可以看到:

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在stackoverflow的回答。

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

進一步瞭解 Git log 命令.

Git查詢

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

$ git show :/query

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

$ git show :/typo

git show :/query

  • 按 q 鍵退出命令。*

合併分支

輸入命令:

$ git branch --merged

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

相反地:

$ git branch --no-merged

會顯示所有還沒有合併到你當前分支的分支列表。

進一步瞭解 Git branch 命令.

使用網頁查看本地倉庫

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

$ git instaweb

執行後打開:

Git instaweb

進一步瞭解 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 命令.

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
來自 :http://snowdream86.gitbooks.io/github-cheat-sheet/content/zh/index.html
發佈了28 篇原創文章 · 獲贊 6 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章