---
# System prepended metadata

title: Git 101

---

# Git 101

# 內容

1. Git 的基本操作
2. 上傳程式碼到 gitlab

# 安裝

### SourceTree (https://www.sourcetreeapp.com/)

1. Log in with Atlassian or Google
2. Connect an Account? Skip Setup.
3. Load SSH Key? No.
4. Download an embedded version of Git for SourceTree alone to use
5. Download an embedded version of Mercurial for SourceTree alone to use

---

# Thinking in Graph

![](https://i.imgur.com/BFUIAMN.png)

![](https://i.imgur.com/4z0pHQK.jpg)

# 設定

1. git config --global user.email "your@example.com"
2. git config --global user.name "Your Name"

# 建立儲存庫（Repository）

1. 建立 demo 資料夾
2. 進入 demo 資料夾
3. 建立 Git 儲存庫

```
mkdir demo

cd demo

git init
```

# Commit

### 工作目錄狀態

```sequence
participant 工作目錄
participant 暫存區域
participant 儲存庫（本地）

工作目錄->暫存區域: git add
暫存區域->儲存庫（本地）: git commit
暫存區域->工作目錄: git reset HEAD <file>
```

### 第一個 Commit

1. 新增一個 Readme.md 檔案
2. 檢查 Git 狀態
3. 將 Readme.md 加入 Git
4. 建立第一個 commit

```
echo Hello Git! > Readme.md

git status

git add Readme.md

git commit -m 'initial commit'
```

```mermaid
graph RL
    subgraph Commits
        c1((C1))
    end

    Master-->c1
```

![](https://i.imgur.com/5WIMBr9.png)

### 練習

1. 新增一個檔案，並 Commit。
```
echo <h1>Hello World!</h1> > index.html

git add index.html

git commit -m 'add Hello World!'
```

```mermaid
graph RL
    subgraph Commits
        c2((C2))
        c1((C1))

        c2-->c1
    end

    master-->c2
```

2. 修改練習1的檔案，並 Commit。
```
vim index.html

git add index.html

git commit 
```

```mermaid
graph RL
    subgraph Commits
        c3((C3))
        c2((C2))
        c1((C1))

        c3-->c2
        c2-->c1
    end

    master-->c3
```

3. 如果不小心刪掉檔案 ，該如何復原？
```
rm index.html

git status

git checkout index.html
```

3. 修改最後一個 Commit。
```
git commit --amend
```

```mermaid
graph RL
    subgraph Commits
        c3((C3))
        c2((C2))
        c1((C1))

        c3-->c2
        c2-->c1
    end

    master-->c3
```

```mermaid
graph RL
    subgraph Commits
        c3((C3'))
        c2((C2))
        c1((C1))

        c3-->c2
        c2-->c1
    end

    master-->c3
```

# Reset

```mermaid
graph RL
    subgraph Commits
        c3((C3))
        c2((C2))
        c1((C1))

        c3-->c2
        c2-->c1
    end

    master-->c3
```

```mermaid
graph RL
    subgraph Commits
        c2((C2))
        c1((C1))

        c2-->c1
    end

    master-->c2
```

```
git reset head~1 --soft 
#刪除Commit，但保留 Commit 的修改在「暫存區域」。

git reset head~1
#刪除Commit，但保留 Commit 的修改在「工作目錄」。

git reset head~1 --hard
#刪除Commit，不保留 Commit。
```

### 工作目錄狀態

```sequence
participant 工作目錄
participant 暫存區域
participant 儲存庫（本地）

工作目錄->暫存區域: git add
暫存區域->儲存庫（本地）: git commit
暫存區域->工作目錄: git reset HEAD <file>
儲存庫（本地）->暫存區域: git reset --soft
儲存庫（本地）->工作目錄: git reset
```

# Revert

```
git revert <Commit SHA1>
```

```mermaid
graph RL
    subgraph Commits
        c3((C3))
        c2((C2))
        c1((C1))

        c3-->c2
        c2-->c1
    end

    master-->c3
```

```mermaid
graph RL
    subgraph Commits
        c2''((C2''))
        c3((C3))
        c2((C2))
        c1((C1))

        c2''-->c3
        c3-->c2
        c2-->c1
    end

    master-->c2''
```

# Checkout

```
git checkout <Commit SHA1>
```

```mermaid
graph RL
    subgraph Commits
        c3((C3))
        c2((C2))
        c1((C1))

        c3-->c2
        c2-->c1
    end

    master-->c3
    head-->c2
```

# 分支 Branch

### 新增分支
```
git branch develop
git checkout develop

or

git checkout -b develop
```

```mermaid
graph RL
    subgraph Commits
        c3((C3))
        c2((C2))
        c1((C1))

        c3-->c2
        c2-->c1
    end

    master-->c3
    head-->c2
```

```mermaid
graph RL
    subgraph Commits
        c3((C3))
        c2((C2))
        c1((C1))

        c3-->c2
        c2-->c1
    end

    master-->c3
    develop-->c2
```

```mermaid
graph RL
    subgraph Commits
        c4((C4))
        c3((C3))
        c2((C2))
        c1((C1))

        c4-->c2
        c3-->c2
        c2-->c1
    end

    master-->c3
    develop-->c4
```

### 刪除分支
```
git branch <name> -d
git branch <name> -D #強制刪除
```

```mermaid
graph RL
    subgraph Commits
        c6((C6))
        c5((C5))
        c4((C4))
        c3((C3))
        c2((C2))
        c1((C1))

        c6-->c5
        c5-->c2
        c4-->c3
        c3-->c2
        c2-->c1
    end

    master-->c4
    develop-->c6
```

```mermaid
graph RL
    subgraph Commits
        c4((C4))
        c3((C3))
        c2((C2))
        c1((C1))

        c4-->c3
        c3-->c2
        c2-->c1
    end

    master-->c4
```

# Merge

### *Straight merge

#### default
```
git merge master
```

```mermaid
graph RL
    subgraph Commits
        c6((C6))
        c5((C5))
        c4((C4))
        c3((C3))
        c2((C2))
        c1((C1))

        c6-->c5
        c5-->c2
        c4-->c3
        c3-->c2
        c2-->c1
    end

    master-->c4
    develop-->c6
```

```mermaid
graph RL
    subgraph Commits
        c7((C7))
        c6((C6))
        c5((C5))
        c4((C4))
        c3((C3))
        c2((C2))
        c1((C1))

        c7-->c4
        c7-->c6
        c6-->c5
        c5-->c2
        c4-->c3
        c3-->c2
        c2-->c1
    end

    master-->c4
    develop-->c7
```

#### *fast-forward
```
git merge master
```

```mermaid
graph RL
    subgraph Commits
        c4((C4))
        c3((C3))
        c2((C2))
        c1((C1))

        c4-->c3
        c3-->c2
        c2-->c1
    end

    develop-->c3
    master-->c4
```

```mermaid
graph RL
    subgraph Commits
        c4((C4))
        c3((C3))
        c2((C2))
        c1((C1))

        c4-->c3
        c3-->c2
        c2-->c1
    end

    master-->c4
    develop-->c4
```

### Squashed commit
```
git merge master --squash
```

```mermaid
graph RL
    subgraph Commits
        c6((C6))
        c5((C5))
        c4((C4))
        c3((C3))
        c2((C2))
        c1((C1))

        c6-->c5
        c5-->c2
        c4-->c3
        c3-->c2
        c2-->c1
    end

    master-->c4
    develop-->c6
```

```mermaid
graph RL
    subgraph Commits
        c7((C7))
        c6((C6))
        c5((C5))
        c4((C4))
        c3((C3))
        c2((C2))
        c1((C1))

        c7-->c6
        c6-->c5
        c5-->c2
        c4-->c3
        c3-->c2
        c2-->c1
    end

    master-->c4
    develop-->c7
```
```
C7 = C3 + C4
```


### cherry-pick
```mermaid
graph RL
    subgraph Commits
        c5((C5))
        c4((C4))
        c3((C3))
        c2((C2))
        c1((C1))

        c5-->c4
        c4-->c3
        c3-->c1
        c2-->c1
    end

    master-->c2
    develop-->c5
```

```mermaid
graph RL
    subgraph Commits
        c5((C5))
        c4'((C4'))
        c4((C4))
        c3((C3))
        c2((C2))
        c1((C1))

        c5-->c4
        c4-->c3
        c4'-->c2
        c3-->c1
        c2-->c1
    end

    master-->c4'
    develop-->c5
```

# Rebase
```
git rebase -i <Commit SHA1>

# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
```

```mermaid
graph RL
    subgraph Commits
        c6((C6))
        c5((C5))
        c4((C4))
        c3((C3))
        c2((C2))
        c1((C1))

        c6-->c5
        c5-->c4
        c4-->c3
        c3-->c2
        c2-->c1
    end

    master-->c6
```

```
C7 = C4 + C5
```

```mermaid
graph RL
    subgraph Commits
        c6((C6))
        c7((C7))
        c3((C3))
        c2((C2))
        c1((C1'))

        c6-->c7
        c7-->c2
        c3-->c1
        c2-->c3
    end

    master-->c6
```

---

```mermaid
graph RL
    subgraph Commits
        c5((C5))
        c4((C4))
        c3((C3))
        c2((C2))
        c1((C1))

        c5-->c4
        c4-->c1
        c3-->c2
        c2-->c1
    end

    master-->c3
    develop-->c5
```

```mermaid
graph RL
    subgraph Commits
        c5'((C5'))
        c4'((C4'))
        c3((C3))
        c2((C2))
        c1((C1))

        c5'-->c4'
        c4'-->c3
        c3-->c2
        c2-->c1
    end

    master-->c3
    develop-->c5'
```

# Gitignore

```
.gitignore
```

https://github.com/github/gitignore

# tag
```mermaid
graph RL
    subgraph Commits
        v>v0.1.0]
        c5((C5))
        c4((C4))
        c3((C3))
        c2((C2))
        c1((C1))

        c5-->c4
        c4-->c3
        c3-->c2
        c2-->c1
    end

    master-->c5
    v-->c4
```

# gitlab

### 儲存庫同步

```sequence
participant 儲存庫（本地）
participant 儲存庫（伺服器）

儲存庫（本地）->儲存庫（伺服器）: git push
儲存庫（伺服器）->儲存庫（本地）: git pull
```

```mermaid
graph RL
    subgraph Commits
        c5((C5))
        c4((C4))
        c3((C3))
        c2((C2))
        c1((C1))

        c5-->c4
        c4-->c3
        c3-->c2
        c2-->c1
    end

    master-->c3
    origin/master-->c5
```

```
git remote add <URL>
git push
```

```mermaid
graph RL
    subgraph Commits
        c5((C5))
        c4((C4))
        c3((C3))
        c2((C2))
        c1((C1))

        c5-->c4
        c4-->c1
        c3-->c2
        c2-->c1
    end

    master-->c3
    origin/master-->c3
    develop-->c5
    origin/develop-->c5
```

# git-svn

### 設定
```
git config --global core.autocrlf false
```

### Clone
```
git svn clone file:///tmp/test-svn -s

or

git svn init file:///tmp/test-svn
gitsvn fetch
```

```

```

### Config


