[TOC]
# Forks and Clones - Fork和clone一個開源的計劃
## Forks
你已經在電腦上建立了一個項目并且 推送 push 到 Github 上,但有趣的不僅如此,更重要的是與人們在不同的專案中合作。
當你 **fork** 一個 程式庫 repository 時,實際上是復制了一份 repository 到自己的GitHub 帳號下,換句話說,你的fork 成為了一個 遠端 **remote** 代碼庫 repository。通常我們通過 forks 用來建立自己所需使用的修改版本,或是協助原始項目修正錯誤、新增功能。
當你成功 forked 某個項目后,便可以從GitHub 將它 下載 **clone**到你的電腦上。這樣一來就可以在沒有網絡的狀況下,修改本地電腦里的項目內容。

## 步驟:Fork Patchwork 代碼庫 Repository
我們接下來要使用的專案是[github.com/jlord/patchwork](https://github.com/jlord/patchwork) 。到頁面上點擊右上方的 fork 按鈕,當 fork的動畫完成時,你的帳號中就會出現一份Patchwork 項目的副本。接下來,把在右邊側欄的HTTPS clone URL 復制起來。
## 步驟:在本地電腦上 下載 Clone Fork
現在,在命令行下載 clone 代碼庫 repository 到你的電腦上,他會自動替 代碼庫 repository建立一個新的文件夾,所以你不需要自己建立一個。但請注意不要在另一個Git程式庫 repository 文件夾中 下載 clone!所以,如果你還在先前挑戰所使用的'hello-world' 代碼庫 repository中,請先離開那個資料夾。
你可以透過切換文件夾指令以及兩個點來移動到文件夾的上一層:
```
$ cd ..
```
然后 下載 clone:
```
$ git clone <URL_FROM_GITHUB>
```
切換到剛剛建立的 fork 資料夾(在這個例子中叫做'patchwork'):
```
$ cd patchwork
```
現在你已經在電腦上得到了一份 代碼庫 repository 的副本,并且被自動連接到你GitHub 帳號下的 遠端 remote 代碼庫 repository(你的 fork 副本)。
### 每次`git clone`不需輸入賬號密碼的方法
最`git clone`,每次都要輸入帳號和密碼,感覺不舒服,于是乎就做了如下設置,然后就可以開心的提交啦~
Linux 或者 Mac 下方法:
創建文件,進入文件,輸入內容:
~~~
$ cd ~
$ touch .git-credentials
$ vim .git-credentials
https://{username}:{password}@github.com
~~~
在終端下輸入:
```
$ git config --global credential.helper store
```
打開`~/.gitconfig`文件,會發現多了一項:
```
[credential]
helper = store
```
這個時候輸入命令 `git clone http://username@url` 時不需要輸入密碼,即可完成代碼的 git
## 步驟:連接到原始的 代碼庫 Repository
但如果原始項目的內容有改變了呢?你會希望能夠收取 **pull**這些變更。所以讓我們來新增另外一個遠端 remote連結到原始的項目[github.com/jlord/patchwork](https://github.com/jlord/patchwork) ,代碼庫 repository 的URL 可以在GitHub 原始項目的側邊欄下方找到。
你可以隨意替這個 遠端 remote 連接命名,**但大家通常用'upstream',讓我們也用這個名字**:
```
$ git remote add upstream https://github.com/jlord/patchwork.git
```
## github中origin和upstream的區別

下面一段是來自 GitHub pages 的解釋:
*When a repo is cloned, it has a default remote called origin that points to your fork on GitHub, not the original repo it was forked from.
To keep track of the original repo, you need to add another remote named upstream
git remote add upstream git://github.com/user/repo_name.git*
總結下來:
1. 如果是 upstream repo,你只可以拉取最新代碼(即 git fetch ),從而保證你本地的倉庫與源倉庫同步
2. 如果是 origin repo,就是你自己的repo(自己創建的,或者 fork 的項目)你可以做 任何推拉操作(pull and push)
3. 你可以通過 pull request 向 upstream repo 貢獻代碼
## Tips
增加遠端 remote 連接
```
$ git remote add <REMOTE_NAME> <URL>
```
遠端 remote 連接
```
$ git remote -v
```
- 介紹
- 1. Get Git - 安裝并且設置Git
- 2. Repository - 建立一個本地的repository
- 3. Commit to it - 檢查狀態、新增或修改commits
- 4. GitHubbin - 注冊GitHub帳號
- 5. Remote Control - 將repository做本地和遠程的連接
- 6. Forks and Clones - Fork和clone一個開源的計劃
- 7. Branches aren't just for Birds - 建立一個feature branch
- Git分支開發模型
- 8. It's a Small World - 邀請并和別人合作
- 9. Pull, Never Out of Date - 利用Push和pull來和GitHub.com同步
- 10. Requesting You Pull Please - 建立一個pull request
- 11. Merge Tada - Merge和刪除branches
- 學會 GitHub
- GitHub 使用指南
- git 與 github 模板配置
- GitHub Actions
- 實用技巧
- Git Flow
- Submodule子模塊
- 工作記錄
- 常用命令
- 幫助
- 資源
- 解疑答惑