diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..6da6f74 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: [tiimgreen] diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..74e1227 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,15 @@ +language: ruby + +rvm: + - 2.2 + +before_script: + - gem install awesome_bot + +script: + - wl=https://github.com/rails/rails/compare/master@,http://git.io/abc123,https://github.com/..,youtube,chrome.google.com/webstore,https://github.com/mathiasbynens/dotfiles/blob/master/.gitconfig*,https://github.com/user/repo/compare/,vimeo + - awesome_bot README.md --allow-dupe --allow-redirect --white-list $wl + - awesome_bot README.ko.md --allow-dupe --allow-redirect --white-list $wl + - awesome_bot README.ja.md --allow-dupe --allow-redirect --white-list $wl + - awesome_bot README.zh-cn.md --allow-dupe --allow-redirect --white-list $wl + - awesome_bot README.zh-tw.md --allow-dupe --allow-redirect --white-list $wl diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 49f87d9..a4f3c02 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,7 +1,7 @@ # Contributing 1. Fork the repository. -2. Add your section - make sure you follow the [styling](#styling) guide. +2. Add your section - make sure you follow the styling guide below. 3. Commit changes. 4. Push your commit. 5. Create a Pull Request. @@ -12,5 +12,6 @@ - Also use `###` for categories (these should group together relevant sections) and `####` for subcategories. - Add a link to your section/category to the contents section (use relative links). - Make sure to include examples wherever possible (preferably GIFs). -- For command-line examples, wrap your commands in a `bash` code block. Ask [@rafalchmiel](https://github.com/rafalchmiel) or [@tiimgreen](https://github.com/tiimgreen) to take a screenshot (so that it is consistent with the rest) of the output if it has color, etc. -- At the end of your section, add a `Read more about...` link. This should directly link to [Git's](http://git-scm.com/docs) or [GitHub's](https://help.github.com/) documentation. +- For command-line examples, wrap your commands in a `bash` code block. +- At the end of your section, add a `Read more about...` link. This should directly link to [Git's](http://git-scm.com/docs), [GitHub's](https://help.github.com), or the relevant documentation. +- If your contribution is a translation, add a (relative) link to the README above the [Table of Contents](README.md#table-of-contents). diff --git a/README.ja.md b/README.ja.md new file mode 100644 index 0000000..050c863 --- /dev/null +++ b/README.ja.md @@ -0,0 +1,998 @@ +# GitHubカンニング・ペーパー [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/sindresorhus/awesome) +これはGitやGitHubの隠された機能やよく知られていない機能の一覧だ。[Zach Holman](https://github.com/holman)によるAloha Ruby Conference 2012での[Git and GitHub Secrets](https://github.com/tiimgreen/github-cheat-sheet)([スライド](https://github.com/tiimgreen/github-cheat-sheet))とWDCNZ 2013での[More Git and GitHub Secrets](https://vimeo.com/72955426)([スライド](https://speakerdeck.com/holman/more-git-and-github-secrets))の二つのトークを元にしている。 + +*短縮URL: [`http://git.io/sheet`](http://git.io/sheet)* + +*Read this in other languages: [English](README.md), [한국어](README.ko.md), [日本語](README.ja.md), [简体中文](README.zh-cn.md), [正體中文](README.zh-tw.md).* + +# 目次 +- [GitHub](#github) + - [空白の無視](#%E7%A9%BA%E7%99%BD%E3%81%AE%E7%84%A1%E8%A6%96) + - [タブ幅の調節](#%E3%82%BF%E3%83%96%E5%B9%85%E3%81%AE%E8%AA%BF%E7%AF%80) + - [特定のユーザーによるコミット履歴](#%E7%89%B9%E5%AE%9A%E3%81%AE%E3%83%A6%E3%83%BC%E3%82%B6%E3%83%BC%E3%81%AB%E3%82%88%E3%82%8B%E3%82%B3%E3%83%9F%E3%83%83%E3%83%88%E5%B1%A5%E6%AD%B4) + - [リポジトリのクローン](#%E3%83%AA%E3%83%9D%E3%82%B8%E3%83%88%E3%83%AA%E3%81%AE%E3%82%AF%E3%83%AD%E3%83%BC%E3%83%B3) + - [ブランチ](#%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81) + - [全ブランチを別のブランチと比較](#%E5%85%A8%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81%E3%82%92%E5%88%A5%E3%81%AE%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81%E3%81%A8%E6%AF%94%E8%BC%83) + - [ブランチ同士の比較](#%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81%E5%90%8C%E5%A3%AB%E3%81%AE%E6%AF%94%E8%BC%83) + - [フォークされたリポジトリ間でのブランチ比較](#%E3%83%95%E3%82%A9%E3%83%BC%E3%82%AF%E3%81%95%E3%82%8C%E3%81%9F%E3%83%AA%E3%83%9D%E3%82%B8%E3%83%88%E3%83%AA%E9%96%93%E3%81%A7%E3%81%AE%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81%E6%AF%94%E8%BC%83) + - [Gists](#gists) + - [Git.io](#gitio) + - [キーボード・ショートカット](#%E3%82%AD%E3%83%BC%E3%83%9C%E3%83%BC%E3%83%89%E3%82%B7%E3%83%A7%E3%83%BC%E3%83%88%E3%82%AB%E3%83%83%E3%83%88) + - [コードの指定行の強調](#%E3%82%B3%E3%83%BC%E3%83%89%E3%81%AE%E6%8C%87%E5%AE%9A%E8%A1%8C%E3%81%AE%E5%BC%B7%E8%AA%BF) + - [コミットからissueを閉じる](#%E3%82%B3%E3%83%9F%E3%83%83%E3%83%88%E3%81%8B%E3%82%89issue%E3%82%92%E9%96%89%E3%81%98%E3%82%8B) + - [issueの相互リンク](#issue%E3%81%AE%E7%9B%B8%E4%BA%92%E3%83%AA%E3%83%B3%E3%82%AF) + - [コメントのロック](#%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%E3%81%AE%E3%83%AD%E3%83%83%E3%82%AF) + - [pull requestでのCI結果の表示](#pull-request%E3%81%A7%E3%81%AEci%E7%B5%90%E6%9E%9C%E3%81%AE%E8%A1%A8%E7%A4%BA) + - [絞り込み検索](#%E7%B5%9E%E3%82%8A%E8%BE%BC%E3%81%BF%E6%A4%9C%E7%B4%A2) + - [Markdownファイルでの構文強調](#markdown%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%A7%E3%81%AE%E6%A7%8B%E6%96%87%E5%BC%B7%E8%AA%BF) + - [Emoji](#emoji) + - [画像及びアニメーションGIF](#%E7%94%BB%E5%83%8F%E5%8F%8A%E3%81%B3%E3%82%A2%E3%83%8B%E3%83%A1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3gif) + - [GitHub Wikiへの画像の添付](#github-wiki%E3%81%B8%E3%81%AE%E7%94%BB%E5%83%8F%E3%81%AE%E6%B7%BB%E4%BB%98) + - [素早く引用](#%E7%B4%A0%E6%97%A9%E3%81%8F%E5%BC%95%E7%94%A8) + - [コメントへのクリップボードの画像の追加](#%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%E3%81%B8%E3%81%AE%E3%82%AF%E3%83%AA%E3%83%83%E3%83%97%E3%83%9C%E3%83%BC%E3%83%89%E3%81%AE%E7%94%BB%E5%83%8F%E3%81%AE%E8%BF%BD%E5%8A%A0) + - [設定済みライセンスの追加](#%E8%A8%AD%E5%AE%9A%E6%B8%88%E3%81%BF%E3%83%A9%E3%82%A4%E3%82%BB%E3%83%B3%E3%82%B9%E3%81%AE%E8%BF%BD%E5%8A%A0) + - [タスクリスト](#%E3%82%BF%E3%82%B9%E3%82%AF%E3%83%AA%E3%82%B9%E3%83%88) + - [Markdownファイルでのタスクリスト](#markdown%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%A7%E3%81%AE%E3%82%BF%E3%82%B9%E3%82%AF%E3%83%AA%E3%82%B9%E3%83%88) + - [相対リンク](#%E7%9B%B8%E5%AF%BE%E3%83%AA%E3%83%B3%E3%82%AF) + - [GitHub Pagesでのメタデータとプラグインのサポート](#github-pages%E3%81%A7%E3%81%AE%E3%83%A1%E3%82%BF%E3%83%87%E3%83%BC%E3%82%BF%E3%81%A8%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3%E3%81%AE%E3%82%B5%E3%83%9D%E3%83%BC%E3%83%88) + - [文書内のYAMLメタデータ](#%E6%96%87%E6%9B%B8%E5%86%85%E3%81%AEyaml%E3%83%A1%E3%82%BF%E3%83%87%E3%83%BC%E3%82%BF) + - [表形式のデータ表示](#%E8%A1%A8%E5%BD%A2%E5%BC%8F%E3%81%AE%E3%83%87%E3%83%BC%E3%82%BF%E8%A1%A8%E7%A4%BA) + - [PDF表示](#pdf%E8%A1%A8%E7%A4%BA) + - [pull requestの取り消し](#pull-request%E3%81%AE%E5%8F%96%E3%82%8A%E6%B6%88%E3%81%97) + - [差分の表示](#%E5%B7%AE%E5%88%86%E3%81%AE%E8%A1%A8%E7%A4%BA) + - [レンダリング済みの差分表示](#%E3%83%AC%E3%83%B3%E3%83%80%E3%83%AA%E3%83%B3%E3%82%B0%E6%B8%88%E3%81%BF%E3%81%AE%E5%B7%AE%E5%88%86%E8%A1%A8%E7%A4%BA) + - [マップ差分の可視化](#%E3%83%9E%E3%83%83%E3%83%97%E5%B7%AE%E5%88%86%E3%81%AE%E5%8F%AF%E8%A6%96%E5%8C%96) + - [差分表示の前後を表示](#%E5%B7%AE%E5%88%86%E8%A1%A8%E7%A4%BA%E3%81%AE%E5%89%8D%E5%BE%8C%E3%82%92%E8%A1%A8%E7%A4%BA) + - [pull requestの内容をDIFFまたはPATCH形式で取得](#pull-request%E3%81%AE%E5%86%85%E5%AE%B9%E3%82%92diff%E3%81%BE%E3%81%9F%E3%81%AFpatch%E5%BD%A2%E5%BC%8F%E3%81%A7%E5%8F%96%E5%BE%97) + - [画像の表示とDIFF](#%E7%94%BB%E5%83%8F%E3%81%AE%E8%A1%A8%E7%A4%BA%E3%81%A8diff) + - [Hub](#hub) + - [開発参加のガイドライン](#%E9%96%8B%E7%99%BA%E5%8F%82%E5%8A%A0%E3%81%AE%E3%82%AC%E3%82%A4%E3%83%89%E3%83%A9%E3%82%A4%E3%83%B3) + - [Octicons](#octicons) + - [GitHub情報](#github%E6%83%85%E5%A0%B1) + - [GitHub Talks](#github-talks) +- [Git](#git) + - [すべての削除済みファイルをワーキング・ツリーから削除する](#%E3%81%99%E3%81%B9%E3%81%A6%E3%81%AE%E5%89%8A%E9%99%A4%E6%B8%88%E3%81%BF%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%82%92%E3%83%AF%E3%83%BC%E3%82%AD%E3%83%B3%E3%82%B0%E3%83%84%E3%83%AA%E3%83%BC%E3%81%8B%E3%82%89%E5%89%8A%E9%99%A4%E3%81%99%E3%82%8B) + - [直前のブランチ](#%E7%9B%B4%E5%89%8D%E3%81%AE%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81) + - [空白の削除](#%E7%A9%BA%E7%99%BD%E3%81%AE%E5%89%8A%E9%99%A4) + - [SSH公開鍵](#ssh%E5%85%AC%E9%96%8B%E9%8D%B5) + - [pull requestのチェックアウト](#pull-request%E3%81%AE%E3%83%81%E3%82%A7%E3%83%83%E3%82%AF%E3%82%A2%E3%82%A6%E3%83%88) + - [空のコミット](#%E7%A9%BA%E3%81%AE%E3%82%B3%E3%83%9F%E3%83%83%E3%83%88) + - [Gitステータスのスタイリング](#git%E3%82%B9%E3%83%86%E3%83%BC%E3%82%BF%E3%82%B9%E3%81%AE%E3%82%B9%E3%82%BF%E3%82%A4%E3%83%AA%E3%83%B3%E3%82%B0) + - [Gitログのスタイリング](#git%E3%83%AD%E3%82%B0%E3%81%AE%E3%82%B9%E3%82%BF%E3%82%A4%E3%83%AA%E3%83%B3%E3%82%B0) + - [コミットログの検索](#%E3%82%B3%E3%83%9F%E3%83%83%E3%83%88%E3%83%AD%E3%82%B0%E3%81%AE%E6%A4%9C%E7%B4%A2) + - [Git Grep](#git-grep) + - [マージ済みブランチ](#%E3%83%9E%E3%83%BC%E3%82%B8%E6%B8%88%E3%81%BF%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81) + - [FixupとAutosquash](#fixup%E3%81%A8autosquash) + - [ローカル・リポジトリを参照するウェブサーバー](#%E3%83%AD%E3%83%BC%E3%82%AB%E3%83%AB%E3%83%AA%E3%83%9D%E3%82%B8%E3%83%88%E3%83%AA%E3%82%92%E5%8F%82%E7%85%A7%E3%81%99%E3%82%8B%E3%82%A6%E3%82%A7%E3%83%96%E3%82%B5%E3%83%BC%E3%83%90%E3%83%BC) + - [Gitの設定](#git%E3%81%AE%E8%A8%AD%E5%AE%9A) + - [エイリアス](#%E3%82%A8%E3%82%A4%E3%83%AA%E3%82%A2%E3%82%B9) + - [コマンドの自動修正](#%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89%E3%81%AE%E8%87%AA%E5%8B%95%E4%BF%AE%E6%AD%A3) + - [色設定](#%E8%89%B2%E8%A8%AD%E5%AE%9A) + - [Git情報](#git%E6%83%85%E5%A0%B1) + - [Git Books](#git-books) +- [訳注](#%E8%A8%B3%E6%B3%A8) + +## GitHub +### 空白の無視 +GitHub上で差分ページを表示している時、そのURLに`?w=1`を加えると、空白の変化によるできた差分は表示されなくなり、コード上の変化だけを参照することができる。 + +![Diff without whitespace](https://camo.githubusercontent.com/797184940defadec00393e6559b835358a863eeb/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f626c6f672f323031312f736563726574732f776869746573706163652e706e67) + +[*GitHubの秘密についてもっと詳しく*](https://github.com/blog/967-github-secrets) + +### タブ幅の調節 +差分やファイルを表示している時、URLに`?ts=4`を追加するとタブを空白4つの幅で表示する。デフォルトは8つだ。`ts`に指定した数で表示されるということだ。これはGistやrawファイルを表示している時には適用されないが、[Chrome](https://chrome.google.com/webstore/detail/github-tab-size/ofjbgncegkdemndciafljngjbdpfmbkn)をインストールすることにより、対応することができる。 + +例えばGoのソースファイルを表示している時、`?ts=4`を追加する前はこのように表示されるが: + +![Before, tab space example](http://i.imgur.com/GIT1Fr0.png) + +`?ts=4`を追加するとこのように表示される: + +![After, tab space example](http://i.imgur.com/70FL4H9.png) + +### 特定のユーザーによるコミット履歴 +特定のユーザーによるあるリポジトリへのコミット履歴のみを参照したい場合は、`?author={user}`をURLの末尾に付ける。 + +``` +https://github.com/rails/rails/commits/master?author=dhh +``` +![DHH commit history](http://i.imgur.com/S7AE29b.png) + +[*コミット・ビューの違いについてもっと詳しく*](https://help.github.com/articles/differences-between-commit-views) + +### リポジトリのクローン +リポジトリをクローンする時、URLの末尾の`.git`は無くても構わない。 + +```bash +$ git clone https://github.com/tiimgreen/github-cheat-sheet +``` + +[*Gitの`clone`コマンドについてもっと詳しく*](http://git-scm.com/docs/git-clone) + +### ブランチ +#### 全ブランチを別のブランチと比較 +コミット一覧ボタンの隣から[ブランチ一覧](https://github.com/tiimgreen/github-cheat-sheet/branches)ページに行くと: + +``` +https://github.com/{user}/{repo}/branches +``` + +メイン・ブランチにマージされていないブランチの一覧が表示される。 + +このページからボタンをクリックしてブランチ比較ページへ移動したりブランチを削除することができる。 + +![Compare branches not merged into master in rails/rails repo - https://github.com/rails/rails/branches](http://i.imgur.com/0FEe30z.png) + +#### ブランチ同士の比較 +GitHubのブランチ比較は以下のような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](http://i.imgur.com/tIRCOsK.png) + +`{range}`には以下のように変更することもできる: + +``` +https://github.com/rails/rails/compare/master@{1.day.ago}...master +https://github.com/rails/rails/compare/master@{2014-10-04}...master +``` + +*日付の形式は`YYYY-MM-DD`だ。* + +![Another compare example](http://i.imgur.com/5dtzESz.png) + +`diff`や`patch`のビューでもブランチを利用して比較することができる: + +``` +https://github.com/rails/rails/compare/master...4-1-stable.diff +https://github.com/rails/rails/compare/master...4-1-stable.patch +``` + +[*時間を指定してのブランチ比較についてもっと詳しく*](https://help.github.com/articles/comparing-commits-across-time) + +#### フォークされたリポジトリ間でのブランチ比較 +GitHubでフォークされたリポジトリ同士でブランチを比較する場合、以下のようなURLを変更する: + +``` +https://github.com/user/repo/compare/{foreign-user}:{branch}...{own-branch} +``` + +例: + +``` +https://github.com/rails/rails/compare/byroot:master...master +``` + +![Forked branch compare](http://i.imgur.com/Q1W6qcB.png) + +### Gists +[Gists](https://gist.github.com/)は少量のコード群を管理する最適な手段だ。ちゃんとしたリポジトリをいちいち作成する必要はない。 + +![Gist](http://i.imgur.com/VkKI1LC.png?1) + +GistのURLの最後に`.pibb`を付ける([例](https://gist.github.com/hail2u/9477708.pibb))と*HTMLのみ*のバージョンが表示されるので、そのソースは他のウェブサイトに貼り付けるにはもってこいだろう。 + +簡単なものとはいえ、Gitリポジトリとして機能するため、以下のようにすれば普通のGitリポジトリと同じようにクローンすることができる: + +```bash +$ git clone https://gist.github.com/tiimgreen/10545817 +``` + +![Gists](http://i.imgur.com/BcFzabp.png) + +This means you also can modify and push updates to Gists: + +```bash +$ git commit +$ git push +Username for 'https://gist.github.com': +Password for 'https://tiimgreen@gist.github.com': +``` + +しかしながら、Gistではディレクトリーがサポートされていない。全てのファイルはリポジトリーのルートに置かれている必要がある。 + +[*Gistの作成についてもっと詳しく*](https://help.github.com/articles/creating-gists) + +### Git.io +[Git.io](http://git.io)はGitHubの提供するGitHub専用のシンプルな短縮URLサービスだ。 + +![Git.io](http://i.imgur.com/6JUfbcG.png?1) + +cURLを使って利用することができる: + +```bash +$ 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`を押すと現在閲覧しているリポジトリーから検索するフォームにフォーカスが当たる。ここでBackspaceを押し「This repository」という文字列を消すことでGitHub全体からの検索へと切り替えることができる + - issue画面で`l`を押すとラベルの編集インターフェイスが開かれる。 + - __ファイルを参照している時__(例: `https://github.com/tiimgreen/github-cheat-sheet/blob/master/README.md`)に`y`を押すと、参照している時の状態で固定されるURLに変更される。つまりそのファイルのコードが後に変化したとしても、そのURLでは今とまったく同じ状態で表示されるということだ。 + +`?`を押すとそのページで使える全ショートカットが表示されるだろう。 + +![Keyboard shortcuts](http://i.imgur.com/y5ZfNEm.png) + +[*検索機能についてもっと詳しく*](https://help.github.com/articles/search-syntax/) + +### コードの指定行の強調 +コードのURLの末尾に`#L52`と付けるか行番号をクリックすると、その行が強調表示される。 + +これは範囲指定も可能だ(例: `#L53-L60`)。こういった範囲を選択するには`shift`を押しながら二つの行をクリックしても良い: + +``` +https://github.com/rails/rails/blob/master/activemodel/lib/active_model.rb#L53-L60 +``` + +![Line Highlighting](http://i.imgur.com/8AhjrCz.png) + +### コミットからissueを閉じる +あるコミットでissueを解決した場合、コミットメッセージで`fix/fixes/fixed`や`close/closes/closed`、`resolve/resolves/resolved`に続けてissue番号を指定すると、そのコミットがmasterブランチにpushされると同時に指定issueが閉じられるだろう。 + +```bash +$ git commit -m "Fix screwup, fixes #12" +``` + +こうするとissue#12が閉じられ、閉じたissueにはそのコミットへの参照が自動的に追加される。 + +![Closing Repo](http://i.imgur.com/Uh1gZdx.png) + +[*コミット・メッセージからissueを閉じる方法についてもっと詳しく*](https://help.github.com/articles/closing-issues-via-commit-messages) + +### issueの相互リンク +同じリポジトリの違うissueへリンクを張り参照させたい場合、`#`に続けてissue番号を指定する。そうすると自動的にリンクが作成されるだろう。 + +別のリポジトリのissueの場合は`{user}/{repo}#ISSUE_NUMBER`とすれば良い(例: `tiimgreen/toc#12`)。 + +![Cross-Link Issues](https://camo.githubusercontent.com/447e39ab8d96b553cadc8d31799100190df230a8/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f626c6f672f323031312f736563726574732f7265666572656e6365732e706e67) + +### コメントのロック +リポジトリのオーナーや共同開発者ならば、pull requestやissueへのコメントをロックできるようになった。 + +![Lock conversation](https://cloud.githubusercontent.com/assets/2723/3221693/bf54dd44-f00d-11e3-8eb6-bb51e825bc2c.png) + +つまりそのプロジェクトの共同開発者ではないユーザーはコメントをすることができないということだ。 + +![Comments locked](https://cloud.githubusercontent.com/assets/2723/3221775/d6e513b0-f00e-11e3-9721-2131cb37c906.png) + +[*コメントのロックについてもっと詳しく*](https://github.com/blog/1847-locking-conversations) + +### pull requestでのCI結果の表示 +適切に設定を行えばpull requestを受け取るたびに、通常のコミットと同じように[Travis CI](https://travis-ci.org/)がそのpull requestをビルドするだろう。どう設定するかは[Travis CI: Getting started](http://docs.travis-ci.com/user/getting-started/)を読むと良い。 + +[![Travis CI status](https://cloud.githubusercontent.com/assets/1687642/2700187/3a88838c-c410-11e3-9a46-e65e2a0458cd.png)](https://github.com/octokit/octokit.rb/pull/452) + +[*コミット・ステータスAPIについてもっと詳しく*](https://github.com/blog/1227-commit-status-api) + +### 絞り込み検索 +issueとpull requestの検索インターフェイスでは、絞り込みをかけることが可能だ。 + +例えば[Railsのリポジトリー](https://github.com/rails/rails/issues)では、以下の様な絞り込み検索により"activerecord"というラベルがついたissueのみを表示することができる: + +`is:issue label:activerecord` + +逆に"activerecord"というラベルが*ついていない*issueのみを表示することもできる: + +`is:issue -label:activerecord` + +そして、この絞り込み検索はpull requestに対しても行うことができる: + +`is:pr -label:activerecord` + +GitHubでは開かれているissueやpull requestのみを表示するタブ、または既に閉じられたそれらのみを表示するタブが用意されているが、絞り込み検索によってマージ済みのpull requestのみを表示することもできる。以下のようなフィルターを使えば良いだろう: + +`is:merged` + +[*issueの検索についてもっと詳しく*](https://help.github.com/articles/searching-issues) + +そしてGitHubはStatus APIの結果を使ってフィルターすることもできるようになった。 + +Status APIでsuccessが設定されたpull requestのみ: + +`status:success` + +[*Status APIを使った絞り込み検索についてもっと詳しく*](https://github.com/blog/2014-filter-pull-requests-by-status) + +### Markdownファイルでの構文強調 +例えばMarkdownファイルでRubyのコードを構文強調したいならば以下のようにする: + + ```ruby + require 'tabbit' + table = Tabbit.new('Name', 'Email') + table.add_row('Tim Green', 'tiimgreen@gmail.com') + puts table.to_s + ``` + +こうすると以下のように表示されることになる: + +```ruby +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)を使って言語を判別し構文強調を行っている。構文強調がサポートされている言語の一覧は[言語定義YAMLファイル](https://github.com/github/linguist/blob/master/lib/linguist/languages.yml)を参照すればわかるだろう。 + +[*GitHub Flavored Markdownについてもっと詳しく*](https://help.github.com/articles/github-flavored-markdown) + +### Emoji +Emojiはpull requestやissue、コミット・メッセージ、リポジトリーの概要などで`:name_of_emoji:`と書くと利用できる: + +GitHubでサポートされているEmojiの完全なリストは[Emoji cheat sheet for Campfire and GitHub](http://www.emoji-cheat-sheet.com/)か[All-Github-Emoji-Icons](https://github.com/scotch-io/All-Github-Emoji-Icons)で確認できる。 +素敵なemojiの検索は[emoji.muan.co](http://emoji.muan.co/)。 + +GitHubで使われているEmojiのトップ5は以下の通りだ: + +1. `:shipit:` +2. `:sparkles:` +3. `:-1:` +4. `:+1:` +5. `:clap:` + +### 画像及びアニメーションGIF +画像やアニメーションGIFはコミットのコメントやREADMEなどで利用できる: + +``` +![Alt Text](http://www.sheawong.com/wp-content/uploads/2013/08/keephatin.gif) +``` + +リポジトリにある画像も直接参照することが出来る: + +``` +![Alt Text](https://github.com/{user}/{repo}/raw/master/path/to/image.gif) +``` + +![Peter don't care](http://www.sheawong.com/wp-content/uploads/2013/08/keephatin.gif) + +あらゆる画像はGitHubでキャッシュされるので、画像のホスティング先が落ちていたとしても変わらず表示されるだろう。 + +#### GitHub Wikiへの画像の添付 +GitHub Wikiで画像を追加する方法がいくつかある。通常のMarkdown記法(前節を参照)はもちろん使える。しかしそれだけではなく、画像の幅と高さを指定する記法も使うことができる: + +```markdown +[[ http://www.sheawong.com/wp-content/uploads/2013/08/keephatin.gif | height = 100px ]] +``` + +こうすると以下のようになる: + +![Just a screenshot](http://i.imgur.com/J5bMf7S.png) + +### 素早く引用 +issueのスレッドで他の人のコメントを引用してコメントしたい場合、引用したい文章を選択した状態で`r`を押すと、ブロック引用の記法を使ってテキストエリアにコピーされる。 + +![Quick Quote](http://i.imgur.com/TzpMIOA.png) + +[*素早く引用する方法についてもっと詳しく*](https://github.com/blog/1399-quick-quotes) + +### コメントへのクリップボードの画像の追加 + +_(Chrome系のブラウザーのみで動作)_ + +スクリーンショットをクリップボードに保存(mac: `cmd-ctrl-shift-4`)した後、その画像はコメント投稿フォームで貼り付け(`cmd-v`または`ctrl-v`)ることができ、自動的にGitHubへアップロードされます。 + +![Pasting Clipboard Image to Comments](https://cloud.githubusercontent.com/assets/39191/5794265/39c9b65a-9f1b-11e4-9bc7-04e41f59ea5f.png) + +[*コメントへのクリップボードの画像の追加についてもっと詳しく*](https://help.github.com/articles/issue-attachments) + +### 設定済みライセンスの追加 +GitHub上でリポジトリを作成する時、あらかじめ設定されているライセンスを追加することもできる: + +![Licese](http://i.imgur.com/Chqj4Fg.png) + +既に存在するリポジトリであってもウェブ上のインターフェイスからファイルを作成することで追加できる。`LICENSE`というファイル名にした場合、ライセンスを選択するオプションが表示されるのだ: + +![License](http://i.imgur.com/fTjQict.png) + +`.gitignore`も同じように作成時に追加することも、後で追加することもできる。 + +[*オープンソース・ライセンスについてもっと詳しく*](https://help.github.com/articles/open-source-licensing) + +### タスクリスト +issueやpull requestでは以下のように(空白に注意)書くとチェックボックスを作成することができる: + +``` +- [ ] Be awesome +- [ ] Prepare dinner + - [ ] Research recipe + - [ ] Buy ingredients + - [ ] Cook recipe +- [ ] Sleep +``` + +![Task List](http://i.imgur.com/jJBXhsY.png) + +これらチェックボックスにチェックが入れられると、同時に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] Mars +- [ ] Jupiter +``` + +- [ ] Mercury +- [x] Venus +- [x] Earth +- [x] Mars +- [ ] Jupiter + +[*Markdownファイルでのタスクリストについてもっと詳しく*](https://github.com/blog/1825-task-lists-in-all-markdown-documents) + +### 相対リンク +Markdownファイルでリポジトリ内のコンテンツへ張る場合、相対リンクを利用することが推奨されている。 + +```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というプラグインがインストールされているので、[Emoji](#emoji)や[@mentions](https://github.com/blog/821)はJekyllの投稿やページでGitHub.com上と同じように動作する。 + +[*GitHub Pageでのメタデータとプラグインのサポートについてもっと詳しく*](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 metadata](https://camo.githubusercontent.com/47245aa16728e242f74a9a324ce0d24c0b916075/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f36343035302f313232383236372f65303439643063362d323761302d313165332d396464382d6131636432323539393334342e706e67) + +[*文書内のYAMLメタデータの表示についてもっと詳しく*](https://github.com/blog/1647-viewing-yaml-metadata-in-your-documents) + +### 表形式のデータ表示 +GitHubでは`.csv`(カンマ区切り)と`.tsv`(タブ区切り)の形式で書かれた表を整形して表示する機能をサポートしている。 + +![Tabular data](https://camo.githubusercontent.com/1b6dd0157ffb45d9939abf14233a0cb13b3b4dfe/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f3238323735392f3937363436322f33323038336463652d303638642d313165332d393262322d3566323863313061353035392e706e67) + +[*表形式のデータ表示についてもっと詳しく*](https://github.com/blog/1601-see-your-csvs) + +### PDF表示 +GitHubではPDFの表示をサポートしている: + +![PDF](https://cloud.githubusercontent.com/assets/1000669/7492902/f8493160-f42e-11e4-8cea-1cb4f02757e7.png) + +[*PDF表示についてもっと詳しく*](https://github.com/blog/1974-pdf-viewing) + +### pull requestの取り消し +pull requestをマージした後、意味がなかったことがわかったり、そのpull requestをマージしたのは間違いだったことがわかることもあるだろう。 + +その取り消しは、pull requestのページに表示されているマージ・コミットの右端にある**Revert**ボタンをクリックすることにより、そのpull requestで行われた変更を取り消すpull requestを作成することによって行うことができる。 + +![Revert button](https://camo.githubusercontent.com/0d3350caf2bb1cba53123ffeafc00ca702b1b164/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f68656c702f70756c6c5f72657175657374732f7265766572742d70756c6c2d726571756573742d6c696e6b2e706e67) + +[*pull requestの取り消しについてもっと詳しく*](https://github.com/blog/1857-introducing-the-revert-button) + +### 差分の表示 +#### レンダリング済みの差分表示 +コミットやpull requestにGitHubでレンダリングされて表示されるもの(例: Markdown)が含まれる場合、その*ソース*と*レンダリング済み*の両方の差分を見ることができる。 + +![Source / Rendered view](https://github-images.s3.amazonaws.com/help/repository/rendered_prose_diff.png) + +レンダリングされた状態での差分を表示したい場合は「Rendered」ボタンをクリックする。レンダリング済みの差分表示では文章の追加や削除、編集がよりわかりやすい: + +![Rendered Prose Diffs](https://f.cloud.github.com/assets/17715/2003056/3997edb4-862b-11e3-90be-5e9586edecd7.png) + +[*レンダリング済みの差分表示についてもっと詳しく*](https://github.com/blog/1784-rendered-prose-diffs) + +#### マップ差分の可視化 +コミットやpull requestにジオデータの変更が含まれている場合はいつも、GitHubではそのジオデータの変化を可視化してくれるだろう。 + +[![Diffable Maps](https://f.cloud.github.com/assets/282759/2090660/63f2e45a-8e97-11e3-9d8b-d4c8078b004e.gif)](https://github.com/benbalter/congressional-districts/commit/2233c76ca5bb059582d796f053775d8859198ec5) + +[*マップ差分の可視化についてもっと詳しく*](https://github.com/blog/1772-diffable-more-customizable-maps) + +#### 差分表示の前後を表示 +差分表示の行番号付近にある*展開*ボタンを使うと、その前後の行をクリックして表示させることができる。*展開*ボタンを押し続けることによってファイル全体を表示することもできるし、またこの機能はあらゆるGitHubの差分表示ビューに用意されている。 + +![Expanding Context in Diffs](https://f.cloud.github.com/assets/22635/1610539/863c1f64-5584-11e3-82bf-151b406a272f.gif) + +[*差分表示の前後を表示についてもっと詳しく*](https://github.com/blog/1705-expanding-context-in-diffs) + +#### pull requestの内容をDIFFまたはPATCH形式で取得 +pull requestによる差分はそのURLの末尾に`.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 + +(...) +``` + +#### 画像の表示とDIFF +GitHubは、PNGやJPG、GIF、PSDといった多くの一般的な画像形式の表示をサポートしている。それに加え、様々な方法でこれら画像形式のバージョンごとの差分を比較することもできる。 + +[![Diffable PSD](https://cloud.githubusercontent.com/assets/2546/3165594/55f2798a-eb56-11e3-92e7-b79ad791a697.gif)](https://github.com/blog/1845-psd-viewing-diffing) + +[*画像の表示とDIFFについてもっと詳しく*](https://help.github.com/articles/rendering-and-diffing-images) + +### Hub +[Hub](https://github.com/github/hub)はGitのラッパーとして機能するコマンドライン・ツールで、これを利用するとGitHubをコマンドラインからとても簡単に扱えるようになる。 + +例えば以下のようにしてリポジトリのクローンが行える: + +```bash +$ hub clone tiimgreen/toc +``` + +これが以下のコマンドの代わりというわけだ: + +```bash +$ git clone https://github.com/tiimgreen/toc.git +``` + +[*Hubが提供する便利な機能についてもっと詳しく*](https://github.com/github/hub#commands) + +### 開発参加のガイドライン +リポジトリのルートに`CONTRIBUTING`という名前のファイルを置くと、issueやpull requestを作成しようとした時にそれへのリンクが表示されるようになる。 + +![Contributing Guidelines](https://camo.githubusercontent.com/71995d6b0e620a9ef1ded00a04498241c69dd1bf/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f736b697463682f6973737565732d32303132303931332d3136323533392e6a7067) + +[*開発参加のガイドラインについてもっと詳しく*](https://github.com/blog/1184-contributing-guidelines) + +### Octicons +GitHubで使われているアイコン(Octicons)はオープンソース化された。 + +![Octicons](https://og.github.com/octicons/octicons@1200x630.png) + +[*GitHubのOcticonsについてもっと詳しく*](https://octicons.github.com) + +### 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 Talks +| 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 +### すべての削除済みファイルをワーキング・ツリーから削除する +例えば`/bin/rm`を使って大量のファイルを削除した場合、以下のコマンドを使えばワーキング・ツリー、そしてインデックスからも削除することができ、ファイルごとにそれぞれGitコマンドを実行する必要がなくなる: + +```bash +$ git rm $(git ls-files -d) +``` + +例えば以下のように実行される: + +```bash +$ git status +On branch master +Changes not staged for commit: + deleted: a + deleted: c + +$ git rm $(git ls-files -d) +rm 'a' +rm 'c' + +$ git status +On branch master +Changes to be committed: + deleted: a + deleted: c +``` + +### 直前のブランチ +Gitで直前のブランチへ移動するには: + +```bash +$ 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) + +### 空白の削除 +Gitの`stripspace`コマンドは以下の作業を行う: + +- 行末の空白文字の削除 +- 空白行の取りまとめ +- ファイル末尾への改行の追加 + +このコマンドを呼ぶ時はファイルを渡さねばならない。例: + +```bash +$ git stripspace < README.md +``` + +[*Gitの`stripspace`コマンドについてもっと詳しく*](http://git-scm.com/docs/git-stripspace) + +### SSH公開鍵 +以下のURLにアクセスすることで、SSH公開鍵の一覧をテキスト形式で取得することができる: + +``` +https://github.com/{user}.keys +``` + +例: [https://github.com/tiimgreen.keys](https://github.com/tiimgreen.keys) + +[*SSH公開鍵についてもっと詳しく*](https://changelog.com/github-exposes-public-ssh-keys-for-its-users/) + +### pull requestのチェックアウト +pull requestはGitHubのリポジトリでは特別なブランチであり、様々な方法でローカルに取り込むことができる: + +特定のpull requestを取り込み、一時的に`FETCH_HEAD`として保存すると、素早く差分を確認してマージすることが可能だ: + +```bash +$ git fetch origin refs/pull/[PR-Number]/head +``` + +参照仕様を使うとすべてのpull requestをローカル・ブランチとして取り込むことができる: + +```bash +$ 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に対しては、そのpull requestを参照するリモート・ブランチから直接ローカル・ブランチとしてチェックアウトすると便利だろう: + +```bash +$ git checkout pr/42 pr-42 +``` + +もしくは様々なリポジトリで作業をするのなら、代わりにグローバルのGit設定で行うことにより、pull requestの取得をグローバルに設定すると良いだろう。 + +```bash +git config --global --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*" +``` + +こうすると以下の様な短いコマンドを利用することが可能になる: + +```bash +git fetch origin +``` + +```bash +git checkout pr/42 +``` + +[*pull requestのチェックアウトについてもっと詳しく*](https://help.github.com/articles/checking-out-pull-requests-locally) + +### 空のコミット +`--allow-empty`オプションを付けると、コードの変化がなくてもコミットを作成することができる: + +```bash +$ git commit -m "Big-ass commit" --allow-empty +``` + +この機能の使い方(便利なもの)としては以下のようなものが挙げられる: + + - 新たな機能や大きな変更を事前に通知する時 + - コード上に現れないような変更をプロジェクトに加えた時 + - リポジトリを利用して誰かと連絡を取りたい時 + - リポジトリへの最初のコミットをやり直しできるようにしたい時: `git commit -m "init repo" --allow-empty`. + +### Gitステータスのスタイリング +普通に実行すると: + +```bash +$ git status +``` + +このように表示されるが: + +![git status](http://i.imgur.com/qjPyvXb.png) + +`-sb`を追加することによって: + +```bash +$ git status -sb +``` + +このように表示することもできる: + +![git status -sb](http://i.imgur.com/K0OY3nm.png) + +[*Gitの`status`コマンドについてもっと詳しく*](http://git-scm.com/docs/git-status) + +### Gitログのスタイリング +以下のように実行すると: + +```bash +$ git log --all --graph --pretty=format:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative +``` + +このように表示される: + +![git log --all --graph --pretty=format:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative](http://i.imgur.com/58eOtkW.png) + +この設定は[Palesz](http://stackoverflow.com/users/88355/palesz)が考えたものだ。 + +*これは[後述の手順](#%E3%82%A8%E3%82%A4%E3%83%AA%E3%82%A2%E3%82%B9)に従ってエイリアスへ追加することもできる。* + +[*Gitの`log`コマンドについてもっと詳しく*](http://git-scm.com/docs/git-log) + +### コミットログの検索 +指定した文字列を今までのコミット・メッセージから検索して、もっとも新しいものを表示することができる。 + +```bash +$ git show :/query +``` + +`query`を検索したい文字列(大文字と小文字を区別する)で置き換えると、最新のコミットがそのコミットにおける差分と同時に表示される。 + +```bash +$ git show :/typo +``` +![git show :/query](http://i.imgur.com/icaGiNt.png) + +注: 終了するには`q`を押す。 + +### Git Grep +Git Grepは指定したパターンに一致する行の一覧を返してくれる。 + +実行すると: + +```bash +$ git grep aliases +``` + +*aliases*という文字を含むファイルを全て表示することだろう。 + +![git grep aliases](http://i.imgur.com/DL2zpQ9.png) + +注: 終了するには`q`を押す。 + +複数のフラグを組み合わせることで、より高度な検索を行うことができる。例えば: + + * `-e` 次のパラメーターをパターンとする (例: regex) + * `--and`、`--or`と`--not` 複数のパターンを組み合わせる + +以下のようにして使う: + +```bash +$ git grep -e pattern --and -e anotherpattern +``` + +[*Gitの`grep`コマンドについてもっと詳しく*](http://git-scm.com/docs/git-grep) + +### マージ済みブランチ +以下のように実行すると: + +```bash +$ git branch --merged +``` + +現在のブランチに既にマージされたブランチの一覧が表示される。 + +逆に: + +```bash +$ git branch --no-merged +``` + +こうするとまだマージされていないブランチが表示されるだろう。 + +[*Gitの`branch`コマンドについてもっと詳しく*](http://git-scm.com/docs/git-branch) + +### FixupとAutosquash +これまでのコミット(HEADの直前でなくても構わない)、例えば`abcde`というチェックサムのコミットで何か問題を見つけた場合、以下の様なコマンドで問題の修正を行うことができる: + +```bash +$ git commit --fixup=abcde +$ git rebase abcde^ --autosquash -i +``` + +[*Gitの`commit`コマンドについてもっと詳しく*](http://git-scm.com/docs/git-commit) +[*Gitの`rebase`コマンドについてもっと詳しく*](http://git-scm.com/docs/git-rebase) + +### ローカル・リポジトリを参照するウェブサーバー +Gitの`instaweb`コマンドを利用すると、自分の作業リポジトリを`gitweb`で参照することができる。このコマンドは`gitweb`とウェブサーバーをセットアップしてローカル・リポジトリをブラウザーで開けるようにする簡単なスクリプトだ。 + +```bash +$ git instaweb +``` + +以下のようなページが開かれる: + +![Git instaweb](http://i.imgur.com/Dxekmqc.png) + +[*Gitの`instaweb`コマンドについてもっと詳しく*](http://git-scm.com/docs/git-instaweb) + +### Gitの設定 +`.gitconfig`とはあらゆる設定が書き込まれるファイルだ。 + +#### エイリアス +エイリアスは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 +``` + +またはコマンドラインからも設定できる: + +```bash +$ git config --global alias.new_alias git_function +``` + +例: + +```bash +$ git config --global alias.cm commit +``` + +注: エイリアスが複数のコマンドからなる場合はクオートで括る必要がある: + +```bash +$ git config --global alias.ac 'add -A . && commit' +``` + +おすすめの設定を挙げておこう: + +| エイリアス | コマンド | 設定方法 | +| --- | --- | --- | +| `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 cleanup` | `git branch --merged \| grep -v '*' \| xargs git branch -d` | `git config --global alias.cleanup "!git branch --merged \| grep -v '*' \| xargs git branch -d"` | +| `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`とタイプした場合、以下のような出力を得ることだろう: + +```bash +$ git comit -m "Message" +# git: 'comit' is not a git command. See 'git --help'. + +# Did you mean this? +# commit +``` + +これを`comit`とタイプした時に`commit`を実行させたい場合、自動修正を有効にすれば良い: + +```bash +$ git config --global help.autocorrect 1 +``` + +すると以下のような出力を得るようになるだろう: + +```bash +$ 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の出力をカラフルにするには以下のような設定を加えると良い: + +```bash +$ 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/ | +| 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/ | +| Git for Computer Scientists | http://eagain.net/articles/git-for-computer-scientists/ | +| Git Magic | http://www-cs-students.stanford.edu/~blynn/gitmagic/ | +| GitHub Training Kit | http://training.github.com/kit | +| Git Visualization Playground | http://onlywei.github.io/explain-git-with-d3/#freeplay | + +#### Git Books +| 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 PluralSight | https://github.com/pluralsight/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 | + +## 訳注 +これは[GitHub Cheat Sheet](https://github.com/tiimgreen/github-cheat-sheet)の日本語訳である。 diff --git a/README.ko.md b/README.ko.md new file mode 100644 index 0000000..0116ba3 --- /dev/null +++ b/README.ko.md @@ -0,0 +1,1110 @@ +# GitHub 치트 시트 [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/sindresorhus/awesome) + +Git과 GitHub에서 꽤 유용하지만 숨겨져 있는 기능들에 대해 다룹니다. 이 치트 시트는 [Zach Holman](https://github.com/holman)이 Aloha Ruby Conference 2012에서 발표한 [Git과 GitHub의 비밀들](http://confreaks.tv/videos/aloharuby2012-git-and-github-secrets) ([슬라이드](https://speakerdeck.com/holman/git-and-github-secrets))과 WDCNZ 2013에서 발표한 [Git과 GitHub에 대한 더 많은 비밀들](https://vimeo.com/72955426) ([슬라이드](https://speakerdeck.com/holman/more-git-and-github-secrets))에서 영감을 받아 작성되었습니다. + +*단축주소: [`http://git.io/sheet`](http://git.io/sheet)* + +*Read this in other languages: [English](README.md), [한국어](README.ko.md), [日本語](README.ja.md), [简体中文](README.zh-cn.md), [正體中文](README.zh-tw.md).* + +# 목록 + + - [GitHub](#github) + - [Ignore Whitespace](#ignore-whitespace) + - [Adjust Tab Space](#adjust-tab-space) + - [Commit History by Author](#commit-history-by-author) + - [Cloning a Repository](#cloning-a-repository) + - [Branch](#branch) + - [Compare all Branches to Another Branch](#compare-all-branches-to-another-branch) + - [Comparing Branches](#comparing-branches) + - [Compare Branches across Forked Repositories](#compare-branches-across-forked-repositories) + - [Gists](#gists) + - [Git.io](#gitio) + - [Keyboard Shortcuts](#keyboard-shortcuts) + - [Line Highlighting in Repositories](#line-highlighting-in-repositories) + - [Closing Issues via Commit Messages](#closing-issues-via-commit-messages) + - [Cross-Link Issues](#cross-link-issues) + - [Locking Conversations](#locking-conversations) + - [CI Status on Pull Requests](#ci-status-on-pull-requests) + - [Filters](#filters) + - [Syntax Highlighting in Markdown Files](#syntax-highlighting-in-markdown-files) + - [Emojis](#emojis) + - [Images/GIFs](#imagesgifs) + - [Embedding Images in GitHub Wiki](#embedding-images-in-github-wiki) + - [Quick Quoting](#quick-quoting) + - [Pasting Clipboard Image to Comments](#pasting-clipboard-image-to-comments) + - [Quick Licensing](#quick-licensing) + - [Task Lists](#task-lists) + - [Task Lists in Markdown Documents](#task-lists-in-markdown-documents) + - [Relative Links](#relative-links) + - [Metadata and Plugin Support for GitHub Pages](#metadata-and-plugin-support-for-github-pages) + - [Viewing YAML Metadata in your Documents](#viewing-yaml-metadata-in-your-documents) + - [Rendering Tabular Data](#rendering-tabular-data) + - [Rendering PDF](#rendering-pdf) + - [Revert a Pull Request](#revert-a-pull-request) + - [Diffs](#diffs) + - [Rendered Prose Diffs](#rendered-prose-diffs) + - [Diffable Maps](#diffable-maps) + - [Expanding Context in Diffs](#expanding-context-in-diffs) + - [Diff or Patch of Pull Request](#diff-or-patch-of-pull-request) + - [Rendering and diffing images](#rendering-and-diffing-images) + - [Hub](#hub) + - [Contribution Guidelines](#contribution-guidelines) + - [CONTRIBUTING file](#contributing-file) + - [ISSUE_TEMPLATE file](#issue_template-file) + - [PULL_REQUEST_TEMPLATE file](#pull_request_template-file) + - [Octicons](#octicons) + - [GitHub Resources](#github-resources) + - [GitHub Talks](#github-talks) + - [Git](#git) + - [Remove All Deleted Files from the Working Tree](#remove-all-deleted-files-from-the-working-tree) + - [Previous Branch](#previous-branch) + - [Stripspace](#stripspace) + - [SSH keys](#ssh-keys) + - [Checking out Pull Requests](#checking-out-pull-requests) + - [Empty Commits](#empty-commits) + - [Styled Git Status](#styled-git-status) + - [Styled Git Log](#styled-git-log) + - [Git Query](#git-query) + - [Git Grep](#git-grep) + - [Merged Branches](#merged-branches) + - [Fixup and Autosquash](#fixup-and-autosquash) + - [Web Server for Browsing Local Repositories](#web-server-for-browsing-local-repositories) + - [Git Configurations](#git-configurations) + - [Aliases](#aliases) + - [Auto-Correct](#auto-correct) + - [Color](#color) + - [Git Resources](#git-resources) + - [Git Books](#git-books) + +## GitHub + +### Ignore Whitespace + +`?w=1`를 diff URL에 추가하면 변경된 코드만 볼 수 있도록, 공백만 바뀐 수정을 제거할 수 있습니다. + +![Diff without whitespace](https://camo.githubusercontent.com/797184940defadec00393e6559b835358a863eeb/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f626c6f672f323031312f736563726574732f776869746573706163652e706e67) + +[*GitHub의 비밀을 더 읽어 보세요.*](https://github.com/blog/967-github-secrets) + +### Adjust Tab Space + +diff나 파일 URL에 `?ts=4`를 덧붙이면 탭 문자의 크기를 기본값인 공백 8개 대신 4개로 보여줍니다. `ts` 뒤의 숫자는 설정에 맞게 변경 하실 수 있습니다. gist나 raw 파일 보기에는 적용 되지 않습니다만, [크롬 확장 프로그램](https://chrome.google.com/webstore/detail/github-tab-size/ofjbgncegkdemndciafljngjbdpfmbkn)이나 으로 자동화 할 수 있습니다. + +여기에 있는 Go 소스 파일은 `?ts=4`를 붙이기 전에는 이렇습니다. + +![Before, tab space example](http://i.imgur.com/GIT1Fr0.png) + +그리고 `?ts=4`를 붙인 다음에는 이렇게 됩니다. + +![After, tab space example](http://i.imgur.com/70FL4H9.png) + +### Commit History by Author + +특정 커미터가 한 모든 커밋을 보고 싶다면 URL에 `?author={user}`를 추가하세요. + +``` +https://github.com/rails/rails/commits/master?author=dhh +``` + +![DHH commit history](http://i.imgur.com/S7AE29b.png) + +[*커밋 뷰간의 차이에 대해 더 읽어보세요.*](https://help.github.com/articles/differences-between-commit-views) + +### Cloning a Repository + +저장소를 클론할 때, 맨 뒤의 `.git`은 생략할 수 있습니다. + +```bash +$ git clone https://github.com/tiimgreen/github-cheat-sheet +``` + +[*Git `clone` 명령에 대해 더 읽어 보세요.*](http://git-scm.com/docs/git-clone) + +### Branch + +#### Compare all Branches to Another Branch + +저장소의 [브랜치](https://github.com/tiimgreen/github-cheat-sheet/branches) 페이지는, 커밋 버튼의 뒤에 있습니다. + +``` +https://github.com/{user}/{repo}/branches +``` + +... 메인 브랜치에 머지되지 않은 모든 브랜치의 리스트를 볼 수 있습니다. + +여기에서 버튼 클릭으로 비교 페이지에 접속하거나 브랜치를 삭제할 수 있습니다. + +![Compare branches not merged into master in rails/rails repo - https://github.com/rails/rails/branches](http://i.imgur.com/0FEe30z.png) + +#### Comparing Branches + +GitHub에서 브랜치 비교를 하시려면, 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](http://i.imgur.com/tIRCOsK.png) + +`{range}`는 이렇게 적을 수도 있습니다. + +``` +https://github.com/rails/rails/compare/master@{1.day.ago}...master +https://github.com/rails/rails/compare/master@{2014-10-04}...master +``` + +*날짜는 `YYYY-MM-DD` 형식으로 적습니다* + +![Another compare example](http://i.imgur.com/5dtzESz.png) + +브랜치는 `diff`나 `patch` 뷰로도 확인할 수 있습니다. + +``` +https://github.com/rails/rails/compare/master...4-1-stable.diff +https://github.com/rails/rails/compare/master...4-1-stable.patch +``` + +[*시간으로 커밋을 비교하는 법에 대해 더 읽어 보세요.*](https://help.github.com/articles/comparing-commits-across-time) + +#### Compare branches across forked repositories + +포크된 저장소 간의 브랜치를 비교하려면 URL을 이렇게 변경하세요. + +``` +https://github.com/{user}/{repo}/compare/{foreign-user}:{branch}...{own-branch} +``` + +예를 들면 + +``` +https://github.com/rails/rails/compare/byroot:idempotent-counter-caches...master +``` + +![Forked branch compare](http://i.imgur.com/Q1W6qcB.png) + +### Gists + +[Gist](https://gist.github.com/)는 본격적인 저장소를 만들지 않고, 작은 코드 조각과 일할 수 있는 쉬운 방법입니다. + +![Gist](http://i.imgur.com/VkKI1LC.png?1) + +[이렇게](https://gist.github.com/tiimgreen/10545817.pibb) Gist URL 뒤에 `.pibb`를 넣으면 다른 사이트에 첨부할 수 있는 *HTML 온리* 버전을 만들 수 있습니다. + +Gist는 저장소처럼 취급할 수 있고 클론도 됩니다. + +```bash +$ git clone https://gist.github.com/tiimgreen/10545817 +``` + +![Gists](http://i.imgur.com/BcFzabp.png) + +이는 Gists에서도 수정하고 업데이트를 푸시할 수 있다는 의미입니다. + +```bash +$ git commit +$ git push +Username for 'https://gist.github.com': +Password for 'https://tiimgreen@gist.github.com': +``` + +하지만, Gists는 디렉터리를 지원하지 않습니다. 모든 파일은 저장소의 루트에 넣을 필요가 있습니다. +[*Gist를 만드는 법에 대해 더 읽어보세요.*](https://help.github.com/articles/creating-gists) + +### Git.io + +[Git.io](http://git.io)는 GitHub를 위한 간단한 URL 단축기입니다. + +![Git.io](http://i.imgur.com/6JUfbcG.png?1) + +Curl으로 순수 HTTP를 통해 사용하실 수도 있습니다. + +```bash +$ 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) + +### Keyboard Shortcuts + +저장소 페이지에서 단축키를 사용하면 쉽게 이동할 수 있습니다. + + - `t`를 누르면 파일 탐색 페이지로 이동합니다. + - `w`를 누르면 브랜치 선택 페이지로 이동합니다. + - `s`를 누르면 현재 저장소의 검색창으로 포커스가 이동합니다. 백스페이스를 누르면 채워져 있는 "This repository"를 지워 GitHub 전체에서 검색할 수 있게 됩니다. + - `l`을 누르면 이슈의 라벨을 수정할 수 있습니다. + - __파일을 보고 있을 때__ (예를 들어 `https://github.com/tiimgreen/github-cheat-sheet/blob/master/README.md`) `y`를 누르면 URL을 지금 보고 있는 페이지를 사실상 고정하도록 합니다. 코드가 바뀐다고 해도 이번에 본 내용을 다시 볼 수 있습니다. + +지금 페이지에서 쓸 수 있는 모든 단축키를 보시려면 `?`를 누르세요. + +![Keyboard shortcuts](http://i.imgur.com/y5ZfNEm.png) + +[*사용할 수 있는 검색 문법에 대해 더 읽어 보세요.*](https://help.github.com/articles/search-syntax/) + +### Line Highlighting in Repos + +코드 파일 URL의 끝에 `#L52`를 추가하거나 줄 번호를 클릭하면 그 줄 번호를 하이라이트합니다. + +`#L53-L60`처럼 범위 지정도 가능합니다. 범위를 지정하려면 `shift`키를 누른 채 두 라인을 클릭하면 됩니다. + +``` +https://github.com/rails/rails/blob/master/activemodel/lib/active_model.rb#L53-L60 +``` + +![Line Highlighting](http://i.imgur.com/8AhjrCz.png) + +### Closing Issues with Commits + +어떤 커밋이 이슈를 고쳤다면, `fix/fixes/fixed`, `close/closes/closed`, `resolve/resolves/resolved`를 키워드로 해당 이슈가 마스터 브랜치에 커밋 될 때 닫을 수 있습니다. + +```bash +$ git commit -m "Fix screwup, fixes #12" +``` + +이렇게 하면 이슈를 닫고 클로징 커밋으로 참조하게 합니다. + +![Closing Repo](http://i.imgur.com/Uh1gZdx.png) + +[*커밋 메시지로 이슈 닫기에 대해 더 읽어 보세요.*](https://help.github.com/articles/closing-issues-via-commit-messages) + +### Cross-Link Issues + +같은 저장소의 다른 이슈를 링크하기 원한다면, `#` 뒤에 이슈 번호만 입력하시면 자동으로 링크됩니다. + +다른 저장소의 이슈를 링크하고 싶다면 `{user}/{repo}#이슈_번호`로 할 수 있습니다. (예 `tiimgreen/toc#12`) + +![Cross-Link Issues](https://camo.githubusercontent.com/447e39ab8d96b553cadc8d31799100190df230a8/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f626c6f672f323031312f736563726574732f7265666572656e6365732e706e67) + +### Locking Conversations + +풀 리퀘스트와 이슈는 저장소의 주인이나 협업자가 잠글 수 있습니다. + +![Lock conversation](https://cloud.githubusercontent.com/assets/2723/3221693/bf54dd44-f00d-11e3-8eb6-bb51e825bc2c.png) + +이는 프로젝트의 협업자가 아닌 사람은 더 이상 댓글을 달 수 없게 된다는 말입니다. + +![Comments locked](https://cloud.githubusercontent.com/assets/2723/3221775/d6e513b0-f00e-11e3-9721-2131cb37c906.png) + +[*대화 잠그기에 대해 더 읽어 보세요.*](https://github.com/blog/1847-locking-conversations) + +### CI Status on Pull Requests + +정확히 설정되었다면, 풀 리퀘스트를 받거나 풀 리퀘스트에 새로운 커밋을 할 때마다, [Travis CI](https://travis-ci.org/)가 풀 리퀘스트를 빌드 할 것입니다. [Travis CI 시작하기](http://docs.travis-ci.com/user/getting-started/)를 좀 더 읽어보세요. + +[![Travis CI status](https://cloud.githubusercontent.com/assets/1687642/2700187/3a88838c-c410-11e3-9a46-e65e2a0458cd.png)](https://github.com/octokit/octokit.rb/pull/452) + +[*커밋 상태 API에 대해 좀 더 읽어보세요.*](https://github.com/blog/1227-commit-status-api) + +### Filters + +이슈, 풀 리퀘스트 둘 다 유저 인터페이스에서 필터링이 가능합니다. + +레일스 저장소 에서, "activerecord" 라벨을 +선택하여 다음 필터를 만들 수 있습니다. + +`is:issue label:activerecord` + +하지만, activerecord 라벨이 없는 모든 이슈를 찾을 수도 있습니다. + +`is:issue -label:activerecord` + +덧붙여, 풀 리퀘스트에도 동작합니다. + +`is:pr -label:activerecord` + +GitHub는 열리거나 닫힌 이슈와 풀 리퀘스트를 위한 탭이 있습니다만 머지된 +풀 리퀘스트도 볼 수 있습니다. 다음 필터를 입력하기만 하면 됩니다. + +`is:merged` + +[*이슈 검색에 대해 좀 더 읽어보세요.*](https://help.github.com/articles/searching-issues) + +마지막으로 이제 상태 API의 상태에 의한 필터도 가능해졌습니다. + +성공 상태의 풀 리퀘스트는 이렇게 검색합니다. + +`status:success` + +[*상태 API 검색에 대해 좀 더 읽어보세요.*](https://github.com/blog/2014-filter-pull-requests-by-status) + +### Syntax Highlighting in Markdown Files + +마크다운에서 예를 들어, 루비 코드를 신텍스 하이라이트 하려면 이렇게 합니다. + + ```ruby + require 'tabbit' + table = Tabbit.new('Name', 'Email') + table.add_row('Tim Green', 'tiimgreen@gmail.com') + puts table.to_s + ``` + +이렇게 보입니다. + +```ruby +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)를 사용해 언어를 감지하고 신택스를 하이라이트합니다. [언어 YAML 파일](https://github.com/github/linguist/blob/master/lib/linguist/languages.yml)을 정독하시면 어떤 키워드가 유효한지 확인하실 수 있습니다. + +[*GitHub Flavored 마크다운에 대해 더 읽어 보세요.*](https://help.github.com/articles/github-flavored-markdown) + +### Emojis + +에모지는 풀 리퀘스트, 이슈, 커밋 메시지, 저장소 설명 등에 `:에모지의_이름:`으로 넣을 수 있습니다. + +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)에서 확인하실 수 있습니다. +편리한 에모지 검색 엔진은 [emoji.muan.co](http://emoji.muan.co/)에 있습니다. + +GitHub에서 많이 사용하는 에모지 탑 5위는 이렇습니다. + +1. `:shipit:` +2. `:sparkles:` +3. `:-1:` +4. `:+1:` +5. `:clap:` + +### Images/GIFs + +댓글이나 README 등에 이미지와 GIF를 넣을 수 있습니다. + +``` +![Alt Text](http://www.sheawong.com/wp-content/uploads/2013/08/keephatin.gif) +``` + +저장소에 있는 이미지는 직접 불러서 사용할 수 있습니다. + +``` +![Alt Text](https://github.com/{user}/{repo}/raw/master/path/to/image.gif) +``` + +![Peter don't care](http://www.sheawong.com/wp-content/uploads/2013/08/keephatin.gif) + +모든 이미지는 GitHub에서 캐시합니다. 그래서 호스트가 죽어도 이미지는 여전히 남습니다. + +#### Embedding Images in GitHub Wiki + +이미지를 위키 페이지에 넣는 방법은 여럿 있습니다. 위에 보이는 일반 마크다운 문법도 있지만, 이미지에 높이와 넓이를 지정할 수 있는 문법도 있습니다. + +```markdown +[[ http://www.sheawong.com/wp-content/uploads/2013/08/keephatin.gif | height = 100px ]] +``` + +이렇게 보입니다. + +![Just a screenshot](http://i.imgur.com/J5bMf7S.png) + +### Quick Quoting + +스레드에 답글을 달 때 전에 누가 말한 어떤 내용을 인용하고 싶다면, 텍스트를 하이라이트하고 `r`을 누르세요. 이렇게 하면 내용을 텍스트 박스에 인용 포맷으로 복사해 줍니다. + +![Quick Quote](https://f.cloud.github.com/assets/296432/124483/b0fa6204-6ef0-11e2-83c3-256c37fa7abc.gif) + +[*빠른 인용에 대해 더 읽어 보세요.*](https://github.com/blog/1399-quick-quotes) + +### Pasting Clipboard Image to Comments + +_(크롬에서만 동작합니다)_ + +스크린샷을 찍고 클립보드에 복사한 경우 (mac: `cmd-ctrl-shift-4`), 간단히 이미지를 댓글 영역에 붙여넣을(`cmd-v / ctrl-v`) 수 있고 이는 자동으로 GitHub에 업로드 됩니다. + +![Pasting Clipboard Image to Comments](https://cloud.githubusercontent.com/assets/39191/5794265/39c9b65a-9f1b-11e4-9bc7-04e41f59ea5f.png) + +[*issue attachments에 대해 더 읽어 보세요.*](https://help.github.com/articles/issue-attachments) + +### Quick Licensing + +저장소를 만들 때, GitHub는 만들어진 저작권을 추가할 수 있는 옵션을 제공합니다. + +![License](http://i.imgur.com/Chqj4Fg.png) + +이미 있는 저장소에도 웹 인터페이스에서 새 파일을 만들어 추가할 수 있습니다. +이름에 `LICENSE`를 입력하면 옵션으로 템플릿을 사용할 수 있습니다. + +![License](http://i.imgur.com/fTjQict.png) + +`.gitignore`에도 똑같이 적용됩니다. + +[*오픈 소스 저작권에 대해 더 읽어 보세요.*](https://help.github.com/articles/open-source-licensing) + +### Task Lists + +이슈와 풀 리퀘스트에서 아래의 문법으로 체크박스를 넣을 수 있습니다.(스페이스에 주의하세요.) + +``` +- [ ] Be awesome +- [ ] Prepare dinner + - [ ] Research recipe + - [ ] Buy ingredients + - [ ] Cook recipe +- [ ] Sleep +``` + +![Task List](http://i.imgur.com/jJBXhsY.png) + +체크박스를 클릭하면, 순수 마크다운에서 갱신이 이루어집니다. + +``` +- [x] Be awesome +- [ ] Prepare dinner + - [x] Research recipe + - [x] Buy ingredients + - [ ] Cook recipe +- [ ] Sleep +``` + +[*할 일 목록에 대해 더 읽어 보세요.*](https://help.github.com/articles/writing-on-github#task-lists) + +#### Task Lists in Markdown Documents + +이제 마크다운 문서에서 **읽기 전용** 체크리스트를 넣을 수 있습니다. + +``` +- [ ] Mercury +- [x] Venus +- [x] Earth + - [x] Moon +- [x] Mars + - [ ] Deimos + - [ ] Phobos +``` + +- [ ] Mercury +- [x] Venus +- [x] Earth + - [x] Moon +- [x] Mars + - [ ] Deimos + - [ ] Phobos + +[*마크다운 문서에서의 할 일 목록에 대해 더 읽어 보세요.*](https://github.com/blog/1825-task-lists-in-all-markdown-documents) + +### Relative Links + +상대 경로 링크는 마크다운 파일이 내부 콘텐츠로 링크될 때 추천합니다. + +```markdown +[Link to a header](#awesome-section) +[Link to a file](docs/readme) +``` + +절대 경로 링크는 URL이 변경 될 때마다 변경해야 합니다. (예를 들어 저장소 이름 변경, 아이디 변경, 프로젝트 포크) +상대 경로 링크를 사용하면 문서를 쉽게 스스로 독립적으로 만들 수 있습니다. + +[*상대 경로 링크에 대해 더 읽어 보세요.*](https://help.github.com/articles/relative-links-in-readmes) + +### Metadata and Plugin Support for GitHub Pages + +지킬로 작성된 페이지와 포스트에서, 저장소 정보는 예를 들어 `{{ site.github.project_title }}`처럼 `site.github` 이름 공간으로 사용하고 표시할 수 있습니다. + +Jemoji와 jekyll-mentions 플러그인은 GitHub.com에서처럼 [emoji](#emojis)와 [@mentions](https://github.com/blog/821)를 지킬 포스트와 페이지에서 사용하게 합니다. + +[*저장소 메타 데이터와 GitHub 페이지의 플러그인 지원에 대해 더 읽어 보세요.*](https://github.com/blog/1797-repository-metadata-and-plugin-support-for-github-pages) + +### Viewing YAML Metadata in your Documents + +[Jekyll](http://jekyllrb.com/)이나 [GitHub Pages](http://pages.github.com/) 같은 많은 블로그에서 포스트의 처음에 YAML 포멧의 메타데이터를 필요로 합니다. GitHub는 이 메타 정보를 읽기 편하게 테이블로 표시해 줍니다. + +![YAML metadata](https://camo.githubusercontent.com/47245aa16728e242f74a9a324ce0d24c0b916075/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f36343035302f313232383236372f65303439643063362d323761302d313165332d396464382d6131636432323539393334342e706e67) + +[*문서에서 YAML 메타데이터 보기에 대해 더 읽어 보세요.*](https://github.com/blog/1647-viewing-yaml-metadata-in-your-documents) + +### Rendering Tabular Data + +GitHub는 `.csv`(comma-separated)와 `.tsv`(tab-separated) 파일에 대해 표(tabular) 데이터 보기를 지원합니다. + +![Tabular data](https://camo.githubusercontent.com/1b6dd0157ffb45d9939abf14233a0cb13b3b4dfe/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f3238323735392f3937363436322f33323038336463652d303638642d313165332d393262322d3566323863313061353035392e706e67) + +[*표 데이터 표시에 대해 더 읽어 보세요.*](https://github.com/blog/1601-see-your-csvs) + +### Rendering PDF + +GitHub에서 PDF를 렌더할 수 있습니다. + +![PDF](https://cloud.githubusercontent.com/assets/1000669/7492902/f8493160-f42e-11e4-8cea-1cb4f02757e7.png) + +[*PDF 렌더링에 대해 더 읽어 보세요.*](https://github.com/blog/1974-pdf-viewing) + +### Revert a Pull Request + +풀 리퀘스트가 머지된 후에, 아무 도움이 안되거나 머지가 잘못된 결정이었다는 걸 +눈치 챌 때가 있습니다. + +풀 리퀘스트 페이지의 커밋의 오른쪽에 있는 **Revert** 버튼을 클릭하면 이 풀 리퀘스트를 되돌리는 풀 리퀘스트를 만들어 되돌릴 수 있습니다. + +![Revert button](https://camo.githubusercontent.com/0d3350caf2bb1cba53123ffeafc00ca702b1b164/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f68656c702f70756c6c5f72657175657374732f7265766572742d70756c6c2d726571756573742d6c696e6b2e706e67) + +[*풀 리퀘스트 되돌리기에 대해 더 읽어 보세요.*](https://github.com/blog/1857-introducing-the-revert-button) + +### Diffs + +#### Rendered Prose Diffs + +GitHub에서 지원하는 산문(prose) 파일(예를 들어 Markdown)이 있는 커밋과 풀 리퀘스트에서는 *source*와 *rendered* 뷰 기능을 사용할 수 있습니다. + +![Source / Rendered view](https://github-images.s3.amazonaws.com/help/repository/rendered_prose_diff.png) + +"rendered" 버튼을 클릭하시면 렌더된 문서에서 변경을 확인하실 수 있습니다. 렌더된 산문(prose) 뷰는 문장을 추가, 삭제, 변경했을 때 유용합니다. + +![Rendered Prose Diffs](https://f.cloud.github.com/assets/17715/2003056/3997edb4-862b-11e3-90be-5e9586edecd7.png) + +[*렌더된 산문(prose) diff에 대해 더 읽어 보세요.*](https://github.com/blog/1784-rendered-prose-diffs) + +#### Diffable Maps + +지오데이터가 포함된 커밋이나 풀 리퀘스트를 볼 때마다 GitHub는 무엇이 변경 되었는지 시각적으로 보여줍니다. + +[![Diffable Maps](https://f.cloud.github.com/assets/282759/2090660/63f2e45a-8e97-11e3-9d8b-d4c8078b004e.gif)](https://github.com/benbalter/congressional-districts/commit/2233c76ca5bb059582d796f053775d8859198ec5) + +[*diff 가능한 맵에 대해 더 읽어 보세요.*](https://github.com/blog/1772-diffable-more-customizable-maps) + +#### Expanding Context in Diffs + +diff의 닫아둔 곳의 *펼침* 버튼을 클릭하면, 문맥을 더 볼 수 있습니다. 전체 파일을 표시할 때까지 계속 *펼칠* 수 있으며, GitHub에서 diff가 표시되는 장소라면 어디서든 사용가능합니다. + +![Expanding Context in Diffs](https://f.cloud.github.com/assets/22635/1610539/863c1f64-5584-11e3-82bf-151b406a272f.gif) + +[*diff에서의 문맥 확장에 대해 더 읽어 보세요.*](https://github.com/blog/1705-expanding-context-in-diffs) + +#### Diff or Patch of Pull Request + +URL 뒤에 `.diff`나 `.patch`를 붙이면 풀 리퀘스트의 diff를 얻을 수 있습니다. +예를 들면 + +``` +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` 확장자는 이런 내용의 평범한(plain) 텍스트를 줍니다. + +``` +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 + +(...) +``` + +#### Rendering and diffing images + +GitHub는 PNG, JPG, GIF, PSD를 포함한 여러 일반적인 이미지 형식을 표시할 수 있습니다. 추가적으로 이미지 형식의 diff를 표시하는 여러 방법을 제공합니다. + +[![Diffable PSD](https://cloud.githubusercontent.com/assets/2546/3165594/55f2798a-eb56-11e3-92e7-b79ad791a697.gif)](https://github.com/blog/1845-psd-viewing-diffing + +[*이미지 diff에 대해 더 읽어 보세요.*](https://help.github.com/articles/rendering-and-diffing-images) + +### Hub + +[Hub](https://github.com/github/hub)는 GitHub를 좀 더 쉽게 사용할 수 있도록 추가 기능 및 명령을 제공하는 커맨드 라인 Git 래퍼입니다. + +이렇게 할 수 있습니다. + +```bash +$ hub clone tiimgreen/toc +``` + +[*Hub가 제공하는 더 멋진 기능들을 확인해 보세요.*](https://github.com/github/hub#commands) + +### Contribution Guidelines + +GitHub는 프로젝트에 기여하는 사용자를 위한 세 가지 각기 다른 파일을 지원합니다. +이 파일은 저장소의 제일 위나 그 아래의 `.github` 디렉터리에 위치할 수 있습니다. + +### CONTRIBUTING File + +저장소의 제일 위나 그 아래의 `.github` 디렉터리에 `CONTRIBUTING`이나 `CONTRIBUTING.md` 파일을 넣어두면 기여자가 이슈를 만들거나 풀 리퀘스트를 만들 때 링크로 보여줍니다. + +![Contributing Guidelines](https://camo.githubusercontent.com/71995d6b0e620a9ef1ded00a04498241c69dd1bf/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f736b697463682f6973737565732d32303132303931332d3136323533392e6a7067) + +[*기여하기에 대해 더 읽어 보세요.*](https://github.com/blog/1184-contributing-guidelines) + +### ISSUE_TEMPLATE file + +프로젝트의 새로운 이슈에 사용될 템플릿을 지정할 수 있습니다. 사용자가 새 이슈를 생성하면 이슈 창이 이 파일의 내용으로 채워집니다. 저장소의 제일 위나 그 아래의 `.github` 디렉터리에 `ISSUE_TEMPLATE`이나 `ISSUE_TEMPLATE.md` 파일을 넣어 두세요. + +[*이슈 템플릿에 대해 더 읽어 보세요.*](https://github.com/blog/2111-issue-and-pull-request-templates) + +[이슈 템플릿 파일 생성기](https://www.talater.com/open-source-templates/) + +![GitHub Issue template](https://cloud.githubusercontent.com/assets/25792/13120859/733479fe-d564-11e5-8a1f-a03f95072f7a.png) + +### PULL_REQUEST_TEMPLATE file + +프로젝트의 새로운 풀 리퀘스트에 사용될 템플릿을 지정할 수 있습니다. 사용자가 새 풀 리퀘스트를 생성하면 텍스트 영역이 이 파일의 내용으로 채워집니다. 저장소의 제일 위나 `.github` 디렉터리에 `PULL_REQUEST_TEMPLATE`이나 `PULL_REQUEST_TEMPLATE.md` 파일을 넣어 두세요. + +[*풀 리퀘스트 템플릿에 대해 더 읽어 보세요.*](https://github.com/blog/2111-issue-and-pull-request-templates) + +[풀 리퀘스트 템플릿 파일 생성기](https://www.talater.com/open-source-templates/) + +### Octicons + +GitHub 아이콘(옥티콘)이 이제 오픈소스가 되었습니다. + +![Octicons](https://og.github.com/octicons/octicons@1200x630.png) + +[*GitHub 옥티콘에 대해 더 읽어보세요.*](https://octicons.github.com) + +### GitHub Resources + +| Title | Link | +| ----- | ---- | +| GitHub Explore | https://github.com/explore | +| GitHub Blog | https://github.com/blog | +| GitHub Help | https://help.github.com/ | +| GitHub Training | https://training.github.com/ | +| GitHub Developer | https://developer.github.com/ | +| GitHub Education (Free Micro Account and other stuff for students) | https://education.github.com/ | + +#### GitHub Talks + +| 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 + +### Remove All Deleted Files from the Working Tree + +`/bin/rm`을 사용해 대량의 파일을 지울 때, 하나씩 제거할 필요 없이 밑의 명령어를 사용해 작업 디렉터리와 인덱스에서 지울 수 있습니다. + +```bash +$ git rm $(git ls-files -d) +``` + +예를 들어 + +```bash +$ git status +On branch master +Changes not staged for commit: + deleted: a + deleted: c + +$ git rm $(git ls-files -d) +rm 'a' +rm 'c' + +$ git status +On branch master +Changes to be committed: + deleted: a + deleted: c +``` + +### Previous Branch + +Git에서 이전 브랜치로 돌아가는 건 이렇게 할 수 있습니다. + +```bash +$ 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는 이런 일을 합니다. + +- 줄 끝의 공백을 제거 +- 빈줄을 줄임 +- 파일 끝에 빈 줄을 추가 + +커맨드를 실행 할 때 파일을 반드시 이런식으로 넘겨줘야 합니다. + +```bash +$ git stripspace < README.md +``` + +[*Git `stripspace` 커맨드에 대해 더 읽어 보세요.*](http://git-scm.com/docs/git-stripspace) + +### SSH keys + +아래 주소를 방문하면 공개 ssh 키의 목록을 일반 텍스트 형식으로 볼 수 있습니다. + +``` +https://github.com/{user}.keys +``` + +예: [https://github.com/tiimgreen.keys](https://github.com/tiimgreen.keys) + +[*공개 ssh 키 접근에 대해 더 읽어 보세요.*](https://changelog.com/github-exposes-public-ssh-keys-for-its-users/) + +### Checking out Pull Requests + +풀 리퀘스트는 GitHub 저장소에서 사용하는 특별한 브랜치로 여러 방법으로 로컬로 +가져올 수 있습니다. + +빠르게 `diff`나 `merge`를 하기 위해 특정 풀 리퀘스트를 임시로 `FETCH_HEAD`로 +가져오려면 이렇게 합니다. + +```bash +$ git fetch origin refs/pull/[PR-Number]/head +``` + +모든 풀 리퀘스트 브랜치를 refspec에 의한 로컬 리모트 브랜치로 받을 수도 +있습니다. + +```bash +$ git fetch origin '+refs/pull/*/head:refs/remotes/origin/pr/*' +``` + +밑의 줄들을 저장소의 `.git/config`에 추가하여, 리모트의 풀 리퀘스트를 자동으로 +가져오게도 할 수 있습니다. + +``` +[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/* +``` + +포크 기반의 풀 리퀘스트 기여를 위해, 풀 리퀘스트의 리모트 브랜치를 `checkout`해 +로컬 브랜치를 만드는 것은 유용합니다. + +```bash +$ git checkout pr/42 pr-42 +``` + +여러 저장소를 다루어야 한다면, 전역 Git 설정에서 풀 리퀘스트를 받을(fetching) 수 있도록 설정 할 수도 있습니다. + +```bash +git config --global --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*" +``` + +이렇게 하면 모든 저장소에서 짧은 명령어를 사용 할 수 있습니다. + +```bash +git fetch origin +``` + +```bash +git checkout pr/42 +``` + +[*풀 리퀘스트를 로컬로 체크아웃 하는 방법에 대해 더 읽어 보세요.*](https://help.github.com/articles/checking-out-pull-requests-locally) + +### Empty Commits + +`--allow-empty`를 추가하시면 코드의 변경 없이 커밋을 넣을 수 있습니다. + +```bash +$ git commit -m "Big-ass commit" --allow-empty +``` + +있을 법한 사용법에는 이런 것들이 있습니다. + + - 한 뭉치의 일을 시작하기 전에 주석으로 사용 + - 코드와 관련없는 프로젝트 수정을 할때 주석으로 사용 + - 저장소를 사용하는 사람과의 대화 + - 저장소의 첫 커밋으로 사용 `git commit -m "Initial commit" --allow-empty` + +### Styled Git Status + +```bash +$ git status +``` + +![git status](http://i.imgur.com/qjPyvXb.png) + +`-sb`를 추가하면 + +```bash +$ git status -sb +``` + +이렇게 바뀝니다. + +![git status -sb](http://i.imgur.com/K0OY3nm.png) + +[*Git `status` 커맨드에 대해 더 읽어 보세요.*](http://git-scm.com/docs/git-status) + +### Styled Git Log + +이 명령을 실행하면 + +```bash +$ git log --all --graph --pretty=format:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative +``` + +이렇게 보입니다. + +![git log --all --graph --pretty=format:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative](http://i.imgur.com/58eOtkW.png) + +[Palesz](http://stackoverflow.com/users/88355/palesz) 님 고맙습니다. + +NOTE: 이 명령을 알리아스(단축 명령)로 넣을 수 있습니다. [여기](#aliases)의 소개를 보세요. + +[*Git `log` 커맨드에 대해 더 읽어 보세요.*](http://git-scm.com/docs/git-log) + +### Git Query + +Git 쿼리는 모든 이전 커밋 메시지에서 검색해 가장 최근의 쿼리에 맞는 커밋을 찾아줍니다. + +```bash +$ git show :/query +``` + +`query`에는 검색하고 싶은 말을 (대소문자를 구분해) 넣으세요. 주어진 검색어가 있는 가장 마지막 커밋을 표시하고 변경된 라인을 보여줍니다. + +```bash +$ git show :/typo +``` + +![git show :/query](http://i.imgur.com/icaGiNt.png) + +*나오려면 `q`를 누르세요.* + +### Git Grep + +Git Grep은 패턴과 일치하는 줄의 목록을 반환합니다. + +다음 명령을 실행해 보세요. + +```bash +$ git grep aliases +``` + +*aliases* 문자열이 포함된 모든 파일을 보여줍니다. + +![git grep aliases](http://i.imgur.com/DL2zpQ9.png) + +*나오려면 `q`를 누르세요.* + +여러 플래그를 이용해서 좀 더 복잡한 검색을 할 수 있습니다. 예를 들면, + + * `-e` 다음 파라미터는 패턴 (e.g. regex) + * `--and`, `--or`, `--not`으로 여러 패턴을 조합 + +이렇게 사용할 수 있습니다. +```bash + $ git grep -e pattern --and -e anotherpattern +``` + +[*Git `grep` 커맨드에 대해 더 읽어 보세요.*](http://git-scm.com/docs/git-grep) + +### Merged Branches + +```bash +$ git branch --merged +``` + +이 명령어는 현재 브랜치에 머지된 모든 브랜치의 목록을 보여줍니다. + +거꾸로 + +```bash +$ git branch --no-merged +``` + +이 명령어는 현재 브랜치에 머지되지 않은 브랜치의 목록을 보여줍니다. + +[*Git `branch` 커맨드에 대해 더 읽어 보세요.*](http://git-scm.com/docs/git-branch) + +### Fixup and Autosquash + +이전(HEAD의 한 개 이상 전의) 커밋에 잘못된 부분이 있다면, 예를 들어 `abcde`라면, +문제를 수정하고 밑의 커맨드를 입력해 고칠 수 있습니다. + +```bash +$ git commit --fixup=abcde +$ git rebase abcde^ --autosquash -i +``` +[*Git `commit` 커맨드에 대해 더 읽어 보세요.*](http://git-scm.com/docs/git-commit) +[*Git `rebase` 커맨드에 대해 더 읽어 보세요.*](http://git-scm.com/docs/git-rebase) + +### Web Server for Browsing Local Repositories + +Git `instaweb` 커맨드을 사용하면, 즉석에서 `gitweb` 안에서 작업중인 저장소를 둘러 볼 +수 있습니다. 이 커맨드는 `gitweb`과 로컬 저장소를 브라우징 하기 위한 웹 서버를 설정하는 스크립트입니다. + +```bash +$ git instaweb +``` + +Opens: + +![Git instaweb](http://i.imgur.com/Dxekmqc.png) + +[*Git `instaweb` 커맨드에 대해 더 읽어 보세요.*](http://git-scm.com/docs/git-instaweb) + +### Git Configurations + +`.gitconfig`는 모든 설정이 들어있는 파일입니다. + +#### Aliases + +알리아스는 커스텀 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 +``` + +커맨드 라인에서 다음 명령을 입력합니다. + +```bash +$ git config --global alias.new_alias git_function +``` + +예를 들면 + +```bash +$ git config --global alias.cm commit +``` + +여러 함수를 알리아스하려면 따옴표를 이용하세요. + +```bash +$ git config --global alias.ac 'add -A . && commit' +``` + +여기 몇 가지 유용한 알리아스가 있습니다. + +| Alias | Current 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 cleanup` | `git branch --merged \| grep -v '*' \| xargs git branch -d` | `git config --global alias.cleanup "!git branch --merged \| grep -v '*' \| xargs git branch -d"` | +| `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 --"` | + +*일부 알리아스는 [@mathiasbynens](https://github.com/mathiasbynens) 님의 dotfiles에서 가져왔습니다.(https://github.com/mathiasbynens/dotfiles/blob/master/.gitconfig)* + +#### Auto-correct + +현재 `git comit`를 치면 이런 결과를 보실 수 있습니다. + +```bash +$ git comit -m "Message" +# git: 'comit' is not a git command. See 'git --help'. + +# Did you mean this? +# commit +``` + +`comit`을 쳤을 때 `commit`을 부르려면, 자동 교정을 켜두세요. + +```bash +$ git config --global help.autocorrect 1 +``` + +이제 이런 결과를 보실 수 있습니다. + +```bash +$ 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... +``` + +#### Color + +Git 커맨드 라인에 색을 넣으려면 이렇게 하세요. + +```bash +$ git config --global color.ui 1 +``` + +[*Git `config` 커맨드에 대해 더 읽어 보세요.*](http://git-scm.com/docs/git-config) + +### Git Resources + +| 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/ | +| Git for Computer Scientists | http://eagain.net/articles/git-for-computer-scientists/ | +| Git Magic | http://www-cs-students.stanford.edu/~blynn/gitmagic/ | +| Git Visualization Playground | http://onlywei.github.io/explain-git-with-d3/#freeplay | +| Learn Git Branching | http://pcottle.github.io/learnGitBranching/ | +| A collection of useful .gitignore templates | https://github.com/github/gitignore | + +#### Git Books + +| 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 PluralSight | https://github.com/pluralsight/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 | + +#### Git Videos + +| Title | Link | +| ----- | ---- | +| Linus Torvalds on Git | https://www.youtube.com/watch?v=4XpnKHJAok8 | +| Introduction to Git with Scott Chacon | https://www.youtube.com/watch?v=ZDR433b0HJY | +| Git From the Bits Up | https://www.youtube.com/watch?v=MYP56QJpDr4 | +| Graphs, Hashes, and Compression, Oh My! | https://www.youtube.com/watch?v=ig5E8CcdM9g | +| GitHub Training & Guides | https://www.youtube.com/watch?list=PLg7s6cbtAD15G8lNyoaYDuKZSKyJrgwB-&v=FyfwLX4HAxM | + +#### Git Articles + +| Title | Link | +| ----- | ---- | +| GitHub Flow | http://scottchacon.com/2011/08/31/github-flow.html | diff --git a/README.md b/README.md index 172bbe7..6b8c9ae 100644 --- a/README.md +++ b/README.md @@ -1,63 +1,116 @@ -# GitHub Cheat Sheet -A collection of cool hidden and not so hidden features of Git and GitHub. This cheat sheet was inspired by [Zach Holman](https://github.com/holman)'s [Git and GitHub Secrets](http://www.confreaks.com/videos/1229-aloharuby2012-git-and-github-secrets) talk at Aloha Ruby Conference 2012 ([slides](https://speakerdeck.com/holman/git-and-github-secrets)) and his [More Git and GitHub Secrets](https://vimeo.com/72955426) talk at WDCNZ 2013 ([slides](https://speakerdeck.com/holman/more-git-and-github-secrets)). +# GitHub Cheat Sheet [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/sindresorhus/awesome) +A collection of cool hidden and not so hidden features of Git and GitHub. This cheat sheet was inspired by [Zach Holman](https://github.com/holman)'s [Git and GitHub Secrets](http://confreaks.tv/videos/aloharuby2012-git-and-github-secrets) talk at Aloha Ruby Conference 2012 ([slides](https://speakerdeck.com/holman/git-and-github-secrets)) and his [More Git and GitHub Secrets](https://vimeo.com/72955426) talk at WDCNZ 2013 ([slides](https://speakerdeck.com/holman/more-git-and-github-secrets)). + +*Shortlink: [`http://git.io/sheet`](http://git.io/sheet)* + +*Read this in other languages: [English](README.md), [한국어](README.ko.md), [日本語](README.ja.md), [简体中文](README.zh-cn.md), [正體中文](README.zh-tw.md).* + +GitHub Cheat Sheet is sponsored by [Snapshot: create interactive professional-quality product photos using AI](https://www.snapshotapp.io/) ## Table of Contents - - [GitHub](#github) - - [Ignore Whitespace](#ignore-whitespace) - - [Commit History by Author](#commit-history-by-author) - - [Cloning a Repository](#cloning-a-repository) - - [Comparing Branches](#comparing-branches) - - [Compare Branches across Forked Repositories](#compare-branches-across-forked-repositories) - - [Gists](#gists) - - [Git.io](#gitio) - - [Keyboard Shortcuts](#keyboard-shortcuts) - - [Line Highlighting in Repositories](#line-highlighting-in-repositories) - - [Closing Issues via Commit Messages](#closing-issues-via-commit-messages) - - [Cross-Link Issues](#cross-link-issues) - - [Syntax Highlighting in Markdown Files](#syntax-highlighting-in-markdown-files) - - [Emojis](#emojis) - - [Images/GIFs](#imagesgifs) - - [Embedding Images in GitHub Wiki](#embedding-images-in-github-wiki) - - [Quick Quoting](#quick-quoting) - - [Quick Licensing](#quick-licensing) - - [Task Lists](#task-lists) - - [Relative Links](#relative-links) - - [Metadata and Plugin Support for GitHub Pages](#metadata-and-plugin-support-for-github-pages) - - [Diffs](#diffs) - - [Rendered prose Diffs](#rendered-prose-diffs) - - [Diffable Maps](#diffable-maps) - - [Expanding Context in Diffs](#expanding-context-in-diffs) - - [Hub](#hub) - - [Decreasing Contributor Friction](#decreasing-contributor-friction) - - [Contributing Guidelines](#contributing-guidelines) - - [Git](#git) - - [Previous Branch](#previous-branch) - - [Checking out Pull Requests](#checking-out-pull-requests) - - [Empty Commits :trollface:](#empty-commits-trollface) - - [Styled Git Status](#styled-git-status) - - [Styled Git Log](#styled-git-log) - - [Git Query](#git-query) - - [Merged Branches](#merged-branches) - - [Web Server for Browsing Local Repositories](#web-server-for-browsing-local-repositories) - - [Git Configurations](#git-configurations) - - [Aliases](#aliases) - - [Auto-Correct](#auto-correct) - - [Color](#color) + - [GitHub](#github) + - [Ignore Whitespace](#ignore-whitespace) + - [Adjust Tab Space](#adjust-tab-space) + - [Commit History by Author](#commit-history-by-author) + - [Cloning a Repository](#cloning-a-repository) + - [Branch](#branch) + - [Compare all Branches to Another Branch](#compare-all-branches-to-another-branch) + - [Comparing Branches](#comparing-branches) + - [Compare Branches across Forked Repositories](#compare-branches-across-forked-repositories) + - [Gists](#gists) + - [Git.io](#gitio) + - [Keyboard Shortcuts](#keyboard-shortcuts) + - [Line Highlighting in Repositories](#line-highlighting-in-repositories) + - [Closing Issues via Commit Messages](#closing-issues-via-commit-messages) + - [Cross-Link Issues](#cross-link-issues) + - [Locking Conversations](#locking-conversations) + - [CI Status on Pull Requests](#ci-status-on-pull-requests) + - [Filters](#filters) + - [Syntax Highlighting in Markdown Files](#syntax-highlighting-in-markdown-files) + - [Emojis](#emojis) + - [Images/GIFs](#imagesgifs) + - [Embedding Images in GitHub Wiki](#embedding-images-in-github-wiki) + - [Quick Quoting](#quick-quoting) + - [Pasting Clipboard Image to Comments](#pasting-clipboard-image-to-comments) + - [Quick Licensing](#quick-licensing) + - [Task Lists](#task-lists) + - [Task Lists in Markdown Documents](#task-lists-in-markdown-documents) + - [Relative Links](#relative-links) + - [Metadata and Plugin Support for GitHub Pages](#metadata-and-plugin-support-for-github-pages) + - [Viewing YAML Metadata in your Documents](#viewing-yaml-metadata-in-your-documents) + - [Rendering Tabular Data](#rendering-tabular-data) + - [Rendering PDF](#rendering-pdf) + - [Revert a Pull Request](#revert-a-pull-request) + - [Diffs](#diffs) + - [Rendered Prose Diffs](#rendered-prose-diffs) + - [Diffable Maps](#diffable-maps) + - [Expanding Context in Diffs](#expanding-context-in-diffs) + - [Diff or Patch of Pull Request](#diff-or-patch-of-pull-request) + - [Rendering and diffing images](#rendering-and-diffing-images) + - [Hub](#hub) + - [Contribution Guidelines](#contribution-guidelines) + - [CONTRIBUTING file](#contributing-file) + - [ISSUE_TEMPLATE file](#issue_template-file) + - [PULL_REQUEST_TEMPLATE file](#pull_request_template-file) + - [Octicons](#octicons) + - [GitHub Student Developer Pack](#github-student-developer-pack) + - [GitHub Resources](#github-resources) + - [GitHub Talks](#github-talks) + - [SSH keys](#ssh-keys) + - [Profile Image](#profile-image) + - [Repository Templates](#repository-templates) + - [Git](#git) + - [Remove All Deleted Files from the Working Tree](#remove-all-deleted-files-from-the-working-tree) + - [Previous Branch](#previous-branch) + - [Stripspace](#stripspace) + - [Checking out Pull Requests](#checking-out-pull-requests) + - [Empty Commits](#empty-commits) + - [Styled Git Status](#styled-git-status) + - [Styled Git Log](#styled-git-log) + - [Git Query](#git-query) + - [Git Grep](#git-grep) + - [Merged Branches](#merged-branches) + - [Fixup and Autosquash](#fixup-and-autosquash) + - [Web Server for Browsing Local Repositories](#web-server-for-browsing-local-repositories) + - [Git Configurations](#git-configurations) + - [Aliases](#aliases) + - [Auto-Correct](#auto-correct) + - [Color](#color) + - [Git Resources](#git-resources) + - [Git Books](#git-books) + - [Git Videos](#git-videos) + - [Git Articles](#git-articles) + ## GitHub ### Ignore Whitespace -Adding `?w=1` to any diff URL will remove any changes only in whitespace, enabling you to see only that code that has changed. +Adding `?w=1` to any diff URL will remove any changes only in whitespace, enabling you to see only the code that has changed. + +![Diff without whitespace](https://camo.githubusercontent.com/797184940defadec00393e6559b835358a863eeb/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f626c6f672f323031312f736563726574732f776869746573706163652e706e67) [*Read more about GitHub secrets.*](https://github.com/blog/967-github-secrets) +### Adjust Tab Space +Adding `?ts=4` to a diff or file URL will display tab characters as 4 spaces wide instead of the default 8. The number after `ts` can be adjusted to suit your preference. This does not work on Gists, or raw file views, but a [Chrome extension](https://chrome.google.com/webstore/detail/tab-size-on-github/ofjbgncegkdemndciafljngjbdpfmbkn) can automate this. + +Here is a Go source file before adding `?ts=4`: + +![Before, tab space example](http://i.imgur.com/GIT1Fr0.png) + +...and this is after adding `?ts=4`: + +![After, tab space example](http://i.imgur.com/70FL4H9.png) + ### Commit History by Author -To view all commits on a repo by author add `?author=username` to the URL. +To view all commits on a repo by author add `?author={user}` to the URL. ``` https://github.com/rails/rails/commits/master?author=dhh ``` -[*Read more about the differences between commits views.*](https://help.github.com/articles/differences-between-commit-views) +![DHH commit history](http://i.imgur.com/S7AE29b.png) + +[*Read more about the differences between commits views.*](https://help.github.com/articles/differences-between-commit-views/) ### Cloning a Repository When cloning a repository the `.git` can be left off the end. @@ -68,20 +121,38 @@ $ git clone https://github.com/tiimgreen/github-cheat-sheet [*Read more about the Git `clone` command.*](http://git-scm.com/docs/git-clone) -### Comparing Branches +### Branch +#### Compare all Branches to Another Branch + +If you go to the repo's [Branches](https://github.com/tiimgreen/github-cheat-sheet/branches) page, next to the Commits button: + +``` +https://github.com/{user}/{repo}/branches +``` + +... you would see a list of all branches which are not merged into the main branch. + +From here you can access the compare page or delete a branch with a click of a button. + +![Compare branches not merged into master in rails/rails repo - https://github.com/rails/rails/branches](http://i.imgur.com/0FEe30z.png) + +#### Comparing Branches To use GitHub to compare branches, change the URL to look like this: ``` -https://github.com/user/repo/compare/{range} +https://github.com/{user}/{repo}/compare/{range} ``` -Where `{range} = master...4-1-stable` +where `{range} = master...4-1-stable` For example: + ``` https://github.com/rails/rails/compare/master...4-1-stable ``` +![Rails branch compare example](http://i.imgur.com/tIRCOsK.png) + `{range}` can be changed to things like: ``` @@ -89,38 +160,67 @@ https://github.com/rails/rails/compare/master@{1.day.ago}...master https://github.com/rails/rails/compare/master@{2014-10-04}...master ``` -...which allows you to see the difference on the master branch up a set time ago or a specified date. +*Here, dates are in the format `YYYY-MM-DD`* + +![Another compare example](http://i.imgur.com/5dtzESz.png) + +Branches can also be compared in `diff` and `patch` views: + +``` +https://github.com/rails/rails/compare/master...4-1-stable.diff +https://github.com/rails/rails/compare/master...4-1-stable.patch +``` -[*Read more about comparing commits across time.*](https://help.github.com/articles/comparing-commits-across-time) +[*Read more about comparing commits across time.*](https://help.github.com/articles/comparing-commits-across-time/) -### Compare Branches across Forked Repositories +#### Compare Branches across Forked Repositories To use GitHub to compare branches across forked repositories, change the URL to look like this: ``` -https://github.com/user/repo/compare/{foreign-user}:{branch}...{own-branch} +https://github.com/{user}/{repo}/compare/{foreign-user}:{branch}...{own-branch} ``` For example: ``` -https://github.com/rails/rails/compare/byroot:idempotent-counter-caches...master +https://github.com/rails/rails/compare/byroot:master...master ``` +![Forked branch compare](http://i.imgur.com/Q1W6qcB.png) + ### Gists -[Gists](https://gist.github.com/) are an easy way to work with small bits of code without creating a fully fledged repository. Add `.pibb` to the end of any Gist URL ([like this](https://gist.github.com/tiimgreen/10545817.pibb)) in order to get the *HTML only* version suitable for embedding in any other site. +[Gists](https://gist.github.com/) are an easy way to work with small bits of code without creating a fully fledged repository. + +![Gist](http://i.imgur.com/VkKI1LC.png?1) -Gists can be treated as a full repository so they can be cloned like any other: +Add `.pibb` to the end of any Gist URL ([like this](https://gist.github.com/tiimgreen/10545817.pibb)) in order to get the *HTML-only* version suitable for embedding in any other site. + +Gists can be treated as a repository so they can be cloned like any other: ```bash $ git clone https://gist.github.com/tiimgreen/10545817 ``` -![Gists](http://i.imgur.com/dULZXXo.png) +![Gists](http://i.imgur.com/BcFzabp.png) + +This means you also can modify and push updates to Gists: + +```bash +$ git commit +$ git push +Username for 'https://gist.github.com': +Password for 'https://tiimgreen@gist.github.com': +``` -[*Read more about creating gists.*](https://help.github.com/articles/creating-gists) +However, Gists do not support directories. All files need to be added to the repository root. +[*Read more about creating Gists.*](https://help.github.com/articles/creating-gists/) ### Git.io -[Git.io](http://git.io) is a simple URL shortener for GitHub. You can also use it via pure HTTP using Curl: +[Git.io](http://git.io) is a simple URL shortener for GitHub. + +![Git.io](http://i.imgur.com/6JUfbcG.png?1) + +You can also use it via pure HTTP using Curl: ```bash $ curl -i http://git.io -F "url=https://github.com/..." @@ -139,18 +239,20 @@ When on a repository page, keyboard shortcuts allow you to navigate easily. - Pressing `t` will bring up a file explorer. - Pressing `w` will bring up the branch selector. - - Pressing `s` will select the Command Bar. + - Pressing `s` will focus the search field for the current repository. Pressing ↓ to select the “All GitHub” option changes the field to search all of GitHub. - Pressing `l` will edit labels on existing Issues. - - Pressing `y` **when looking at a file** (e.g. `https://github.com/tiimgreen/github-cheat-sheet/blob/master/README.md`) will change your URL to one which, in effect, freezes the page you are looking at. If this code changes, you will still be able to see what you saw at that current time. + - Pressing `y` **when looking at a file** (e.g., `https://github.com/tiimgreen/github-cheat-sheet/blob/master/README.md`) will change your URL to one which, in effect, freezes the page you are looking at. If this code changes, you will still be able to see what you saw at that current time. + +To see all of the shortcuts for the current page press `?`: -To see all of the shortcuts for the current page press `?`. +![Keyboard shortcuts](http://i.imgur.com/y5ZfNEm.png) -[*Read more about using the Command Bar.*](https://help.github.com/articles/using-the-command-bar) +[Read more about search syntax you can use.](https://help.github.com/articles/search-syntax/) ### Line Highlighting in Repositories -Either adding `#L52` to the end of a code file URL or simply clicking the line number will highlight that line number. +Either adding, e.g., `#L52` to the end of a code file URL or simply clicking the line number will highlight that line number. -It also works with ranges, e.g. `#L53-L60`, to select ranges, hold `shift` and click two lines: +It also works with ranges, e.g., `#L53-L60`, to select ranges, hold `shift` and click two lines: ``` https://github.com/rails/rails/blob/master/activemodel/lib/active_model.rb#L53-L60 @@ -159,25 +261,75 @@ https://github.com/rails/rails/blob/master/activemodel/lib/active_model.rb#L53-L ![Line Highlighting](http://i.imgur.com/8AhjrCz.png) ### Closing Issues via Commit Messages -If a particular commit fixes an issue, any of the keywords `fix/fixes/fixed` or `close/closes/closed`, followed by the issue number, will close the issue once it is committed to the master branch. +If a particular commit fixes an issue, any of the keywords `fix/fixes/fixed`, `close/closes/closed` or `resolve/resolves/resolved`, followed by the issue number, will close the issue once it is committed to the repository's default branch. ```bash -$ git commit -m "Fix cock up, fixes #12" +$ git commit -m "Fix screwup, fixes #12" ``` This closes the issue and references the closing commit. -![Closing Repo](http://i.imgur.com/URXFprQ.png) +![Closing Repo](http://i.imgur.com/Uh1gZdx.png) -[*Read more about closing Issues via commit messages.*](https://help.github.com/articles/closing-issues-via-commit-messages) +[*Read more about closing Issues via commit messages.*](https://help.github.com/articles/closing-issues-via-commit-messages/) ### Cross-Link Issues -If you want to link to another issue in the same repository, simple type hash `#` then the issue number, it will be auto-linked. +If you want to link to another issue in the same repository, simply type hash `#` then the issue number, and it will be auto-linked. -To link to an issue in another repository, `user_name/repo_name#ISSUE_NUMBER` e.g. `tiimgreen/toc#12`. +To link to an issue in another repository, `{user}/{repo}#ISSUE_NUMBER`, e.g., `tiimgreen/toc#12`. ![Cross-Link Issues](https://camo.githubusercontent.com/447e39ab8d96b553cadc8d31799100190df230a8/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f626c6f672f323031312f736563726574732f7265666572656e6365732e706e67) +### Locking Conversations +Pull Requests and Issues can now be locked by owners or collaborators of the repo. + +![Lock conversation](https://cloud.githubusercontent.com/assets/2723/3221693/bf54dd44-f00d-11e3-8eb6-bb51e825bc2c.png) + +This means that users who are not collaborators on the project will no longer be able to comment. + +![Comments locked](https://cloud.githubusercontent.com/assets/2723/3221775/d6e513b0-f00e-11e3-9721-2131cb37c906.png) + +[*Read more about locking conversations.*](https://github.com/blog/1847-locking-conversations) + + +### CI Status on Pull Requests +If set up correctly, every time you receive a Pull Request, [Travis CI](https://travis-ci.org/) will build that Pull Request just like it would every time you make a new commit. Read more about how to [get started with Travis CI](http://docs.travis-ci.com/user/getting-started/). + +[![Travis CI status](https://cloud.githubusercontent.com/assets/1687642/2700187/3a88838c-c410-11e3-9a46-e65e2a0458cd.png)](https://github.com/octokit/octokit.rb/pull/452) + +[*Read more about the commit status API.*](https://github.com/blog/1227-commit-status-api) + +### Filters + +Both issues and pull requests allow filtering in the user interface. + +For the Rails repo: https://github.com/rails/rails/issues, the following filter is built by selecting the label "activerecord": + +`is:issue label:activerecord` + +But, you can also find all issues that are NOT labeled activerecord: + +`is:issue -label:activerecord` + +Additionally, this also works for pull requests: + +`is:pr -label:activerecord` + +Github has tabs for displaying open or closed issues and pull requests but you +can also see merged pull requests. Just put the following in the filter: + +`is:merged` + +[*Read more about searching issues.*](https://help.github.com/articles/searching-issues/) + +Finally, github now allows you to filter by the Status API's status. + +Pull requests with only successful statuses: + +`status:success` + +[*Read more about searching on the Status API.*](https://github.com/blog/2014-filter-pull-requests-by-status) + ### Syntax Highlighting in Markdown Files For example, to syntax highlight Ruby code in your Markdown files write: @@ -199,28 +351,21 @@ puts table.to_s GitHub uses [Linguist](https://github.com/github/linguist) to perform language detection and syntax highlighting. You can find out which keywords are valid by perusing the [languages YAML file](https://github.com/github/linguist/blob/master/lib/linguist/languages.yml). -[*Read more about GitHub Flavored Markdown.*](https://help.github.com/articles/github-flavored-markdown) +[*Read more about GitHub Flavored Markdown.*](https://help.github.com/articles/github-flavored-markdown/) ### Emojis -Emojis can added to on Pull Requests, Issues, commit messages, Markdown files, etc. using `:name_of_emoji:`: - -``` -:smile: -``` - -Would produce: +Emojis can be added to Pull Requests, Issues, commit messages, repository descriptions, etc. using `:name_of_emoji:`. -:smile: +The full list of supported Emojis on GitHub can be found at [emoji-cheat-sheet.com](http://www.emoji-cheat-sheet.com/) or [scotch-io/All-Github-Emoji-Icons](https://github.com/scotch-io/All-Github-Emoji-Icons). +A handy emoji search engine can be found at [emoji.muan.co](http://emoji.muan.co/). -The full list of supported Emojis on GitHub can be found [here](http://www.emoji-cheat-sheet.com/) or [here](https://github.com/scotch-io/All-Github-Emoji-Icons). +The top 5 used Emojis on GitHub are: -The top 5 used Ejmojis on GitHub are: - -1. :shipit: - `:shipit:` -2. :sparkles: - `:sparkles:` -3. :-1: - `:-1:` -4. :+1: - `:+1:` -5. :clap: - `:clap:` +1. `:shipit:` +2. `:sparkles:` +3. `:-1:` +4. `:+1:` +5. `:clap:` ### Images/GIFs Images and GIFs can be added to comments, READMEs etc.: @@ -229,6 +374,12 @@ Images and GIFs can be added to comments, READMEs etc.: ![Alt Text](http://www.sheawong.com/wp-content/uploads/2013/08/keephatin.gif) ``` +Raw images from the repo can be used by calling them directly.: + +``` +![Alt Text](https://github.com/{user}/{repo}/raw/master/path/to/image.gif) +``` + ![Peter don't care](http://www.sheawong.com/wp-content/uploads/2013/08/keephatin.gif) All images are cached on GitHub, so if your host goes down, the image will remain available. @@ -251,8 +402,18 @@ When on a comment thread and you want to quote something someone previously said [*Read more about quick quoting.*](https://github.com/blog/1399-quick-quotes) +### Pasting Clipboard Image to Comments + +_(Works on Chrome browsers only)_ + +After taking a screenshot and adding it to the clipboard (mac: `cmd-ctrl-shift-4`), you can simply paste (`cmd-v / ctrl-v`) the image into the comment section and it will be auto-uploaded to github. + +![Pasting Clipboard Image to Comments](https://cloud.githubusercontent.com/assets/39191/5794265/39c9b65a-9f1b-11e4-9bc7-04e41f59ea5f.png) + +[*Read more about issue attachments.*](https://help.github.com/articles/issue-attachments/) + ### Quick Licensing -When creating a repository GitHub gives you the options of adding in a pre-made license: +When creating a repository, GitHub gives you the option of adding in a pre-made license: ![License](http://i.imgur.com/Chqj4Fg.png) @@ -262,28 +423,57 @@ You can also add them to existing repositories by creating a new file through th Also works for `.gitignore`. -[*Read more about open source licensing.*](https://help.github.com/articles/open-source-licensing) +[*Read more about open source licensing.*](https://help.github.com/articles/open-source-licensing/) ### Task Lists In Issues and Pull requests check boxes can be added with the following syntax (notice the space): ``` - [ ] Be awesome -- [ ] Do stuff +- [ ] Prepare dinner + - [ ] Research recipe + - [ ] Buy ingredients + - [ ] Cook recipe - [ ] Sleep ``` -![Task List](http://i.imgur.com/k2qZi56.png) +![Task List](http://i.imgur.com/jJBXhsY.png) When they are clicked, they will be updated in the pure Markdown: ``` - [x] Be awesome -- [x] Do stuff +- [ ] Prepare dinner + - [x] Research recipe + - [x] Buy ingredients + - [ ] Cook recipe - [ ] Sleep ``` -[*Read more about task lists.*](https://github.com/blog/1375%0A-task-lists-in-gfm-issues-pulls-comments) +[*Read more about task lists.*](https://help.github.com/articles/writing-on-github/#task-lists) + +#### Task Lists in Markdown Documents +In full Markdown documents **read-only** checklists can now be added using the following syntax: + +``` +- [ ] Mercury +- [x] Venus +- [x] Earth + - [x] Moon +- [x] Mars + - [ ] Deimos + - [ ] Phobos +``` + +- [ ] Mercury +- [x] Venus +- [x] Earth + - [x] Moon +- [x] Mars + - [ ] Deimos + - [ ] Phobos + +[*Read more about task lists in markdown documents.*](https://github.com/blog/1825-task-lists-in-all-markdown-documents) ### Relative Links Relative links are recommended in your Markdown files when linking to internal content. @@ -293,20 +483,51 @@ Relative links are recommended in your Markdown files when linking to internal c [Link to a file](docs/readme) ``` -Absolute links have to be updated whenever the URL changes (e.g. repository renamed, username changed, project forked). Using relative links makes your documentation easily stand on its own. +Absolute links have to be updated whenever the URL changes (e.g., repository renamed, username changed, project forked). Using relative links makes your documentation easily stand on its own. -[*Read more about relative links.*](https://help.github.com/articles/relative-links-in-readmes) +[*Read more about relative links.*](https://help.github.com/articles/relative-links-in-readmes/) ### Metadata and Plugin Support for GitHub Pages Within Jekyll pages and posts, repository information is available within the `site.github` namespace, and can be displayed, for example, using `{{ site.github.project_title }}`. The Jemoji and jekyll-mentions plugins enable [emoji](#emojis) and [@mentions](https://github.com/blog/821) in your Jekyll posts and pages to work just like you'd expect when interacting with a repository on GitHub.com. -[*Read more about repository metadata and plugin support for GitHub Pages.*](Repository metadata and plugin support for GitHub Pages) +[*Read more about repository metadata and plugin support for GitHub Pages.*](https://github.com/blog/1797-repository-metadata-and-plugin-support-for-github-pages) + +### Viewing YAML Metadata in your Documents +Many blogging websites, like [Jekyll](http://jekyllrb.com/) with [GitHub Pages](https://pages.github.com), depend on some YAML-formatted metadata at the beginning of your post. GitHub will render this metadata as a horizontal table, for easier reading + +![YAML metadata](https://camo.githubusercontent.com/47245aa16728e242f74a9a324ce0d24c0b916075/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f36343035302f313232383236372f65303439643063362d323761302d313165332d396464382d6131636432323539393334342e706e67) + +[*Read more about viewing YAML metadata in your documents.*](https://github.com/blog/1647-viewing-yaml-metadata-in-your-documents) + +### Rendering Tabular Data +GitHub supports rendering tabular data in the form of `.csv` (comma-separated) and `.tsv` (tab-separated) files. + +![Tabular data](https://camo.githubusercontent.com/1b6dd0157ffb45d9939abf14233a0cb13b3b4dfe/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f3238323735392f3937363436322f33323038336463652d303638642d313165332d393262322d3566323863313061353035392e706e67) + +[*Read more about rendering tabular data.*](https://github.com/blog/1601-see-your-csvs) + +### Rendering PDF + +GitHub supports rendering PDF: + +![PDF](https://cloud.githubusercontent.com/assets/1000669/7492902/f8493160-f42e-11e4-8cea-1cb4f02757e7.png) + +[*Read more about rendering PDF.*](https://github.com/blog/1974-pdf-viewing) + +### Revert a Pull Request +After a pull request is merged, you may find it does not help anything or it was a bad decision to merge the pull request. + +You can revert it by clicking the **Revert** button on the right side of a commit in the pull request page to create a pull request with reverted changes to this specific pull request. + +![Revert button](https://camo.githubusercontent.com/0d3350caf2bb1cba53123ffeafc00ca702b1b164/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f68656c702f70756c6c5f72657175657374732f7265766572742d70756c6c2d726571756573742d6c696e6b2e706e67) + +[*Read more about reverting pull requests*](https://github.com/blog/1857-introducing-the-revert-button) ### Diffs #### Rendered Prose Diffs -Commits and pull requests including rendered documents supported by GitHub (e.g. Markdown) feature *source* and *rendered* views. +Commits and pull requests, including rendered documents supported by GitHub (e.g., Markdown), feature *source* and *rendered* views. ![Source / Rendered view](https://github-images.s3.amazonaws.com/help/repository/rendered_prose_diff.png) @@ -330,6 +551,44 @@ Using the *unfold* button in the gutter of a diff, you can reveal additional lin [*Read more about expanding context in diffs.*](https://github.com/blog/1705-expanding-context-in-diffs) +#### Diff or Patch of Pull Request +You can get the diff of a Pull Request by adding a `.diff` or `.patch` +extension to the end of the URL. For example: + +``` +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 +``` + +The `.diff` extension would give you this in plain text: + +``` +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 + +(...) +``` + +#### Rendering and diffing images +GitHub can display several common image formats, including PNG, JPG, GIF, and PSD. In addition, there are several ways to compare differences between versions of those image formats. + +[![Diffable PSD](https://cloud.githubusercontent.com/assets/2546/3165594/55f2798a-eb56-11e3-92e7-b79ad791a697.gif)](https://github.com/blog/1845-psd-viewing-diffing) + +[*Read more about rendering and diffing images.*](https://help.github.com/articles/rendering-and-diffing-images/) + ### Hub [Hub](https://github.com/github/hub) is a command line Git wrapper that gives you extra features and commands that make working with GitHub easier. @@ -339,29 +598,132 @@ This allows you to do things like: $ hub clone tiimgreen/toc ``` -...instead of: +[*Check out some more cool commands Hub has to offer.*](https://github.com/github/hub#commands) -```bash -$ git clone https://github.com/tiimgreen/toc.git +### Contribution Guidelines +GitHub supports adding 3 different files which help users contribute to your project. +These files can either be placed in the root of your repository or a `.github` directory under the root. + +#### CONTRIBUTING File +Adding a `CONTRIBUTING` or `CONTRIBUTING.md` file to either the root of your repository or a `.github` directory will add a link to your file when a contributor creates an Issue or opens a Pull Request. + +![Contributing Guidelines](https://camo.githubusercontent.com/71995d6b0e620a9ef1ded00a04498241c69dd1bf/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f736b697463682f6973737565732d32303132303931332d3136323533392e6a7067) + +[*Read more about contributing guidelines.*](https://github.com/blog/1184-contributing-guidelines) + +#### ISSUE_TEMPLATE file +You can define a template for all new issues opened in your project. The content of this file will pre-populate the new issue box when users create new issues. Add an `ISSUE_TEMPLATE` or `ISSUE_TEMPLATE.md` file to either the root of your repository or a `.github` directory. + +[*Read more about issue templates.*](https://github.com/blog/2111-issue-and-pull-request-templates) + +[Issue template file generator](https://www.talater.com/open-source-templates/) + +![GitHub Issue template](https://cloud.githubusercontent.com/assets/25792/13120859/733479fe-d564-11e5-8a1f-a03f95072f7a.png) + +#### PULL_REQUEST_TEMPLATE file +You can define a template for all new pull requests opened in your project. The content of this file will pre-populate the text area when users create pull requests. Add a `PULL_REQUEST_TEMPLATE` or `PULL_REQUEST_TEMPLATE.md` file to either the root of your repository or a `.github` directory. + +[*Read more about pull request templates.*](https://github.com/blog/2111-issue-and-pull-request-templates) + +[Pull request template file generator](https://www.talater.com/open-source-templates/) + +### Octicons +GitHubs icons (Octicons) have now been open sourced. + +![Octicons](https://og.github.com/octicons/octicons@1200x630.png) + +[*Read more about GitHub's Octicons*](https://octicons.github.com) + +### GitHub Student Developer Pack + +If you are a student you will be eligible for the GitHub Student Developer Pack. This gives you free credit, free trials and early access to software that will help you when developing. + +![GitHub Student Developer Pack](http://i.imgur.com/9ru3K43.png) + +[*Read more about GitHub's Student Developer Pack*](https://education.github.com/pack) + +### GitHub Resources +| Title | Link | +| ----- | ---- | +| GitHub Explore | https://github.com/explore | +| GitHub Blog | https://github.com/blog | +| GitHub Help | https://help.github.com/ | +| GitHub Training | https://training.github.com/ | +| GitHub Developer | https://developer.github.com/ | +| Github Education (Free Micro Account and other stuff for students) | https://education.github.com/ | +| GitHub Best Practices | [Best Practices List](https://www.datree.io/resources/github-best-practices) | + +#### GitHub Talks +| 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 | + +### SSH keys + +You can get a list of public ssh keys in plain text format by visiting: + +``` +https://github.com/{user}.keys ``` -[*Check out some more cool commands Hub has to offer.*](https://github.com/github/hub#commands) +e.g. [https://github.com/tiimgreen.keys](https://github.com/tiimgreen.keys) -### Decreasing Contributor Friction -If you want people to use and contribute to your project, you need to start by answering their most basic questions. What does the project do? How do I use it? How am I allowed to use it? How do I contribute? How do I get up and running in development? How do I make sure my new features didn't break old functionality? +[*Read more about accessing public ssh keys.*](https://changelog.com/github-exposes-public-ssh-keys-for-its-users/) -[Friction](https://github.com/rafalchmiel/friction) is a command line script that will check your project for common [answers to these questions](https://github.com/rafalchmiel/friction/wiki). This is some example output: +### Profile Image -[![Friction output](http://i.imgur.com/4EgpWo4.png)](https://github.com/rafalchmiel/friction) +You can get a user's profile image by visiting: -### Contributing Guidelines -Adding a `CONTRIBUTING` file to the root of your repository will add a link to your file when a contributor creates an Issue or opens a Pull Request. +``` +https://github.com/{user}.png +``` -![Contributing Guidelines](https://camo.githubusercontent.com/71995d6b0e620a9ef1ded00a04498241c69dd1bf/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f736b697463682f6973737565732d32303132303931332d3136323533392e6a7067) +e.g. [https://github.com/tiimgreen.png](https://github.com/tiimgreen.png) -[*Read more about contributing guidelines.*](https://github.com/blog/1184-contributing-guidelines) +### Repository Templates + +You can enable templating on your repository which allows anyone to copy the directory structure and files, allowing them to instantly use the files (e.g. for a tutorial or if writing boilerplate code). This can be enabled in the settings of your repository. + +![Convert](https://i.postimg.cc/hGCrVm9F/Template.gif) + +Changing to a template repository will give a new URL endpoint which can be shared and instantly allows users to use your repository as a template. Alternatively, they can go to your repository and click the 'Use as template' button. + +![Template](https://i.postimg.cc/L8PKCHx0/New-Template.gif) + +[*Read more about using repositories as templates*](https://github.blog/2019-06-06-generate-new-repositories-with-repository-templates/) ## Git +### Remove All Deleted Files from the Working Tree +When you delete a lot of files using `/bin/rm` you can use the following command to remove them from the working tree and from the index, eliminating the need to remove each one individually: + +```bash +$ git rm $(git ls-files -d) +``` + +For example: + +```bash +$ git status +On branch master +Changes not staged for commit: + deleted: a + deleted: c + +$ git rm $(git ls-files -d) +rm 'a' +rm 'c' + +$ git status +On branch master +Changes to be committed: + deleted: a + deleted: c +``` + ### Previous Branch To move to the previous branch in Git: @@ -378,33 +740,38 @@ $ git checkout - [*Read more about Git branching.*](http://git-scm.com/book/en/Git-Branching-Basic-Branching-and-Merging) -### Checking out Pull Requests -If you want to check out pull request locally, you can fetch it using that command: +### Stripspace -```bash -$ git fetch origin '+refs/pull/*/head:refs/pull/*' -``` +Git Stripspace: -then, checkout Pull Request (i.e. 42) using +- Strips trailing whitespace +- Collapses newlines +- Adds newline to end of file +A file must be passed when calling the command, e.g.: ```bash -$ git checkout refs/pull/42 +$ git stripspace < README.md ``` -Alternatively, you can fetch them as remote branches: +[*Read more about the Git `stripspace` command.*](http://git-scm.com/docs/git-stripspace) + +### Checking out Pull Requests + +Pull Requests are special branches on the GitHub repository which can be retrieved locally in several ways: + +Retrieve a specific Pull Request and store it temporarily in `FETCH_HEAD` for quickly `diff`-ing or `merge`-ing: ```bash -$ git fetch origin '+refs/pull/*/head:refs/remotes/origin/pr/*' +$ git fetch origin refs/pull/[PR-Number]/head ``` -and checkout as: +Acquire all Pull Request branches as local remote branches by refspec: ```bash -$ git checkout origin/pr/42 +$ git fetch origin '+refs/pull/*/head:refs/remotes/origin/pr/*' ``` -and even fetch them automatically, if you add corresponding lines in your .git/config: - +Or setup the remote to fetch Pull Requests automatically by adding these corresponding lines in your repository's `.git/config`: ``` [remote "origin"] @@ -419,9 +786,31 @@ and even fetch them automatically, if you add corresponding lines in your .git/c fetch = +refs/pull/*/head:refs/remotes/origin/pr/* ``` -[*Read more about checking out pull requests locally.*](https://help.github.com/articles/checking-out-pull-requests-locally) +For Fork-based Pull Request contributions, it's useful to `checkout` a remote branch representing the Pull Request and create a local branch from it: + +```bash +$ git checkout pr/42 pr-42 +``` + +Or should you work on more repositories, you can globally configure fetching pull requests in the global git config instead. + +```bash +git config --global --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*" +``` + +This way, you can use the following short commands in all your repositories: + +```bash +git fetch origin +``` + +```bash +git checkout pr/42 +``` -### Empty Commits :trollface: +[*Read more about checking out pull requests locally.*](https://help.github.com/articles/checking-out-pull-requests-locally/) + +### Empty Commits Commits can be pushed with no code changes by adding `--allow-empty`: ```bash @@ -433,6 +822,7 @@ Some use-cases for this (that make sense), include: - Annotating the start of a new bulk of work or a new feature. - Documenting when you make changes to the project that aren't code related. - Communicating with people using your repository. + - The first commit of a repository: `git commit -m "Initial commit" --allow-empty`. ### Styled Git Status Running: @@ -441,9 +831,9 @@ Running: $ git status ``` -Produces: +produces: -![git status](http://i.imgur.com/o3PEHAA.png) +![git status](http://i.imgur.com/qjPyvXb.png) By adding `-sb`: @@ -451,9 +841,9 @@ By adding `-sb`: $ git status -sb ``` -This is produced: +this is produced: -![git status -sb](http://i.imgur.com/xNI1bT0.png) +![git status -sb](http://i.imgur.com/K0OY3nm.png) [*Read more about the Git `status` command.*](http://git-scm.com/docs/git-status) @@ -461,12 +851,14 @@ This is produced: Running: ```bash -$ git log --all --graph --decorate --oneline --abbrev-commit +$ git log --all --graph --pretty=format:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative ``` -Produces: +produces: + +![git log --all --graph --pretty=format:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative](http://i.imgur.com/58eOtkW.png) -![git log --all --graph --decorate --oneline --abbrev-commit](http://i.imgur.com/RUPycwI.png) +Credit to [Palesz](http://stackoverflow.com/users/88355/palesz) *This can be aliased using the instructions found [here](https://github.com/tiimgreen/github-cheat-sheet#aliases).* @@ -479,7 +871,7 @@ A Git query allows you to search all your previous commit messages and find the $ git show :/query ``` -Where `query` is the term you want to search, this then finds the last one and gives details on the lines that were changed. +where `query` (case-sensitive) is the term you want to search, this then finds the last one and gives details on the lines that were changed. ```bash $ git show :/typo @@ -488,6 +880,33 @@ $ git show :/typo *Press `q` to quit.* + +### Git Grep + +Git Grep will return a list of lines matching a pattern. + +Running: +```bash +$ git grep aliases +``` +will show all the files containing the string *aliases*. + +![git grep aliases](http://i.imgur.com/DL2zpQ9.png) + +*Press `q` to quit.* + +You can also use multiple flags for more advanced search. For example: + + * `-e` The next parameter is the pattern (e.g., regex) + * `--and`, `--or` and `--not` Combine multiple patterns. + +Use it like this: +```bash + $ git grep -e pattern --and -e anotherpattern +``` + +[*Read more about the Git `grep` command.*](http://git-scm.com/docs/git-grep) + ### Merged Branches Running: @@ -495,7 +914,7 @@ Running: $ git branch --merged ``` -Will give you a list of all branches that have been merged into your current branch. +will give you a list of all branches that have been merged into your current branch. Conversely: @@ -503,10 +922,19 @@ Conversely: $ git branch --no-merged ``` -Will give you a list of branches that have not been merged into your current branch. +will give you a list of branches that have not been merged into your current branch. [*Read more about the Git `branch` command.*](http://git-scm.com/docs/git-branch) +### Fixup and Autosquash +If there is something wrong with a previous commit (can be one or more from HEAD), for example `abcde`, run the following command after you've amended the problem: +```bash +$ git commit --fixup=abcde +$ git rebase abcde^ --autosquash -i +``` +[*Read more about the Git `commit` command.*](http://git-scm.com/docs/git-commit) +[*Read more about the Git `rebase` command.*](http://git-scm.com/docs/git-rebase) + ### Web Server for Browsing Local Repositories Use the Git `instaweb` command to instantly browse your working repository in `gitweb`. This command is a simple script to set up `gitweb` and a web server for browsing the local repository. @@ -514,7 +942,7 @@ Use the Git `instaweb` command to instantly browse your working repository in `g $ git instaweb ``` -Opens: +opens: ![Git instaweb](http://i.imgur.com/Dxekmqc.png) @@ -542,19 +970,19 @@ To add an alias, either navigate to `~/.gitconfig` and fill it out in the follow ...or type in the command-line: ```bash -$ git config alias.new_alias git_function +$ git config --global alias.new_alias git_function ``` For example: ```bash -$ git config alias.cm commit +$ git config --global alias.cm commit ``` For an alias with multiple functions use quotes: ```bash -$ git config alias.ac 'add -A . && commit' +$ git config --global alias.ac 'add -A . && commit' ``` Some useful aliases include: @@ -567,10 +995,16 @@ Some useful aliases include: | `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 cleanup` | `git branch --merged \| grep -v '*' \| xargs git branch -d` | `git config --global alias.cleanup "!git branch --merged \| grep -v '*' \| xargs git branch -d"` | | `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 --"` | + +*Some Aliases are taken from [@mathiasbynens](https://github.com/mathiasbynens) dotfiles: https://github.com/mathiasbynens/dotfiles/blob/master/.gitconfig* #### Auto-Correct -If you type `git comit` you will get this: +Git gives suggestions for misspelled commands and if auto-correct is enabled the command can be fixed and executed automatically. Auto-correct is enabled by specifying an integer which is the delay in tenths of a second before git will run the corrected command. Zero is the default value where no correcting will take place, and a negative value will run the corrected command with no delay. + +For example, if you type `git comit` you will get this: ```bash $ git comit -m "Message" @@ -580,21 +1014,23 @@ $ git comit -m "Message" # commit ``` -To call `commit` when `comit` is typed, just enable auto-correct: +Auto-correct can be enabled like this (with a 1.5 second delay): ```bash -$ git config --global help.autocorrect 1 +$ git config --global help.autocorrect 15 ``` -So now you will get this: +So now the command `git comit` will be auto-corrected to `git commit` like this: ```bash $ 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... +# in 1.5 seconds automatically... ``` +The delay before git will rerun the command is so the user has time to abort. + #### Color To add more color to your Git output: @@ -603,3 +1039,47 @@ $ git config --global color.ui 1 ``` [*Read more about the Git `config` command.*](http://git-scm.com/docs/git-config) + +### Git Resources +| 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/ | +| Git God | https://github.com/gorosgobe/git-god | +| Git for Computer Scientists | http://eagain.net/articles/git-for-computer-scientists/ | +| Git Magic | http://www-cs-students.stanford.edu/~blynn/gitmagic/ | +| Git Visualization Playground | http://onlywei.github.io/explain-git-with-d3/#freeplay | +| Learn Git Branching | http://pcottle.github.io/learnGitBranching/ | +| A collection of useful .gitignore templates | https://github.com/github/gitignore | +| Unixorn's git-extra-commands collection of git scripts | https://github.com/unixorn/git-extra-commands | + +#### Git Books +| Title | Link | +| ----- | ---- | +| Pragmatic Version Control Using Git | https://pragprog.com/titles/tsgit/pragmatic-version-control-using-git | +| Pro Git | http://git-scm.com/book | +| Git Internals PluralSight | https://github.com/pluralsight/git-internals-pdf | +| Git in the Trenches | http://cbx33.github.io/gitt/ | +| Version Control with Git | http://www.amazon.com/Version-Control-Git-collaborative-development/dp/1449316387 | +| Pragmatic Guide to Git | https://pragprog.com/titles/pg_git/pragmatic-guide-to-git | +| Git: Version Control for Everyone | https://www.packtpub.com/application-development/git-version-control-everyone | + +#### Git Videos +| Title | Link | +| ----- | ---- | +| Linus Torvalds on Git | https://www.youtube.com/watch?v=4XpnKHJAok8 | +| Introduction to Git with Scott Chacon | https://www.youtube.com/watch?v=ZDR433b0HJY | +| Git From the Bits Up | https://www.youtube.com/watch?v=MYP56QJpDr4 | +| Graphs, Hashes, and Compression, Oh My! | https://www.youtube.com/watch?v=ig5E8CcdM9g | +| GitHub Training & Guides | https://www.youtube.com/watch?list=PLg7s6cbtAD15G8lNyoaYDuKZSKyJrgwB-&v=FyfwLX4HAxM | + +#### Git Articles +| Title | Link | +| ----- | ---- | +| GitHub Flow | http://scottchacon.com/2011/08/31/github-flow.html | +| Migrating to Git Large File Storate (Git LFS) | http://vooban.com/en/tips-articles-geek-stuff/migrating-to-git-lfs-for-developing-deep-learning-applications-with-large-files/ | diff --git a/README.zh-cn.md b/README.zh-cn.md new file mode 100644 index 0000000..9a28341 --- /dev/null +++ b/README.zh-cn.md @@ -0,0 +1,927 @@ +# GitHub秘籍 [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/sindresorhus/awesome) +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))。 + +*其他语言版本: [English](README.md), [한국어](README.ko.md), [日本語](README.ja.md), [简体中文](README.zh-cn.md), [正體中文](README.zh-tw.md).* + +# 目录 + - [GitHub](#github) + - [不比较空白字符](#不比较空白字符) + - [调整 Tab 字符所代表的空格数](#调整-tab-字符所代表的空格数) + - [查看某个用户的 Commit 历史](#查看某个用户的-commit-历史) + - [仓库克隆](#仓库克隆) + - [分支](#分支) + - [将某个分支与其他所有分支进行对比](#将某个分支与其他所有分支进行对比) + - [比较分支](#比较分支) + - [比较不同派生库的分支](#比较不同派生库的分支) + - [Gists](#gists) + - [Git.io](#gitio) + - [键盘快捷键](#键盘快捷键) + - [整行高亮](#整行高亮) + - [用 Commit 信息关闭 Issue](#用-commit-信息关闭-issue) + - [链接其他仓库的 Issue](#链接其他仓库的-issue) + - [锁定项目对话功能](#锁定项目对话功能) + - [设置 CI 对每条 Pull Request 都进行构建](#设置-ci-对每条-pull-request-都进行构建) + - [Markdown 文件语法高亮](#markdown-文件语法高亮) + - [表情符](#表情符) + - [图片 / GIF 动画](#图片--gif-动画) + - [在 GitHub Wiki 中引用图片](#在-github-wiki-中引用图片) + - [快速引用](#快速引用) + - [粘贴剪贴板中的图片到评论](#粘贴剪贴板中的图片到评论) + - [快速添加许可证文件](#快速添加许可证文件) + - [任务列表](#任务列表) + - [Markdown 文件中的任务列表](#markdown-文件中的任务列表) + - [相对链接](#相对链接) + - [GitHub Pages 的元数据与插件支持](#github-pages-的元数据与插件支持) + - [查看 YAML 格式的元数据](#查看-yaml-格式的元数据) + - [渲染表格数据](#渲染表格数据) + - [撤销 Pull Request](#撤销-pull-request) + - [Diffs](#diffs) + - [可渲染文档的Diffs](#可渲染文档的diffs) + - [可比较的地图数据](#可比较的地图数据) + - [在 Diff 中展开查看更多的上下文](#在-diff-中展开查看更多的上下文) + - [获取 Pull Request 的 diff 或 patch 文件](#获取-pull-request-的-diff-或-patch-文件) + - [显示图片以及比较图片](#显示图片以及比较图片) + - [Hub](#hub) + - [贡献者指南](#贡献者指南) + - [Octicons](#octicons) + - [GitHub 资源](#github-资源) + - [GitHub 相关演讲视频](#github-相关演讲视频) + - [Git](#git) + - [从工作区去除大量已删除文件](#从工作区去除大量已删除文件) + - [上一个分支](#上一个分支) + - [去除空白](#去除空白) + - [检出 Pull Requests](#检出-pull-requests) + - [没有任何改动的提交](#没有任何改动的提交) + - [美化 Git Status](#美化-git-status) + - [美化 Git Log](#美化-git-log) + - [Git 查询](#git-查询) + - [合并分支](#合并分支) + - [修复有问题的提交以及自动合并](#修复有问题的提交以及自动合并) + - [以网站方式查看本地仓库](#以网站方式查看本地仓库) + - [Git 配置](#git-配置) + - [Git 命令自定义别名](#git-命令自定义别名) + - [自动更正](#自动更正) + - [颜色输出](#颜色输出) + - [Git 资源](#git-资源) + - [Git 参考书籍](#git-参考书籍) + +## GitHub +### 不比较空白字符 + +在任意 diff 页面的 UR L后加上 `?w=1`,可以去掉那些只是空白字符的改动,使你能更专注于代码改动。 + +![Diff without whitespace](https://camo.githubusercontent.com/797184940defadec00393e6559b835358a863eeb/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f626c6f672f323031312f736563726574732f776869746573706163652e706e67) + +[*详见 GitHub secrets.*](https://github.com/blog/967-github-secrets) + +### 调整 Tab 字符所代表的空格数 +在 diff 或文件的 URL 后面加上 `?ts=4` ,这样当显示 tab 字符的长度时就会是 4 个空格的长度,不再是默认的 8 个空格。 `ts` 后面的数字还可以根据你个人的偏好进行修改。这个技巧不适用于 Gists,或者以 Raw 格式查看文件, 但有浏览器扩展插件可以帮你自动调整: [Chrome 扩展](https://chrome.google.com/webstore/detail/github-tab-size/ofjbgncegkdemndciafljngjbdpfmbkn)。 + +下面以一个 Go 语言源文件为例,看看在 URL 里添加 `?ts=4` 参数的效果。添加前: + +![Before, tab space example](http://i.imgur.com/GIT1Fr0.png) + +... 添加后的样子: + +![After, tab space example](http://i.imgur.com/70FL4H9.png) + +### 查看用户的全部 Commit 历史 +在 Commits 页面 URL 后加上 `?author={user}` 查看用户全部的提交。 + +``` +https://github.com/rails/rails/commits/master?author=dhh +``` + +![DHH commit history](http://i.imgur.com/S7AE29b.png) + +[*深入了解提交视图之间的区别*](https://help.github.com/articles/differences-between-commit-views) + +### 仓库克隆 +当克隆仓库时可以不要那个`.git`后缀。 + +```bash +$ git clone https://github.com/tiimgreen/github-cheat-sheet +``` + +[*更多对 Git `clone` 命令的介绍.*](http://git-scm.com/docs/git-clone) + +### 分支 +#### 将某个分支与其他所有分支进行对比 + +当你查看某个仓库的分支(Branches)页面(紧挨着 Commits 链接)时 + +``` +https://github.com/{user}/{repo}/branches +``` +你会看到一个包含所有未合并的分支的列表。 + +在这里你可以访问分支比较页面或删除某个分支。 + +![Compare branches not merged into master in rails/rails repo - https://github.com/rails/rails/branches](http://i.imgur.com/0FEe30z.png) + +#### 比较分支 + +如果要在 GitHub 上直接比较两个分支,可以使用如下形式的 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](http://i.imgur.com/tIRCOsK.png) + +`{range}` 参数还可以使用下面的形式: + +``` +https://github.com/rails/rails/compare/master@{1.day.ago}...master +https://github.com/rails/rails/compare/master@{2014-10-04}...master +``` + +*日期格式 `YYYY-MM-DD`* + +![Another compare example](http://i.imgur.com/5dtzESz.png) + +在 `diff` 和 `patch` 页面里也可以比较分支: + +``` +https://github.com/rails/rails/compare/master...4-1-stable.diff +https://github.com/rails/rails/compare/master...4-1-stable.patch +``` + +[*了解更多关于基于时间的 Commit 比较.*](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 +``` + +![Forked branch compare](http://i.imgur.com/Q1W6qcB.png) + +### Gists + +[Gists](https://gist.github.com/) 方便我们管理代码片段,不必使用功能齐全的仓库。 + +![Gist](http://i.imgur.com/VkKI1LC.png?1) + +Gist 的 URL 后加上 `.pibb`([像这样](https://gist.github.com/tiimgreen/10545817.pibb))可以得到便于嵌入到其他网站 的 HTML 代码。 + +Gists 可以像任何标准仓库一样被克隆。 + +```bash +$ git clone https://gist.github.com/tiimgreen/10545817 +``` + +![Gists](http://i.imgur.com/BcFzabp.png) + +这意味着你可以像 Github 仓库一样修改和更新 Gists : + +```bash +$ git commit +$ git push +Username for 'https://gist.github.com': +Password for 'https://tiimgreen@gist.github.com': +``` + +但是, Gists 不支持目录。所有文件都必须添加在仓库的根目录下。 +[*进一步了解如何创建 Gists.*](https://help.github.com/articles/creating-gists) + +### Git.io +[Git.io](http://git.io)是 Github 的短网址服务。 + +![Git.io](http://i.imgur.com/6JUfbcG.png?1) + +你可以通过 Curl 命令以普通 HTTP 协议使用它: + +```bash +$ 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` 键聚焦光标到当前仓库的搜索框。此时按退格键就会从搜索当前仓库切换到搜索整个 Github 网站。 + - 按 `l` 键编辑 Issue 列表页的标签。 + - **查看文件内容时**(如:`https://github.com/tiimgreen/github-cheat-sheet/blob/master/README.md`),按 `y` 键将会冻结这个页面,这样就算代码被修改了也不会影响你当前看到的。 + +按`?`查看当前页面支持的快捷键列表: + +![Keyboard shortcuts](http://i.imgur.com/y5ZfNEm.png) + +[*进一步了解可用的搜索语法.*](https://help.github.com/articles/search-syntax/) + +### 整行高亮 + +在代码文件地址 URL 后加上`#L52`或者单击行号 52 都会将第 52 行代码高亮显示。 + +多行高亮也可以,比如用`#L53-L60`选择范围,或者按住 `shift` 键,然后再点击选择的两行。 + +``` +https://github.com/rails/rails/blob/master/activemodel/lib/active_model.rb#L53-L60 +``` + +![整行高亮](http://i.imgur.com/8AhjrCz.png) + +### 用 Commit 信息关闭 Issue + +如果某个提交修复了一个 Issue,当提交到 master 分支时,提交信息里可以使用 `fix/fixes/fixed`, `close/closes/closed` 或者 `resolve/resolves/resolved` 等关键词,后面再跟上 Issue 号,这样就会关闭这个 Issue 。 + +```bash +$ git commit -m "Fix screwup, fixes #12" +``` + +这将会关闭 Issue #12,并且在 Issue 讨论列表里关联引用这次提交。 + +![Closing Repo](http://i.imgur.com/Uh1gZdx.png) + +[*进一步了解通过提交信息关闭 Issue.*](https://help.github.com/articles/closing-issues-via-commit-messages) + +### 链接其他仓库的 Issue +如果你想引用到同一个仓库中的一个 Issue,只需使用井号 `#` 加上 Issue 号,这样就会自动创建到此 Issue 的链接。 + +要链接到其他仓库的 Issue ,就使用`{user}/{repo}#ISSUE_NUMBER`的方式,例如`tiimgreen/toc#12`。 + +![Cross-Link Issues](https://camo.githubusercontent.com/447e39ab8d96b553cadc8d31799100190df230a8/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f626c6f672f323031312f736563726574732f7265666572656e6365732e706e67) + +### 锁定项目对话功能 +现在仓库的管理员和合作者可以将 Pull Requests 和 Issue 的评论功能关闭。 + +![Lock conversation](https://cloud.githubusercontent.com/assets/2723/3221693/bf54dd44-f00d-11e3-8eb6-bb51e825bc2c.png) + +这样,不是项目合作者的用户就不能在这个项目上使用评论功能。 + +![Comments locked](https://cloud.githubusercontent.com/assets/2723/3221775/d6e513b0-f00e-11e3-9721-2131cb37c906.png) + +[*进一步了解对话锁定功能.*](https://github.com/blog/1847-locking-conversations) + +### 设置 CI 对每条 Pull Request 都进行构建 +如果配置正确,[Travis CI](https://travis-ci.org/) 会为每个你收到的 Pull Request 执行构建,就像每次提交也会触发构建一样。想了解更多关于 Travis CI 的信息,请参考 [Travis CI入门](http://docs.travis-ci.com/user/getting-started/)。 + +[![Travis CI status](https://cloud.githubusercontent.com/assets/1687642/2700187/3a88838c-c410-11e3-9a46-e65e2a0458cd.png)](https://github.com/octokit/octokit.rb/pull/452) + +[*进一步了解提交状态 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 + ``` + +效果如下: + +```ruby +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:` +2. `:sparkles:` +3. `:-1:` +4. `:+1:` +5. `:clap:` + +### 图片 / GIF 动画 +注释和README等文件里也可以使用图片和 GIF 动画: + +``` +![Alt Text](http://www.sheawong.com/wp-content/uploads/2013/08/keephatin.gif) +``` + +仓库中的图片可以被直接引用: + +``` +![Alt Text](https://github.com/{user}/{repo}/raw/master/path/to/image.gif) +``` + +![Peter don't care](http://www.sheawong.com/wp-content/uploads/2013/08/keephatin.gif) + +所有图片都缓存在 Github,不用担心你的站点不能访问时就看不到图片了。 + +#### 在 GitHub Wiki 中引用图片 +有多种方法可以在 Wiki 页面里嵌入图片。既可以像上一条里那样使用标准的 Markdown 语法,也可以像下面这样指定图片的高度或宽度: + +```markdown +[[ http://www.sheawong.com/wp-content/uploads/2013/08/keephatin.gif | height = 100px ]] +``` +结果: + +![Just a screenshot](http://i.imgur.com/J5bMf7S.png) + +### 快速引用 +在主题评论中引用之前某个人所说的,只需选中文本,然后按 `r` 键,想要的就会以引用的形式复制到你的输入框里。 + +![Quick Quote](https://f.cloud.github.com/assets/296432/124483/b0fa6204-6ef0-11e2-83c3-256c37fa7abc.gif) + +[*进一步了解快速引用.*](https://github.com/blog/1399-quick-quotes) + +### 粘贴剪贴板中的图片到评论 + +_(仅适用于 Chrome 浏览器)_ + +当截屏图片复制到剪贴板后(mac 上用 `cmd-ctrl-shift-4`),你可以用(`cmd-v / ctrl-v`)把图片粘贴到评论框里,然后它就会自动上传到 Github。 + +![Pasting Clipboard Image to Comments](https://cloud.githubusercontent.com/assets/39191/5794265/39c9b65a-9f1b-11e4-9bc7-04e41f59ea5f.png) + +[*进一步了解在 issue 中使用附件*](https://help.github.com/articles/issue-attachments) + + +### 快速添加许可证文件 +创建一个仓库时,Github会为你提供一个预置的软件许可列表: + +![License](http://i.imgur.com/Chqj4Fg.png) + +对于已有的仓库,可以通过 web 界面创建文件来添加软件许可。输入`LICENSE`作为文件名后,同样可以从预置的列表中选择一个作为模板。 + +![License](http://i.imgur.com/fTjQict.png) + +这个技巧也适用于 `.gitignore` 文件。 + +[*进一步了解开源许可证*](https://help.github.com/articles/open-source-licensing) + +### 任务列表 +Issues 和 Pull requests 里可以添加复选框,语法如下(注意空白符): + +``` +- [ ] Be awesome +- [ ] Prepare dinner + - [ ] Research recipe + - [ ] Buy ingredients + - [ ] Cook recipe +- [ ] Sleep +``` + +![Task List](http://i.imgur.com/jJBXhsY.png) + +当项目被选中时,它对应的 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文件里链接到内部内容时推荐使用相对链接。 + +```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](#emojis)和[@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 metadata](https://camo.githubusercontent.com/47245aa16728e242f74a9a324ce0d24c0b916075/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f36343035302f313232383236372f65303439643063362d323761302d313165332d396464382d6131636432323539393334342e706e67) + +[*进一步了解 在文档里查看 YAML 元数据.*](https://github.com/blog/1647-viewing-yaml-metadata-in-your-documents) + +### 渲染表格数据 + +GitHub 支持将 `.csv` (逗号分隔)和 `.tsv` (制表符分隔)格式的文件渲染成表格数据。 + +![Tabular data](https://camo.githubusercontent.com/1b6dd0157ffb45d9939abf14233a0cb13b3b4dfe/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f3238323735392f3937363436322f33323038336463652d303638642d313165332d393262322d3566323863313061353035392e706e67) + +[*进一步了解渲染表格数据.*](https://github.com/blog/1601-see-your-csvs) + +### 撤销 Pull Request +合并一个 Pull Request 之后,你可能会反悔:要么是这次 Pull Request 没什么用处,要么是还不到合并的时候。 + +此时可以通过 Pull Request 中的 Revert 按钮来撤销一个已合并的 Pull Request 中的 commit。按下按钮后将自动生成一个进行逆操作的 Pull Request。 + +![Revert button](https://camo.githubusercontent.com/0d3350caf2bb1cba53123ffeafc00ca702b1b164/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f68656c702f70756c6c5f72657175657374732f7265766572742d70756c6c2d726571756573742d6c696e6b2e706e67) + +[*进一步了解“撤销”按钮](https://github.com/blog/1857-introducing-the-revert-button) + +### Diffs +#### 可渲染文档的Diffs + +Commit 和 Pull Request 里包含有 Github 支持的可渲染文档(比如 Markdown)会提供*source* 和 *rendered* 两个视图功能。 + +![Source / Rendered view](https://github-images.s3.amazonaws.com/help/repository/rendered_prose_diff.png) + +点击 "rendered" 按钮,看看改动在渲染后的显示效果。当你添加、删除或修改文本时,渲染纯文本视图非常方便。 + +![Rendered Prose Diffs](https://f.cloud.github.com/assets/17715/2003056/3997edb4-862b-11e3-90be-5e9586edecd7.png) + +[*进一步了解渲染纯文本视图Diffs.*](https://github.com/blog/1784-rendered-prose-diffs) + +#### 可比较的地图数据 +当你在GitHub上查看一个包含地理数据的 commit 或 pull request时,Github 将以可视化的方式对比版本之间的差异。 + +[![Diffable Maps](https://f.cloud.github.com/assets/282759/2090660/63f2e45a-8e97-11e3-9d8b-d4c8078b004e.gif)](https://github.com/benbalter/congressional-districts/commit/2233c76ca5bb059582d796f053775d8859198ec5) + +[*进一步了解可比较的地图数据.*](https://github.com/blog/1772-diffable-more-customizable-maps) + +#### 在 Diff 中展开查看更多的上下文 +你可以通过点击 diff 边栏里的 *unfold* 按钮来多显示几行上下文。你可以一直点击 *unfold* 按钮直到显示了文件的全部内容。这个功能在所有 GitHub 的 diff 功能中都可以使用。 + +![Expanding Context in Diffs](https://f.cloud.github.com/assets/22635/1610539/863c1f64-5584-11e3-82bf-151b406a272f.gif) + +[*进一步了解展开 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 在内的多种图片格式并提供了几种方式来比较这些格式的图片文件版本间的不同。 + +[![Diffable PSD](https://cloud.githubusercontent.com/assets/2546/3165594/55f2798a-eb56-11e3-92e7-b79ad791a697.gif)](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。 + +例如可以像下面这样进行克隆: + +```bash +$ hub clone tiimgreen/toc +``` + +[*查看更多 Hub 提供的超酷命令.*](https://github.com/github/hub#commands) + +### 贡献者指南 + +在仓库的根目录添加一个名为 `CONTRIBUTING` 的文件后,贡献者在新建 Issue 或 Pull Request 时会看到一个指向这个文件的链接。 + +![Contributing Guidelines](https://camo.githubusercontent.com/71995d6b0e620a9ef1ded00a04498241c69dd1bf/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f736b697463682f6973737565732d32303132303931332d3136323533392e6a7067) + +[*进一步了解贡献者指南.*](https://github.com/blog/1184-contributing-guidelines) + +### Octicons +GitHubs 图标库 (Octicons) 现已开源。 + +![Octicons](https://og.github.com/octicons/octicons@1200x630.png) + +[*进一步了解 GitHub 图标库*](https://octicons.github.com) + +### GitHub 资源 +| 内容 | 链接 | +| ----- | ---- | +| 探索 GitHub | https://github.com/explore | +| GitHub 博客 | https://github.com/blog | +| GitHub 帮助 | https://help.github.com/ | +| GitHub 培训 | http://training.github.com/ | +| GitHub 开发者 | https://developer.github.com/ | + +#### GitHub 相关演讲视频 +| 内容 | 链接 | +| ----- | ---- | +| 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 +### 从工作区去除大量已删除文件 +当用 `/bin/rm` 命令删除了大量文件之后,你可以用下面一条命令从工作区和索引中去除这些文件,以免一个一个的删除: + +```bash +$ git rm $(git ls-files -d) +``` + +例如: + +```bash +$ git status +On branch master +Changes not staged for commit: + deleted: a + deleted: c + +$ git rm $(git ls-files -d) +rm 'a' +rm 'c' + +$ git status +On branch master +Changes to be committed: + deleted: a + deleted: c +``` + +### 上一个分支 +快速检出上一个分支: + +```bash +$ 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) + +### 去除空白 + +Git Stripspace 命令可以: + +- 去掉行尾空白符 +- 多个空行压缩成一行 +- 必要时在文件末尾增加一个空行 + +使用此命令时必须传入一个文件,像这样: + +```bash +$ git stripspace < README.md +``` + +[*进一步了解 Git `stripspace` 命令.*](http://git-scm.com/docs/git-stripspace) + +### 检出 Pull Requests +对 Github 仓库来说,Pull Request 是种特殊分支, 可以通过以下多种方式取到本地: + +取出某个特定的 Pull Request 并临时作为本地的 `FETCH_HEAD` 中以便进行快速查看更改( diff )以及合并( merge ): + +```bash +$ git fetch origin refs/pull/[PR-Number]/head +``` + +通过 refspec 获取所有的 Pull Request 为本地分支: + +```bash +$ 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 的远端分支再由此分支建立一个本地分支: + +```bash +$ git checkout pr/42 pr-42 +``` + +操作多个仓库的时候,可以在 Git 中设置获取 Pull Request 的全局选项。 + +```bash +git config --global --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*" +``` + +此时可以在任意仓库中使用以下命令: + +```bash + git fetch origin +``` + +```bash +git checkout pr/42 +``` + + +[*进一步了解如何本地检出 pull request.*](https://help.github.com/articles/checking-out-pull-requests-locally) + +### 没有任何改动的提交 +可以使用`--allow-empty`选项强制创建一个没有任何改动的提交: + +```bash +$ git commit -m "Big-ass commit" --allow-empty +``` + +这样做在如下几种情况下是有意义的: + + - 标记新的工作或一个新功能的开始。 + - 记录对项目的跟代码无关的改动。 + - 跟使用你仓库的其他人交流。 + - 作为仓库的第一次提交,因为第一次提交后不能被 rebase: `git commit -m "init repo" --allow-empty`. + +### 美化 Git Status +在命令行输入如下命令: + +```bash +$ git status +``` + +可以看到: + +![git status](http://i.imgur.com/qjPyvXb.png) + +加上`-sb`选项: + +```bash +$ git status -sb +``` + +这会得到: + +![git status -sb](http://i.imgur.com/K0OY3nm.png) + +[*进一步了解 Git `status` 命令.*](http://git-scm.com/docs/git-status) + +### 美化 Git Log +输入如下命令: + +```bash +$ 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(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative](http://i.imgur.com/58eOtkW.png) + +这要归功于 [Palesz](http://stackoverflow.com/users/88355/palesz) 在 stackoverflow 的回答。 + +*这个命令可以被用作别名,详细做法见[这里](#git%E5%91%BD%E4%BB%A4%E8%87%AA%E5%AE%9A%E4%B9%89%E5%88%AB%E5%90%8D)。* + +[*进一步了解 Git `log` 命令.*](http://git-scm.com/docs/git-log) + +### Git 查询 +Git 查询运行你在之前的所有提交信息里进行搜索,找到其中和搜索条件相匹配的最近的一条。 + +```bash +$ git show :/query +``` + +这里 `query` (区别大小写)是你想要搜索的词语, 这条命令会找到包含这个词语的最后那个提交并显示变动详情。 + +```bash +$ git show :/typo +``` +![git show :/query](http://i.imgur.com/icaGiNt.png) + +* 按 `q` 键退出命令。* + +### 合并分支 +输入命令: + +```bash +$ git branch --merged +``` + +这会显示所有已经合并到你当前分支的分支列表。 + +相反地: + +```bash +$ git branch --no-merged +``` + +会显示所有还没有合并到你当前分支的分支列表。 + +[*进一步了解 Git `branch` 命令.*](http://git-scm.com/docs/git-branch) + +### 修复有问题的提交以及自动合并 +如果上一个或多个提交包含了错误,可以在你修复问题后使用下列命令处理(假设要修复的提交版本是`abcde`): +```bash +$ git commit --fixup=abcde +$ git rebase abcde^ --autosquash -i +``` +[*进一步了解 Git `commit` 命令.*](http://git-scm.com/docs/git-commit) +[*进一步了解 Git `rebase` 命令.*](http://git-scm.com/docs/git-rebase) + +### 以网站方式查看本地仓库 +使用 Git 的 `instaweb` 可以立即在 `gitweb` 中浏览你的工作仓库。这个命令是个简单的脚本,配置了 `gitweb` 和用来浏览本地仓库的Web服务器。*(译者注:默认需要lighttpd支持)* + +```bash +$ git instaweb +``` + +执行后打开: + +![Git instaweb](http://i.imgur.com/Dxekmqc.png) + +[*进一步了解 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 +``` + +...或者在命令行里键入: + +```bash +$ git config --global alias.new_alias git_function +``` + +例如: + +```bash +$ git config --global alias.cm commit +``` + +指向多个命令的别名可以用引号来定义: + +```bash +$ 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 cleanup` | `git branch --merged \| grep -v '*' \| xargs git branch -d` | `git config --global alias.cleanup "!git branch --merged \| grep -v '*' \| xargs git branch -d"` | +| `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` 你会看到如下输出: + +```bash +$ git comit -m "Message" +# git: 'comit' is not a git command. See 'git --help'. + +# Did you mean this? +# commit +``` + +为了在键入 `comit` 调用 `commit`命令,只需启用自动纠错功能: + +```bash +$ git config --global help.autocorrect 1 +``` + +现在你就会看到: + +```bash +$ 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 命令输出里加上颜色的话,可以用如下命令: + +```bash +$ 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/ | +| 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/ | +| Git for Computer Scientists | http://eagain.net/articles/git-for-computer-scientists/ | +| Git Magic | http://www-cs-students.stanford.edu/~blynn/gitmagic/ | +| GitHub Training Kit | http://training.github.com/kit | +| Git Visualization Playground | http://onlywei.github.io/explain-git-with-d3/#freeplay | + +#### 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 Pluralsight | https://github.com/pluralsight/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 | diff --git a/README.zh-tw.md b/README.zh-tw.md new file mode 100644 index 0000000..cfc43c2 --- /dev/null +++ b/README.zh-tw.md @@ -0,0 +1,971 @@ +# GitHub秘笈 [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/sindresorhus/awesome) +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))。 + +*其他語言版本: [English](README.md), [한국어](README.ko.md), [日本語](README.ja.md), [簡體中文](README.zh-cn.md), [正體中文](README.zh-tw.md).* + +# 目錄 + - [GitHub](#github) + - [不比較空白字串](#不比較空白字串) + - [調整 Tab 字串所代表的空格數](#調整-tab-字串所代表的空格數) + - [查看某個使用者的 Commit 歷史](#查看某個使用者的-commit-歷史) + - [倉庫複製](#倉庫複製) + - [分支](#分支) + - [將某個分支與其他所有分支進行比對](#將某個分支與其他所有分支進行比對) + - [比較分支](#比較分支) + - [比較不同派生庫的分支](#比較不同派生庫的分支) + - [Gists](#gists) + - [Git.io](#gitio) + - [鍵盤快捷鍵](#鍵盤快捷鍵) + - [整行高亮](#整行高亮) + - [用 Commit 訊息關閉 Issue](#用-commit-訊息關閉-issue) + - [連接其他倉庫的 Issue](#連接其他倉庫的-issue) + - [鎖定項目對話功能](#鎖定項目對話功能) + - [設置 CI 對每條 Pull Request 都進行構建](#設置-ci-對每條-pull-request-都進行構建) + - [Markdown 文件語法高亮](#markdown-文件語法高亮) + - [表情符號](#表情符號) + - [圖片 / GIF 動畫](#圖片--gif-動畫) + - [在 GitHub Wiki 中引用圖片](#在-github-wiki-中引用圖片) + - [快速引用](#快速引用) + - [複製貼上剪貼板中的圖片到評論](#複製貼上剪貼板中的圖片到評論) + - [快速添加許可證文件](#快速添加許可證文件) + - [任務列表](#任務列表) + - [Markdown 文件中的任務列表](#markdown-文件中的任務列表) + - [相對連接](#相對連接) + - [GitHub Pages 的中繼資料與插件支持](#github-pages-的中繼資料與插件支持) + - [查看 YAML 格式的中繼資料](#查看-yaml-格式的中繼資料) + - [渲染表格數據](#渲染表格數據) + - [撤銷 Pull Request](#撤銷-pull-request) + - [Diffs](#diffs) + - [可渲染文件的Diffs](#可渲染文件的diffs) + - [可比較的地圖數據](#可比較的地圖數據) + - [在 Diff 中展開查看更多的上下文](#在-diff-中展開查看更多的上下文) + - [獲取 Pull Request 的 diff 或 patch 文件](#獲取-pull-request-的-diff-或-patch-文件) + - [顯示圖片以及比較圖片](#顯示圖片以及比較圖片) + - [Hub](#hub) + - [貢獻者指南](#貢獻者指南) + - [Octicons](#octicons) + - [GitHub 資源](#github-資源) + - [GitHub 相關演講視頻](#github-相關演講視頻) + - [Git](#git) + - [從工作區去除大量已刪除文件](#從工作區去除大量已刪除文件) + - [上一個分支](#上一個分支) + - [去除空白](#去除空白) + - [SSH 金鑰](#ssh-金鑰) + - [檢出 Pull Requests](#檢出-pull-requests) + - [沒有任何改動的提交](#沒有任何改動的提交) + - [美化 Git Status](#美化-git-status) + - [美化 Git Log](#美化-git-log) + - [Git 查詢](#git-查詢) + - [Git Grep](#git-grep) + - [合併分支](#合併分支) + - [修復有問題的提交以及自動合併](#修復有問題的提交以及自動合併) + - [以網站方式查看本地倉庫](#以網站方式查看本地倉庫) + - [Git 設置](#git-設置) + - [Git 命令自定義別名](#git-命令自定義別名) + - [自動更正](#自動更正) + - [顏色輸出](#顏色輸出) + - [Git 資源](#git-資源) + - [Git 參考書籍](#git-參考書籍) + +## GitHub +### 不比較空白字串 + +在任意 diff 頁面的 UR L後加上 `?w=1`,可以去掉那些隻是空白字串的改動,使你能更專註於代碼改動。 + +![Diff without whitespace](https://camo.githubusercontent.com/797184940defadec00393e6559b835358a863eeb/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f626c6f672f323031312f736563726574732f776869746573706163652e706e67) + +[*詳見 GitHub secrets.*](https://github.com/blog/967-github-secrets) + +### 調整 Tab 字串所代表的空格數 +在 diff 或文件的 URL 後面加上 `?ts=4` ,這樣當顯示 tab 字串的長度時就會是 4 個空格的長度,不再是默認的 8 個空格。 `ts` 後面的數字還可以根據你個人的偏好進行修改。這個技巧不適用於 Gists,或者以 Raw 格式查看文件, 但有瀏覽器擴展插件可以幫你自動調整: [Chrome 擴展](https://chrome.google.com/webstore/detail/github-tab-size/ofjbgncegkdemndciafljngjbdpfmbkn)。 + +下面以一個 Go 語言原始碼為例,看看在 URL 裡添加 `?ts=4` 參數的效果。添加前: + +![Before, tab space example](http://i.imgur.com/GIT1Fr0.png) + +... 添加後的樣子: + +![After, tab space example](http://i.imgur.com/70FL4H9.png) + +### 查看使用者的全部 Commit 歷史 +在 Commits 頁面 URL 後加上 `?author={user}` 查看使用者全部的提交。 + +``` +https://github.com/rails/rails/commits/master?author=dhh +``` + +![DHH commit history](http://i.imgur.com/S7AE29b.png) + +[*深入了解提交視圖之間的區別*](https://help.github.com/articles/differences-between-commit-views) + +### 倉庫複製 +當複製倉庫時可以不要那個`.git`後綴。 + +```bash +$ git clone https://github.com/tiimgreen/github-cheat-sheet +``` + +[*更多對 Git `clone` 命令的介紹.*](http://git-scm.com/docs/git-clone) + +### 分支 +#### 將某個分支與其他所有分支進行比對 + +當你查看某個倉庫的分支(Branches)頁面(緊挨著 Commits 連接)時 + +``` +https://github.com/{user}/{repo}/branches +``` +你會看到一個包含所有未合併的分支的列表。 + +在這裡你可以訪問分支比較頁面或刪除某個分支。 + +![Compare branches not merged into master in rails/rails repo - https://github.com/rails/rails/branches](http://i.imgur.com/0FEe30z.png) + +#### 比較分支 + +如果要在 GitHub 上直接比較兩個分支,可以使用如下形式的 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](http://i.imgur.com/tIRCOsK.png) + +`{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](http://i.imgur.com/5dtzESz.png) + +在 `diff` 和 `patch` 頁面裡也可以比較分支: + +``` +https://github.com/rails/rails/compare/master...4-1-stable.diff +https://github.com/rails/rails/compare/master...4-1-stable.patch +``` + +[*了解更多關於基於時間的 Commit 比較.*](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 +``` + +![Forked branch compare](http://i.imgur.com/Q1W6qcB.png) + +### Gists + +[Gists](https://gist.github.com/) 方便我們管理代碼片段,不必使用功能齊全的倉庫。 + +![Gist](http://i.imgur.com/VkKI1LC.png?1) + +Gist 的 URL 後加上 `.pibb`([像這樣](https://gist.github.com/tiimgreen/10545817.pibb))可以得到便於嵌入到其他網站 的 HTML 代碼。 + +Gists 可以像任何標準倉庫一樣被複製。 + +```bash +$ git clone https://gist.github.com/tiimgreen/10545817 +``` + +![Gists](http://i.imgur.com/BcFzabp.png) + +這意味著你可以像 Github 倉庫一樣修改和更新 Gists : + +```bash +$ git commit +$ git push +Username for 'https://gist.github.com': +Password for 'https://tiimgreen@gist.github.com': +``` + +但是, Gists 不支持目錄。所有文件都必須添加在倉庫的根目錄下。 +[*進一步了解如何建立 Gists.*](https://help.github.com/articles/creating-gists) + +### Git.io +[Git.io](http://git.io)是 Github 的短網址服務。 + +![Git.io](http://i.imgur.com/6JUfbcG.png?1) + +你可以通過 Curl 命令以普通 HTTP 協議使用它: + +```bash +$ 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` 鍵聚焦游標到當前倉庫的搜索框。此時按刪除鍵就會從搜索當前倉庫切換到搜索整個 Github 網站。 + - 按 `l` 鍵編輯 Issue 列表頁的標籤。 + - **查看文件內容時**(如:`https://github.com/tiimgreen/github-cheat-sheet/blob/master/README.md`),按 `y` 鍵將會凍結這個頁面,這樣就算代碼被修改了也不會影響你當前看到的。 + +按`?`查看當前頁面支持的快捷鍵列表: + +![Keyboard shortcuts](http://i.imgur.com/y5ZfNEm.png) + +[*進一步了解可用的搜索語法.*](https://help.github.com/articles/search-syntax/) + +### 整行高亮 + +在代碼文件地址 URL 後加上`#L52`或者單擊行號 52 都會將第 52 行代碼高亮顯示。 + +多行高亮也可以,比如用`#L53-L60`選擇範圍,或者按住 `shift` 鍵,然後再點擊選擇的兩行。 + +``` +https://github.com/rails/rails/blob/master/activemodel/lib/active_model.rb#L53-L60 +``` + +![整行高亮](http://i.imgur.com/8AhjrCz.png) + +### 用 Commit 訊息關閉 Issue + +如果某個提交修復了一個 Issue,當提交到 master 分支時,提交訊息裡可以使用 `fix/fixes/fixed`, `close/closes/closed` 或者 `resolve/resolves/resolved` 等關鍵詞,後面再跟上 Issue 號,這樣就會關閉這個 Issue 。 + +```bash +$ git commit -m "Fix screwup, fixes #12" +``` + +這將會關閉 Issue #12,並且在 Issue 討論列表裡關聯引用這次提交。 + +![Closing Repo](http://i.imgur.com/Uh1gZdx.png) + +[*進一步了解通過提交訊息關閉 Issue.*](https://help.github.com/articles/closing-issues-via-commit-messages) + +### 連接其他倉庫的 Issue +如果你想引用到同一個倉庫中的一個 Issue,隻需使用井號 `#` 加上 Issue 號,這樣就會自動建立到此 Issue 的連接。 + +要連接到其他倉庫的 Issue ,就使用`{user}/{repo}#ISSUE_NUMBER`的方式,例如`tiimgreen/toc#12`。 + +![Cross-Link Issues](https://camo.githubusercontent.com/447e39ab8d96b553cadc8d31799100190df230a8/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f626c6f672f323031312f736563726574732f7265666572656e6365732e706e67) + +### 鎖定項目對話功能 +現在倉庫的管理員和合作者可以將 Pull Requests 和 Issue 的評論功能關閉。 + +![Lock conversation](https://cloud.githubusercontent.com/assets/2723/3221693/bf54dd44-f00d-11e3-8eb6-bb51e825bc2c.png) + +這樣,不是項目合作者的使用者就不能在這個項目上使用評論功能。 + +![Comments locked](https://cloud.githubusercontent.com/assets/2723/3221775/d6e513b0-f00e-11e3-9721-2131cb37c906.png) + +[*進一步了解對話鎖定功能.*](https://github.com/blog/1847-locking-conversations) + +### 設置 CI 對每條 Pull Request 都進行構建 +如果設置正確,[Travis CI](https://travis-ci.org/) 會為每個你收到的 Pull Request 執行構建,就像每次提交也會觸發構建一樣。想了解更多關於 Travis CI 的訊息,請參考 [Travis CI入門](http://docs.travis-ci.com/user/getting-started/)。 + +[![Travis CI status](https://cloud.githubusercontent.com/assets/1687642/2700187/3a88838c-c410-11e3-9a46-e65e2a0458cd.png)](https://github.com/octokit/octokit.rb/pull/452) + +[*進一步了解提交狀態 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 + ``` + +效果如下: + +```ruby +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:` +2. `:sparkles:` +3. `:-1:` +4. `:+1:` +5. `:clap:` + +### 圖片 / GIF 動畫 +註解和README等文件裡也可以使用圖片和 GIF 動畫: + +``` +![Alt Text](http://www.sheawong.com/wp-content/uploads/2013/08/keephatin.gif) +``` + +倉庫中的圖片可以被直接引用: + +``` +![Alt Text](https://github.com/{user}/{repo}/raw/master/path/to/image.gif) +``` + +![Peter don't care](http://www.sheawong.com/wp-content/uploads/2013/08/keephatin.gif) + +所有圖片都緩存在 Github,不用擔心你的網站不能訪問時就看不到圖片了。 + +#### 在 GitHub Wiki 中引用圖片 +有多種方法可以在 Wiki 頁面裡嵌入圖片。既可以像上一條裡那樣使用標準的 Markdown 語法,也可以像下面這樣指定圖片的高度或寬度: + +```markdown +[[ http://www.sheawong.com/wp-content/uploads/2013/08/keephatin.gif | height = 100px ]] +``` +結果: + +![Just a screenshot](http://i.imgur.com/J5bMf7S.png) + +### 快速引用 +在主題評論中引用之前某個人所說的,只需選中文件,然後按 `r` 鍵,想要的就會以引用的形式複製到你的輸入框裡。 + +![Quick Quote](https://f.cloud.github.com/assets/296432/124483/b0fa6204-6ef0-11e2-83c3-256c37fa7abc.gif) + +[*進一步了解快速引用.*](https://github.com/blog/1399-quick-quotes) + +### 複製貼上剪貼板中的圖片到評論 + +_(僅適用於 Chrome 瀏覽器)_ + +當截屏圖片複製到剪貼板後(mac 上用 `cmd-ctrl-shift-4`),你可以用(`cmd-v / ctrl-v`)把圖片複製貼上到評論框裡,然後它就會自動上傳到 Github。 + +![Pasting Clipboard Image to Comments](https://cloud.githubusercontent.com/assets/39191/5794265/39c9b65a-9f1b-11e4-9bc7-04e41f59ea5f.png) + +[*進一步了解在 issue 中使用附件*](https://help.github.com/articles/issue-attachments) + + +### 快速添加許可證文件 +建立一個倉庫時,Github會為你提供一個預設的軟體授權條款: + +![License](http://i.imgur.com/Chqj4Fg.png) + +對於已有的倉庫,可以通過 web 界面建立文件來添加軟體授權條款。輸入`LICENSE`作為文件名後,同樣可以從預設的列表中選擇一個作為模板。 + +![License](http://i.imgur.com/fTjQict.png) + +這個技巧也適用於 `.gitignore` 文件。 + +[*進一步了解開源許可證*](https://help.github.com/articles/open-source-licensing) + +### 任務列表 +Issues 和 Pull requests 裡可以添加複選框,語法如下(註意空白符): + +``` +- [ ] Be awesome +- [ ] Prepare dinner + - [ ] Research recipe + - [ ] Buy ingredients + - [ ] Cook recipe +- [ ] Sleep +``` + +![Task List](http://i.imgur.com/jJBXhsY.png) + +當項目被選中時,它對應的 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文件裡連接到內部內容時推薦使用相對連接。 + +```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](#emojis)和[@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 metadata](https://camo.githubusercontent.com/47245aa16728e242f74a9a324ce0d24c0b916075/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f36343035302f313232383236372f65303439643063362d323761302d313165332d396464382d6131636432323539393334342e706e67) + +[*進一步了解 在文件裡查看 YAML 中繼資料.*](https://github.com/blog/1647-viewing-yaml-metadata-in-your-documents) + +### 渲染表格數據 + +GitHub 支持將 `.csv` (逗號分隔)和 `.tsv` (定位點分隔)格式的文件渲染成表格數據。 + +![Tabular data](https://camo.githubusercontent.com/1b6dd0157ffb45d9939abf14233a0cb13b3b4dfe/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f3238323735392f3937363436322f33323038336463652d303638642d313165332d393262322d3566323863313061353035392e706e67) + +[*進一步了解渲染表格數據.*](https://github.com/blog/1601-see-your-csvs) + +### 撤銷 Pull Request +合併一個 Pull Request 之後,你可能會反悔:要麼是這次 Pull Request 沒什麼用處,要麼是還不到合併的時候。 + +此時可以通過 Pull Request 中的 Revert 按鈕來撤銷一個已合併的 Pull Request 中的 commit。按下按鈕後將自動生成一個進行逆操作的 Pull Request。 + +![Revert button](https://camo.githubusercontent.com/0d3350caf2bb1cba53123ffeafc00ca702b1b164/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f68656c702f70756c6c5f72657175657374732f7265766572742d70756c6c2d726571756573742d6c696e6b2e706e67) + +[*進一步了解「撤銷」按鈕](https://github.com/blog/1857-introducing-the-revert-button) + +### Diffs +#### 可渲染文件的Diffs + +Commit 和 Pull Request 裡包含有 Github 支持的可渲染文件(比如 Markdown)會提供*source* 和 *rendered* 兩個視圖功能。 + +![Source / Rendered view](https://github-images.s3.amazonaws.com/help/repository/rendered_prose_diff.png) + +點擊 "rendered" 按鈕,看看改動在渲染後的顯示效果。當你添加、刪除或修改文件時,渲染純文件視圖非常方便。 + +![Rendered Prose Diffs](https://f.cloud.github.com/assets/17715/2003056/3997edb4-862b-11e3-90be-5e9586edecd7.png) + +[*進一步了解渲染純文件視圖Diffs.*](https://github.com/blog/1784-rendered-prose-diffs) + +#### 可比較的地圖數據 +當你在GitHub上查看一個包含地理數據的 commit 或 pull request時,Github 將以可視化的方式比對版本之間的差異。 + +[![Diffable Maps](https://f.cloud.github.com/assets/282759/2090660/63f2e45a-8e97-11e3-9d8b-d4c8078b004e.gif)](https://github.com/benbalter/congressional-districts/commit/2233c76ca5bb059582d796f053775d8859198ec5) + +[*進一步了解可比較的地圖數據.*](https://github.com/blog/1772-diffable-more-customizable-maps) + +#### 在 Diff 中展開查看更多的上下文 +你可以通過點擊 diff 邊欄裡的 *unfold* 按鈕來多顯示幾行上下文。你可以一直點擊 *unfold* 按鈕直到顯示了文件的全部內容。這個功能在所有 GitHub 的 diff 功能中都可以使用。 + +![Expanding Context in Diffs](https://f.cloud.github.com/assets/22635/1610539/863c1f64-5584-11e3-82bf-151b406a272f.gif) + +[*進一步了解展開 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 在內的多種圖片格式並提供了幾種方式來比較這些格式的圖片文件版本間的不同。 + +[![Diffable PSD](https://cloud.githubusercontent.com/assets/2546/3165594/55f2798a-eb56-11e3-92e7-b79ad791a697.gif)](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。 + +例如可以像下面這樣進行複製: + +```bash +$ hub clone tiimgreen/toc +``` + +[*查看更多 Hub 提供的超酷命令.*](https://github.com/github/hub#commands) + +### 貢獻者指南 + +在倉庫的根目錄添加一個名為 `CONTRIBUTING` 的文件後,貢獻者在新建 Issue 或 Pull Request 時會看到一個指向這個文件的連接。 + +![Contributing Guidelines](https://camo.githubusercontent.com/71995d6b0e620a9ef1ded00a04498241c69dd1bf/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f736b697463682f6973737565732d32303132303931332d3136323533392e6a7067) + +[*進一步了解貢獻者指南.*](https://github.com/blog/1184-contributing-guidelines) + +### Octicons +GitHubs 圖標庫 (Octicons) 現已開源。 + +![Octicons](https://og.github.com/octicons/octicons@1200x630.png) + +[*進一步了解 GitHub 圖標庫*](https://octicons.github.com) + +### GitHub 資源 +| 內容 | 連接 | +| ----- | ---- | +| 探索 GitHub | https://github.com/explore | +| GitHub 博客 | https://github.com/blog | +| GitHub 幫助 | https://help.github.com/ | +| GitHub 培訓 | http://training.github.com/ | +| GitHub 開發者 | https://developer.github.com/ | + +#### GitHub 相關演講視頻 +| 內容 | 連接 | +| ----- | ---- | +| 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 +### 從工作區去除大量已刪除文件 +當用 `/bin/rm` 命令刪除了大量文件之後,你可以用下面一條命令從工作區和索引中去除這些文件,以免一個一個的刪除: + +```bash +$ git rm $(git ls-files -d) +``` + +例如: + +```bash +$ git status +On branch master +Changes not staged for commit: + deleted: a + deleted: c + +$ git rm $(git ls-files -d) +rm 'a' +rm 'c' + +$ git status +On branch master +Changes to be committed: + deleted: a + deleted: c +``` + +### 上一個分支 +快速檢出上一個分支: + +```bash +$ 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) + +### 去除空白 + +Git Stripspace 命令可以: + +- 去掉行尾空白符 +- 多個空行壓縮成一行 +- 必要時在文件末尾增加一個空行 + +使用此命令時必須傳入一個文件,像這樣: + +```bash +$ git stripspace < README.md +``` + +[*進一步了解 Git `stripspace` 命令.*](http://git-scm.com/docs/git-stripspace) + +### SSH 金鑰 + +您可以藉由下面網址得到全部公開的 ssh 金鑰: + +``` +https://github.com/{user}.keys +``` + +範例:[https://github.com/tiimgreen.keys](https://github.com/tiimgreen.keys) + +[*更多詳細 SSH 金鑰資訊...*](https://changelog.com/github-exposes-public-ssh-keys-for-its-users/) + + +### 檢出 Pull Requests + +對 Github 倉庫來說,Pull Request 是種特殊分支, 可以通過以下多種方式取到本地: + +取出某個特定的 Pull Request 並臨時作為本地的 `FETCH_HEAD` 中以便進行快速查看更改( diff )以及合併( merge ): + +```bash +$ git fetch origin refs/pull/[PR-Number]/head +``` + +通過 refspec 獲取所有的 Pull Request 為本地分支: + +```bash +$ 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 的遠端分支再由此分支建立一個本地分支: + +```bash +$ git checkout pr/42 pr-42 +``` + +操作多個倉庫的時候,可以在 Git 中設置獲取 Pull Request 的全局選項。 + +```bash +git config --global --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*" +``` + +此時可以在任意倉庫中使用以下命令: + +```bash + git fetch origin +``` + +```bash +git checkout pr/42 +``` + + +[*進一步了解如何本地檢出 pull request.*](https://help.github.com/articles/checking-out-pull-requests-locally) + +### 沒有任何改動的提交 +可以使用`--allow-empty`選項強製建立一個沒有任何改動的提交: + +```bash +$ git commit -m "Big-ass commit" --allow-empty +``` + +這樣做在如下幾種情況下是有意義的: + + - 標記新的工作或一個新功能的開始。 + - 記錄對項目的跟代碼無關的改動。 + - 跟使用你倉庫的其他人交流。 + - 作為倉庫的第一次提交,因為第一次提交後不能被 rebase: `git commit -m "init repo" --allow-empty`. + +### 美化 Git Status +在命令行輸入如下命令: + +```bash +$ git status +``` + +可以看到: + +![git status](http://i.imgur.com/qjPyvXb.png) + +加上`-sb`選項: + +```bash +$ git status -sb +``` + +這會得到: + +![git status -sb](http://i.imgur.com/K0OY3nm.png) + +[*進一步了解 Git `status` 命令.*](http://git-scm.com/docs/git-status) + +### 美化 Git Log +輸入如下命令: + +```bash +$ 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(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative](http://i.imgur.com/58eOtkW.png) + +這要歸功於 [Palesz](http://stackoverflow.com/users/88355/palesz) 在 stackoverflow 的回答。 + +*這個命令可以被用作別名,詳細做法見[這裡](#git%E5%91%BD%E4%BB%A4%E8%87%AA%E5%AE%9A%E4%B9%89%E5%88%AB%E5%90%8D)。* + +[*進一步了解 Git `log` 命令.*](http://git-scm.com/docs/git-log) + +### Git 查詢 +Git 查詢運行你在之前的所有提交訊息裡進行搜索,找到其中和搜索條件相匹配的最近的一條。 + +```bash +$ git show :/query +``` + +這裡 `query` (區別大小寫)是你想要搜索的詞語, 這條命令會找到包含這個詞語的最後那個提交並顯示變動詳情。 + +```bash +$ git show :/typo +``` +![git show :/query](http://i.imgur.com/icaGiNt.png) + +* 按 `q` 鍵退出命令。* + +### Git Grep + +Git Grep 將顯示匹配字串的行號。 + +執行: + +```bash +$ git grep aliases +``` +會找出檔案內有出現 *aliases* 字串。 + +![git grep aliases](http://i.imgur.com/DL2zpQ9.png) + +* 按 `q` 結束搜尋* + +你也可以使用多重 flags 來達到進階搜尋。範例: + + * `-e` 下一個搜尋匹配條件 (e.g. regex) + * `--and`, `--or` and `--not` 合併多重匹配字串. + +使用如下: +```bash + $ git grep -e pattern --and -e anotherpattern +``` + +[*更多詳細 Git `grep` 指令介紹*](http://git-scm.com/docs/git-grep) + +### 合併分支 + +輸入命令: + +```bash +$ git branch --merged +``` + +這會顯示所有已經合併到你當前分支的分支列表。 + +相反地: + +```bash +$ git branch --no-merged +``` + +會顯示所有還沒有合併到你當前分支的分支列表。 + +[*進一步了解 Git `branch` 命令.*](http://git-scm.com/docs/git-branch) + +### 修復有問題的提交以及自動合併 +如果上一個或多個提交包含了錯誤,可以在你修復問題後使用下列命令處理(假設要修復的提交版本是`abcde`): +```bash +$ git commit --fixup=abcde +$ git rebase abcde^ --autosquash -i +``` +[*進一步了解 Git `commit` 命令.*](http://git-scm.com/docs/git-commit) +[*進一步了解 Git `rebase` 命令.*](http://git-scm.com/docs/git-rebase) + +### 以網站方式查看本地倉庫 +使用 Git 的 `instaweb` 可以立即在 `gitweb` 中瀏覽你的工作倉庫。這個命令是個簡單的腳本,設置了 `gitweb` 和用來瀏覽本地倉庫的Web服務器。*(譯者註:默認需要lighttpd支持)* + +```bash +$ git instaweb +``` + +執行後打開: + +![Git instaweb](http://i.imgur.com/Dxekmqc.png) + +[*進一步了解 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 +``` + +...或者在命令行裡鍵入: + +```bash +$ git config --global alias.new_alias git_function +``` + +例如: + +```bash +$ git config --global alias.cm commit +``` + +指向多個命令的別名可以用引號來定義: + +```bash +$ 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 cleanup` | `git branch --merged \| grep -v '*' \| xargs git branch -d` | `git config --global alias.cleanup "!git branch --merged \| grep -v '*' \| xargs git branch -d"` | +| `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` 你會看到如下輸出: + +```bash +$ git comit -m "Message" +# git: 'comit' is not a git command. See 'git --help'. + +# Did you mean this? +# commit +``` + +為了在鍵入 `comit` 調用 `commit`命令,隻需啟用自動糾錯功能: + +```bash +$ git config --global help.autocorrect 1 +``` + +現在你就會看到: + +```bash +$ 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 命令輸出裡加上顏色的話,可以用如下命令: + +```bash +$ 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/ | +| 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/ | +| Git for Computer Scientists | http://eagain.net/articles/git-for-computer-scientists/ | +| Git Magic | http://www-cs-students.stanford.edu/~blynn/gitmagic/ | +| GitHub Training Kit | http://training.github.com/kit | +| Git Visualization Playground | http://onlywei.github.io/explain-git-with-d3/#freeplay | + +#### 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 Pluralsight | https://github.com/pluralsight/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 |