Git 以「快照」而非「差異」儲存版本。每次提交對所有檔案建立快照並產生唯一 SHA-1 雜湊,未變更檔案僅建立連結,極大提升效能與完整性。
git init
touch README.md
git add .
git commit -m "Initial commit"
git clone https://github.com/torvalds/linux.git
# 完整複製歷史紀錄,離線也能瀏覽所有版本
git status
# 顯示工作區、暫存區檔案狀態:未追蹤、已修改、已暫存
git add <file> # 指定檔案
git add . # 所有變更
git add -p # 互動式選擇區塊
git commit -m "訊息"
git commit --amend --reset-author # 修正上一筆提交
git log --oneline --graph --all # 圖形化顯示分支歷史
git log -p # 顯示每次提交的差異
git diff # 工作區 vs 暫存區
git diff --staged # 暫存區 vs 最後提交
git diff HEAD # 工作區 vs 最後提交
git rm file.txt # 刪除並暫存
git mv old.txt new.txt # 重命名並暫存
git branch feature-x # 建立分支
git checkout feature-x # 切換分支
git checkout -b feature-y # 建立並切換
git merge feature-x # 合併至當前分支
git branch -d feature-x # 刪除已合併分支
git remote add origin <url>
git push -u origin master
git pull origin master
git fetch --all --prune
git stash 暫存未完成工作,git stash pop 還原。git bisect 二分查找引入 Bug 的提交。git rebase -i 互動式整理提交歷史。