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 韋詠祥


課前準備

  • 安裝 Git 工具
  • 註冊 GitHub 帳號

安裝 Git 工具


macOS 安裝方式

先開啟 iTerm2 或 Terminal 終端機

輸入 git 指令,內建的 Xcode 將會跳出安裝資訊

git --version

Windows 安裝方式

搜尋 Git for Windows


依指示安裝

基本上都下一步,編輯器這邊可以改成自己慣用的


Linux 安裝方式

相信你知道怎麼做的 :D

# Debian / Ubuntu
apt install git
# Fedora
dnf install git
# Arch Linux
pacman -S git
# Alpine
apk add git

註冊 GitHub 帳號

網站:https://github.com/

基本 Linux 指令

  • 切換目錄
  • 顯示檔案內容
  • 查看目前狀態
  • 以下有 3 大部分,請跟著實作一次

先大概理解就好,也可以上網查「Bash XXX 用法」

Note:
2022/11/02
上學期教過初探 Linux


Linux 指令 Part 1

ls      # 列出目前目錄下的檔案
ls -a   # 包含隱藏檔案
ls -l   # 顯示詳細資訊
ls -al  # 我全都要!

pwd           # 顯示當前目錄名稱
cd Documents  # 進入資料夾
cd ..         # 回到前一層目錄
cd            # 不加參數,代表回到家目錄

Note:
知道在哪、探索、移動


Linux 指令 Part 2

mkdir mycode  # 建立資料夾
touch myfile  # 產生空檔案

mv myfile file1  # 移動檔案
cp file1 file2   # 複製檔案

echo Hey
echo "Hello   World" > file3

Linux 指令 Part 3

cat /etc/passwd   # 印出檔案內容
less /etc/passwd  # 慢慢查看檔案內容

rm file1      # 刪除檔案
rmdir mycode  # 刪除空資料夾

在開始之前,你有沒有看過


什麼是版本控制?


傳統中央控管型


分散式版本控制系統

Note:
對 Git 來說,支援分岔


Git 核心使用概念


開始使用 Git


環境設定

git config --global user.name "Sean"  # 你的暱稱
git config --global user.email me@sean.taipei
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 --"

環境設定(續)

git config --global gc.auto 0  # Disable garbage collection
git config --global pull.ff only  # Fast-forward only
git config --global core.excludesFile = ~/.gitignore
git config --global rerere.enable true  # Reuse Recorded Resolution
git config --global rebase.autosquash true
git config --global checkout.defaultRemote origin

Note:
Removed

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

cd ~/Documents
mkdir my-project  # 建立資料夾
cd my-project
git init          # 初始化 git
touch main.py     # 建立 main.py 或 main.cpp 檔案
git add main.py   # 把檔案從 working area 加入 staging area
git commit
# 在編輯器中輸入 commit 訊息:init
# 完成!

稍微改點東西

先用編輯器打開 main.py 檔案

explorer .   # Windows: 用檔案總管開啟資料夾
open .       # macOS:   開啟資料夾
EDIT main.py  # Linux:   直接編輯檔案

forrange()
讓他輸出 0 到 4 這五個數字,每次換行

完成後用 git 保存版本紀錄

git status  # 有誰被改到了
git add main.py
git commit

Note:
先講 status
下頁講 diff
再來是 show


需求變更

輸入:一個正整數 N

輸出:從 0 到 N-1 的數字,每次換行

git diff  # 我們改了什麼
git add -p main.py
git commit

Hint:用 int(input()) 接收輸入

Note:
剛講 status
現在是 diff
下頁講 show


再次修改

輸入:一個正整數 N

輸出:從 1 到 N 的數字,每次換行

git add -p .
git commit -m '<commit message>'

試著查看歷史紀錄

git show         # 預設為 HEAD
git show HEAD~1  # 往前 1 筆

Note:
講完 status、diff
這頁講 show


.gitignore

別把我的黑歷史記下來 (/ω\)


使用情境

當我們有個不必要的檔案


設定方式

echo __pycache__/ >> .gitignore
echo config.py >> .gitignore

cat .gitignore
git add .gitignore

補充:事後刪除敏感資料

假如我們要從 git 歷史中
完整刪除 pkg/ 底下所有 .deb 檔案

git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch pkg/*.deb' \
--prune-empty --tag-name-filter cat

參閱:凍仁的筆記(2014)


建立 GitHub Repo


設定遠端 Repo 網址

git remote add origin https://github.com/<User>/<Repo>.git
git remote -v
git push -u origin master

加入說明檔案

touch README.md  # 去編輯 README.md 檔案
git add README.md
git commit
git push

LICENSE 授權條款

git fetch --all
git pull
git log

完成!

讓我們進到 Git 進階用法


Git-flow


Git Branch

小練習:
輸入前詢問「Input N: 」
輸出 0 到 N-1

git switch -h                    # --help
git switch -c feat-prompt-input  # --create

Git Push

git push
git push -f  # --force
git push --force-with-lease

Git Restore

git restore
git clean -x -i -d  # no-ignore, interactive, directory

Git Stash

git stash
git stash pop

Git Rebase

git rebase -i master

Git Reflog

救回被 rebase 到不見的檔案

git reflog

Demo: Sea-n/my-python-project

Note:
找 secret
測試 rebase 後 reflog
提醒 working area / staging / git


學習資源

Learn Git Branching Challenge

網址:https://learngitbranching.js.org/


Thanks

投影片連結:https://hackmd.io/@Sean64/git-csst2023


CC-BY

這份投影片以 創用 CC - 姓名標示 授權公眾使用,原始碼及講稿請見 此連結
Select a repo