git 教學
===
###### tags: `github` `winter_camp` `tutorial`
---
[TOC]
# 簡介

## Why git?
<iframe src="https://www.slideshare.net/slideshow/embed_code/key/2gXpdq3uoGwdEW?hostedIn=&referer=" width="760px" height="570px" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:none;" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe>
---
# git 架構

# Linux 基本操作
[Link](https://hackmd.io/@NCTU-auv/H1TLKa51P)
# git 實行
https://gitforwindows.org/
## 文字編輯器
- vim
```
$ sudo apt-get install vim
```
## 設定 ssh-key 金鑰
[設定教學](https://blog.jaycetyle.com/2018/02/github-ssh/)
## Example
file strcture
```
- /project
- happy.cpp
```
happy.cpp
``` cpp=
#include<iostream>
int main(){
std::cout << "hello world";
}
```
## git init
``` bash
//為資料夾做git初始化
$ git init
```
## git status
``` bash
//顯示git 目前的工作環境狀態
$ git status
```
## git diff
``` bash
//在commit 之前 先確定有改了多少東西
$ git diff
```
視覺化工具
```bash
meld [file A] [file B] #比較兩個檔案
meld [file A] #比較此檔案與前一版本之差異
```
## git add & git reset
```bash
//將git加入暫存當中
$ git add [檔名]
//全部加入
$ git add .
//將檔案由暫存清空
$ git reset [檔名]
//將暫存回覆到某個commit
$ git reset
```

## git commit
### git commit
``` bash
//寫下註解
//-m message 最簡單的註解方式
$ git commit -m " "
```
## git config (設定資料或指令)
```git
//增加設定
$ git config --global --add user.name eason27271563
$ git config --global --add user.email eason27271563.me05@nctu.edu.tw
//删除設定
$ git config --global --unset user.name
//改便設定
$ git config --global user.name EasonHuang-tw
//查詢已建立的設定
git config --list
```
## Something important
### ReadMe.md
- 使其他人了解專案內容與執行方式
- 用 MarkDown 形式寫
EX:
https://github.com/zonghan0904/NCRL_Huskey_CV/blob/master/README.md
### 需要忽視的檔案
.gitignore 內寫入不要被git 追縱的檔案
EX:不想追蹤執行檔
```
*.exe
```
# Github
## 新增一個repository


```git
// 本地端專案知道 origin 對應到遠端網址
git remote add origin https://github.com/eason27271563/tutorial.git
```
#### Github default 分支 改名 (master -> main)
- 2020/10以後,github 會將原本的分支命名為 main ,跟我們習慣git 上面的 master 不同。如果直接推上去會導致github上有一個空的main分支,加上一個有檔案的master分支。**下面提供兩種方法,選一個就好。**
- 更改 Local 端
```
git branch (看一下自己現在的分支)
git branch -M main
```
- 推送你的專案
```
// push
git push -u origin main or
//-u 就是 --set-upstream
//之後就只要
git push
//若要從github 上抓更新的程式則使用,例如你的同學更新了某個功能
git pull
```
# 版本管理
## git branch

```git
git branch "brnach 的名稱"
git checkout "branch 的名稱"
//作一些修改之後
git add .
git commit -m "XDDD"
git push origin "branch 的名稱"
git log --graph
```
## git merge 檔案合併

回到 master 分支產生一些改變
```git
git checkout main
//作一些修改之後
git add .
git commit -m "註解"
git merge "branch 名稱"
```
## 觀看history
```
git log --graph
```
EX: openCV

## git fork 專案參與

所有人都可以去fork一個專案,這樣擁有者就不用煩惱把大家都加進去協作者了(上百人一起開發)。

- 按下 fork 按鈕後,你可以建立一個自己的副本,可以有權限可以更改
### 流程如下
1. 從 main 建立一個主題分支。
2. 加入一些變更來改善這個專案。
3. 把這個分支推送到你的 GitHub 專案。
4. 在 GitHub 上建立一個 Pull Request。
5. 專案擁有者視情況決定要把這個 Pull Request 合併進原始專案,或是關閉它。
## git clone
### git clone 下載自己的repository
```git
//按下clone 按鈕 並複製網址
git clone "複製的網址"
```
# reference
[Git 與 Github 版本控制基本指令與操作入門教學](https://blog.techbridge.cc/2018/01/17/learning-programming-and-coding-with-python-git-and-github-tutorial/)
[連猴子都能讀懂的github](https://backlog.com/git-tutorial/tw/intro/intro1_1.html)
[參與一個專案](https://git-scm.com/book/zh-tw/v2/GitHub-%E5%8F%83%E8%88%87%E4%B8%80%E5%80%8B%E5%B0%88%E6%A1%88)