Git Cheat Sheet

本篇不包含關於 Git 的概念教學

一些基礎設定

列出設定

列出目前的設定

1
$ git config list

列出全域設定

1
$ git config --global --list

列出目前 Git Repo 的設定

1
$ git config --local --list

修改設定

全域設定和個別 Git Repo 的設定分別加--global--local來設定,以下只舉全域為例

個人資料

1
2
$ git config --global user.name "Your Name"
$ git config --global user.email "Your Email"

GPG 簽章

1
2
$ git config --global commit.gpgsign "true"
$ git config --global user.signingkey "Your GPG Key"

預設分支名稱

1
$ git config --global init.defaultbranch "main"

Repo 操作

初始

建立 Repo

1
$ git init

複製 Repo

1
2
# URL可以是ssh或是http(s)的,但建議用ssh,比較方便
$ git clone <URL>

修改與提交

查看目前的修改狀態

1
$ git status

將檔案移至 stage

1
$ git add <file>

將檔案移出 stage

1
$ git reset <file>

Commit

1
$ git commit -m "commit message"

推送

1
$ git push

差異

查看目前工作目錄與 HEAD 的差異

1
$ git diff

查看 Stage 與 HEAD 的差異

1
$ git diff --staged

查看某個檔案與 HEAD 的差異

1
$ git diff <file>

查看兩個 Commit 的差異

1
$ git diff <commit1 hash> <commit2 hash>

查看某個檔案在兩個 Commit 的差異

1
$ git diff <commit1 hash> <commit2 hash> <file>

記錄

1
2
3
$ git log
$ git log --graph --oneline
$ git log --graph --oneline --all

分支與撤銷

建立分支

有很多種方式建立

1
2
3
4
5
6
# 建立但不切換
$ git branch <new branch name>
# 建立並切換
$ git checkout -b <new branch name>
# 建立並切換
$ git switch -c <new branch name>

切換分支

git checkout也可以切換,但checkout包含的功能很多,checkout被切分成多個指令,如switchrestore

1
$ git switch <branch name>

切換 Tag

1
$ git switch --detach <tag name>

將 B 的 Commit merge 進 A

1
2
$ git switch A
$ git merge B

Rebase

1
2
3
4
# 將目前的修改重新接到B branch新更新的位置
$ git rebase B
# 拉取時重新rebase
$ git pull --rebase

刪除分支快取

1
$ git fetch --all --prune

Reset

1
2
3
4
5
6
# 預設為--mixed,stage變成拆掉的commit內容,不修改工作區
$ git reset <commit>
# 只拆掉commit,不修改stage和工作區
$ git reset --soft <commit>
# 將stage清掉,工作區會到commit時的狀態
$ git reset --hard <commit>

Restore

checkout拆出來的指令,包含部分reset的功能,但不會修改HEAD,只動檔案

1
2
3
4
5
6
# 從stage還原到工作區
$ git restore <file>
# 將HEAD還原到stage,不動工作區
$ git restore --staged <file>
# 同時從HEAD還原到stage和工作區
$ git restore --staged --worktree <file>

Stash

列出暫存

1
$ git stash list

將工作區和 Stage 都暫時儲存

1
$ git stash

只暫存 stage

1
$ git stash push --staged

恢復暫存

1
$ git stash pop

Remote

列出所有 Remote

1
git remote show

增加 Remote

1
$ git remote add <remote name> <remote URL>

Git Cheat Sheet
https://www.zenwen.eu.org/git-cheat-sheet/
作者
Zen Wen
發布於
2025年9月2日
許可協議