---
# System prepended metadata

title: git

---

# Git

## slido
有問題丟slido
這匿名的
https://app.sli.do/event/49FjvfjRWTCzhE5ipNgBJx

## 版本控制工具與git
版本控制工具分為兩種:分散式跟集中式

SVN(Subversion)是集中式
檔案庫都放在雲端，要有網路才能編輯

git是屬於分散式
每個人都可以有獨立的倉庫，所以可以直接對自己的倉庫進行操作。可以在離線的環境編輯

## 前置工作
**Register a Github account**
https://github.com/

**Download Github desktop**
https://desktop.github.com/?ref_cta=download+desktop&ref_loc=installing+github+desktop&ref_page=docs

**DOWNLOAD VISUAL STUDIO CODE (optional)**
https://code.visualstudio.com/Download
如果用wsl實作又手賤想選版本，別選1.86版，因為remote ssh不能用，原因如下:
https://github.com/microsoft/vscode-remote-release/issues/9467

非常推薦，vscode最頂文字編輯器
優點:用vscode看起來就比較會寫扣，功能超多，寫分手信也能用
缺點:剩下優點

## 實作
* create repository
* commit and push
* create branch


實務操作上的小例子:
A寫完commit push 交給B寫->B寫完commit push 給C->C完成所有程式碼->完成

其他merge/reset/pull request等操作就不說了，要用到的自己google

## 以下都補充，如果想用shell實作就自己加油
本來要講這些，但好像有點硬?
想學的自己斟酌
但應該不難
**install vscode extension(optional)**
1. Git Graph
2. Git History

**Install git**

```bash=
sudo apt install git #if you use wsl terminal
winget install --id Git.Git -e --source winget #if you use windows powershell terminal
```
windows/mac : https://git-scm.com/ 


**check:**
```bash=
git --version 
```

**setting config:**
```bash=
git config --global user.name "(your github user name)"
git config --global user.email "(email)"
```

**check config:**

```bash=
git config --global user.name 
git config --global user.email 

git config --list 
```

### 實作-如果你想打指令:


#### 建立資料夾
```bash=
cd ~
mkdir (folder name)
cd (folder name)
```

#### 建立檔案

```bash=
touch file1.txt # 建立file1.txt檔案
vim file1.txt #用vim編輯，可以跳過，windows 應該沒有vim，mac好像有
(type something) #optinal
(press ESC on your keyboard) #optional
:wq #optional
```

#### 指令
```bash=
git init
(create file1.txt)
git status # file1.txt是紅字，還未被加入
git add file1.txt 
git status # file1.txt是綠字，已被加入
git commit -m "first commit"
git log
(create file2.txt)
(create file3.txt)
git status
git add . #加入當前目錄下所有檔案
git commit  -m "second commit"
git log 
git branch branch2 #建立branch2
git checkout branch2 #切換到branch2
git branch
(create file4.txt)
git add .
git commit -m "commit in branch2"
git checkout - #切換回原分支，會看到file4.txt不見了
```
其他指令自己找，就不示範了

#### ssh key

```bash=
cd ~/.ssh #direct to .ssh folder
ls 
ssh-keygen -m PEM -t rsa -b 4096
#don't type anything, just enter 3 times
cat id_rsa.pub
(copy ssh key)
```

#### push
```bash=
git remote add origin git@github.com:(.......)
git branch -M main
git push -u origin main
```

#### git clone
```bash=
git clone https://github.com/sheng12077/git_clone
```

### 實作-如果不想打太多指令:

#### 建立資料夾
**for vscode**

File->open folder->(choose your folder)
or 
ctrl k + ctrl o ->(choose your folder)

#### 建立檔案
圖像化界面，非常直觀

#### 指令
```bash=
git init
(create file1.txt)
git status # file1.txt是紅字，還未被加入
git add file1.txt 
git status # file1.txt是綠字，已被加入
git commit -m "first commit"
git log
(create file2.txt)
(create file3.txt)
git status
git add . #加入當前目錄下所有檔案
git commit  -m "second commit"
git log 
git branch branch2 #建立branch2
git checkout branch2 #切換到branch2
git branch
(create file4.txt)
git add .
git commit -m "commit in branch2"
git checkout - #切換回原分支，會看到file4.txt不見了
```
其他指令自己找，就不示範了

#### ssh key

```bash=
cd ~/.ssh #direct to .ssh folder
ls 
ssh-keygen -m PEM -t rsa -b 4096
#don't type anything, just enter 3 times
cat id_rsa.pub
(copy ssh key)
```

#### push
```bash=
git remote add origin git@github.com:(.......)
git branch -M main
git push -u origin main
```

#### git clone
```bash=
git clone https://github.com/sheng12077/git_clone
```

### 補充中的補充
#### 快捷指令(mac/windows 的版本自己找，我懶)
For WSL:
```bash=
vim ~/zshrc # if you use zshrc
vim ~/.bashrc # if you use bashrc
```
```bash=
parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
COLOR_DEF='%f'
COLOR_USR='%F{4}'
COLOR_DIR='%F{15}'
COLOR_GIT='%F{118}'
NEWLINE=$'\n'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n@%M ${COLOR_DIR}%d ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}'
alias gcm="git checkout main"
alias gb="git branch -vv"
alias gp="git pull"
alias gl="git log --pretty=oneline"
alias gs="git status"
alias gc="git checkout"
```
```bash=
source ~/zshrc
source ~/.bashrc
```