# Git 基礎實戰演練 CSST 程式設計討論會 2023 Author: Sean 韋詠祥 Note: 日期:2023-05-10(三) 時間:19:00 - 21:00 TODO: webserver .git/config merge conflict git reflog reflog game git commit git tag gui git blame ---- ## 講者介紹 - 陽交資工大四 Sean Wei 韋詠祥 ![](https://i.imgur.com/E7CdAg1.png) ---- ## 課前準備 - 安裝 Git 工具 - 註冊 GitHub 帳號 ---- ## 安裝 Git 工具 ---- ## macOS 安裝方式 先開啟 [iTerm2](https://iterm2.com/) 或 Terminal 終端機 ![](https://i.imgur.com/8jX4z8v.png) 輸入 git 指令,內建的 Xcode 將會跳出安裝資訊 ```bash git --version ``` ---- ## Windows 安裝方式 搜尋 Git for Windows ![](https://i.imgur.com/byaJvzk.png) ###### Link: https://gitforwindows.org/ ---- ## 依指示安裝 基本上都下一步,編輯器這邊可以改成自己慣用的 ![](https://i.imgur.com/OmETRqD.png) ---- ## Linux 安裝方式 相信你知道怎麼做的 :D ```bash # Debian / Ubuntu apt install git ``` ```bash # Fedora dnf install git ``` ```bash # Arch Linux pacman -S git ``` ```bash # Alpine apk add git ``` ---- # 註冊 GitHub 帳號 ![](https://i.imgur.com/hm0v8qW.png) ###### 網站:https://github.com/ --- # 基本 Linux 指令 - 切換目錄 - 顯示檔案內容 - 查看目前狀態 - 以下有 3 大部分,請跟著實作一次 先大概理解就好,也可以上網查「Bash XXX 用法」 Note: 2022/11/02 上學期教過初探 Linux ---- ## Linux 指令 Part 1 ```bash ls # 列出目前目錄下的檔案 ls -a # 包含隱藏檔案 ls -l # 顯示詳細資訊 ls -al # 我全都要! ``` <br> ```bash pwd # 顯示當前目錄名稱 ``` ```bash cd Documents # 進入資料夾 cd .. # 回到前一層目錄 cd # 不加參數,代表回到家目錄 ``` Note: 知道在哪、探索、移動 ---- ## Linux 指令 Part 2 ```bash mkdir mycode # 建立資料夾 ``` ```bash touch myfile # 產生空檔案 ``` <br> ```bash mv myfile file1 # 移動檔案 ``` ```bash cp file1 file2 # 複製檔案 ``` <br> ```bash echo Hey echo "Hello World" > file3 ``` ---- ## Linux 指令 Part 3 ```bash cat /etc/passwd # 印出檔案內容 ``` ```bash less /etc/passwd # 慢慢查看檔案內容 ``` <br> ```bash rm file1 # 刪除檔案 ``` ```bash rmdir mycode # 刪除空資料夾 ``` --- ## 在開始之前,你有沒有看過... ![](https://i.imgur.com/uZArLj9.png) ---- ## 什麼是版本控制? ![](https://i.imgur.com/HqNNopD.png) ---- ## 傳統中央控管型 ![](https://i.imgur.com/cT78gbx.png) ---- ## 分散式版本控制系統 ![](https://i.imgur.com/OkbeF6I.png) Note: 對 Git 來說,支援分岔 ---- ## Git 核心使用概念 ![](https://i.imgur.com/x5r0Y34.png) --- # 開始使用 Git ---- ## 環境設定 ```bash git config --global user.name "Sean" # 你的暱稱 git config --global user.email me@sean.taipei ``` ```bash git config --global alias.lg "log --color --graph --all --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --" ``` <!-- .element: class="wrap-code" --> ---- ## 環境設定(續) ```bash git config --global gc.auto 0 # Disable garbage collection git config --global pull.ff only # Fast-forward only git config --global core.excludesFile = ~/.gitignore ``` ```bash git config --global rerere.enable true # Reuse Recorded Resolution git config --global rebase.autosquash true ``` ```bash git config --global checkout.defaultRemote origin ``` <!-- .element: class="wrap-code" --> Note: Removed ```bash git config --global core.editor 'vim' git config --global merge.tool vimdiff git config --global init.defaultBranch master git config --global url."git@github.com:".insteadOf "https://github.com/" ``` ---- ## 建立第一個本地 Repo Git Repository ```bash cd ~/Documents mkdir my-project # 建立資料夾 cd my-project git init # 初始化 git ``` ```bash touch main.py # 建立 main.py 或 main.cpp 檔案 git add main.py # 把檔案從 working area 加入 staging area ``` ```bash git commit # 在編輯器中輸入 commit 訊息:init ``` ```bash # 完成! ``` ---- ## 稍微改點東西 先用編輯器打開 `main.py` 檔案 ```bash explorer . # Windows: 用檔案總管開啟資料夾 open . # macOS: 開啟資料夾 EDIT main.py # Linux: 直接編輯檔案 ``` 用 `for` 跟 `range()` 讓他輸出 0 到 4 這五個數字,每次換行 完成後用 git 保存版本紀錄 ```bash git status # 有誰被改到了 git add main.py git commit ``` Note: 先講 status 下頁講 diff 再來是 show ---- ## 需求變更 輸入:一個正整數 N 輸出:從 0 到 N-1 的數字,每次換行 ```bash git diff # 我們改了什麼 git add -p main.py git commit ``` Hint:用 `int(input())` 接收輸入 Note: 剛講 status 現在是 diff 下頁講 show ---- ## 再次修改 輸入:一個正整數 N 輸出:從 1 到 N 的數字,每次換行 ```bash git add -p . git commit -m '<commit message>' ``` 試著查看歷史紀錄 ```bash git show # 預設為 HEAD git show HEAD~1 # 往前 1 筆 ``` Note: 講完 status、diff 這頁講 show --- # .gitignore 別把我的黑歷史記下來 (/ω\) ---- ## 使用情境 當我們有個不必要的檔案 ![](https://hackmd.io/_uploads/SkZDoquEh.png) ---- ## 設定方式 ```bash echo __pycache__/ >> .gitignore echo config.py >> .gitignore cat .gitignore git add .gitignore ``` ---- ## 補充:事後刪除敏感資料 假如我們要從 git 歷史中 完整刪除 `pkg/` 底下所有 `.deb` 檔案 ```bash git filter-branch --force --index-filter \ 'git rm --cached --ignore-unmatch pkg/*.deb' \ --prune-empty --tag-name-filter cat ``` 參閱:[凍仁的筆記](http://note.drx.tw/2014/01/git-remove-sensitive-data.html)(2014) --- # 建立 GitHub Repo ![](https://hackmd.io/_uploads/HyX0exKV3.png) ---- ## 設定遠端 Repo 網址 ![](https://i.imgur.com/kqC1gao.png) ```bash git remote add origin https://github.com/<User>/<Repo>.git git remote -v git push -u origin master ``` ---- ## 加入說明檔案 ![](https://hackmd.io/_uploads/rkYNbeYE3.png) ```bash touch README.md # 去編輯 README.md 檔案 git add README.md git commit git push ``` ---- ## LICENSE 授權條款 ![](https://i.imgur.com/2yxyoDG.png) ```bash git fetch --all git pull git log ``` ---- ## 完成! 讓我們進到 Git 進階用法 --- ## Git-flow ![](https://i.imgur.com/wRgeAvb.png) ---- # Git Branch 小練習: 輸入前詢問「Input N: 」 輸出 0 到 N-1 ```bash git switch -h # --help git switch -c feat-prompt-input # --create ``` ---- ## Git Push ```bash git push ``` ```bash git push -f # --force git push --force-with-lease ``` ---- ## Git Restore ```bash git restore ``` ```bash git clean -x -i -d # no-ignore, interactive, directory ``` ---- ## Git Stash ```bash git stash git stash pop ``` ---- ## Git Rebase ```bash git rebase -i master ``` ---- ## Git Reflog 救回被 rebase 到不見的檔案 ```bash git reflog ``` Demo: [Sea-n/my-python-project](https://github.com/Sea-n/my-python-project) Note: 找 secret 測試 rebase 後 reflog 提醒 working area / staging / git --- ## 學習資源 Learn Git Branching Challenge ![](https://hackmd.io/_uploads/ryTC5Au4n.png) 網址:https://learngitbranching.js.org/ ---- # Thanks 投影片連結:https://hackmd.io/@Sean64/git-csst2023 <!-- .element: class="r-fit-text" --> <br> [![CC-BY](https://mirrors.creativecommons.org/presskit/buttons/88x31/png/by.png)](https://creativecommons.org/licenses/by/4.0/deed.zh_TW) ###### 這份投影片以 [創用 CC - 姓名標示](https://creativecommons.org/licenses/by/4.0/deed.zh_TW) 授權公眾使用,原始碼及講稿請見 [此連結](https://hackmd.io/@Sean64/git-csst2023/edit)。 <style> .reveal pre.wrap-code code { white-space: pre-wrap; } </style>
{"metaMigratedAt":"2023-06-18T02:42:33.132Z","metaMigratedFrom":"YAML","title":"Git 基礎實戰演練 - CSST 2023","breaks":"true","description":"Sean 韋詠祥 / 2023-05-10","contributors":"[{\"id\":\"8a6148ae-d280-4bfd-a5d9-250c22d4675c\",\"add\":8402,\"del\":1777}]"}
    687 views