← 返回所有文章

Git

UtilsGit作者:VincentCorgi約 1 分鐘閱讀

功能

收錄日常 Git 速查:遠端與 rebase、修改歷史 commit 訊息、reset/stash、分支操作、submodule 等常用指令與注意事項。

情境

  • 忘記先 pull 就本地 commit、需要安全推送或整理線性歷史時對照「遠端與 rebase」段落。
  • 誤提交、要改 message、或專案含 submodule 需在多台機器同步時,依段落找對應指令。

Git

遠端與 rebase

Code
# 先將遠端最新提交下載並「接到」目前分支後面(rebase),避免多一個 merge commit
# 若本機尚未 push 的 commit 與遠端衝突,會停下來請你解 conflict
git pull --rebase origin
Code
# 範例輸出(已與遠端同步時)
Already up to date.

# 範例輸出(rebase 成功)
Successfully rebased and updated refs/heads/main.

修改以前的 commit 訊息

Code
# 互動式 rebase:從 <commit_hash>「之後」的提交開始編排(該 hash 通常是「要改的那筆」的父提交)
# 編輯器開啟後把要改的該行前面的 pick 改成 reword(r),存檔關閉後 Git 會再開一次讓你改訊息
git rebase -i <commit_hash>
# 編輯器內常見指令:pick 保留、s 擠壓、f 擠壓不保留訊息、d 刪除、r 改訊息
# 已推上遠端且有人基於其開發時,改歷史風險高;僅在確定可協調時使用
git push --force-with-lease
Code
# 範例:force-with-lease 成功(遠端沒被別人推過新東西)
To github.com:user/repo.git
 + a1b2c3d...e4f5g6h main -> main (forced update)

# 若遠端已有你不知道的更新,會拒絕(比 -f 安全)
! [rejected]        main -> main (stale info)

還原成遠端狀態

Code
# 本機搞亂了、想以遠端 <branch> 為準再 rebase 一次(會依你本機與遠端差異而有不同結果,執行前確認沒有要保留的未推送工作)
git pull --rebase origin <branch>
Code
# 範例(無新東西可拉)
From github.com:user/repo
 * branch            main     -> FETCH_HEAD
Already up to date.

git pull vs. git fetch

  • git pull 會直接合併
  • git fetch 只會拉下來不會做合併

刪除分支

Code
# 刪除遠端分支(需有權限;遠端名常為 origin)
git push -d <remote_name> <branchname>
# 刪除本機已合併的分支(未合併會拒絕)
git branch -d <branchname>
# 強制刪除本機分支(未合併也刪)
git branch -D <branch_name>
Code
# 範例:刪除遠端分支
To github.com:user/repo.git
 - [deleted]         feature/foo

# 範例:刪除本機分支
Deleted branch feature/foo (was a1b2c3d).

同步遠端已刪除的分支

Code
# 在其他機器執行:更新所有遠端,並清掉本地已不存在的遠端追蹤分支(refs/remotes/...)
git fetch --all --prune
# 只針對 origin
git fetch origin --prune
Code
# 範例(有 prune 掉過時的遠端追蹤分支時可能看到)
From github.com:user/repo
 - [deleted]         (none)     -> origin/old-feature

git reset

Code
# 工作目錄與索引都回到指定 commit(危險:未提交變更會丟失,除非已 stash)
git reset --hard <commit-id>
# --mixed(預設):索引清空,工作目錄保留檔案變更
# --soft:索引與工作目錄都保留,只移動 HEAD
Code
# 成功時通常無輸出,或僅顯示 HEAD 位置
HEAD is now at a1b2c3d commit message

常用指令

Code
# 單行精簡歷史,適合快速找 commit
git log --oneline
# 強制推送(覆寫遠端歷史,團隊協作慎用;較安全用 --force-with-lease)
git push -f
# 把某個 commit 的變更套到目前分支
git cherry-pick <commit-hash>
Code
# 範例:git log --oneline
a1b2c3d (HEAD -> main, origin/main) docs: update readme
e4f5g6h fix: handle null input
9z8y7x6 feat: add login

# 範例:cherry-pick 成功
[main 0f1e2d3] fix: typo
 Date: ...
 1 file changed, 2 insertions(+), 1 deletion(-)

stash

Code
# 看最新 stash 的檔案層級差異摘要(不含完整 diff)
git stash show
# -p:顯示完整 patch,等同 git diff
git stash show -p
# 列出所有 stash 條目
git stash ls
Code
# 範例:git stash list
stash@{0}: WIP on main: a1b2c3d last commit msg
stash@{1}: On feature/x: temp work
Code
# apply:套用 stash 但不從清單刪除(可重複套用,可能衝突)
git stash apply              # 最新一筆
git stash apply stash@{1}    # 指定序號
Code
# 範例(成功)
On branch main
Changes not staged for commit:
  modified:   src/app.ts
Code
# pop:套用並刪除該筆 stash(與 apply 二選一即可)
git stash pop
git stash pop stash@{1}
Code
# 刪除 stash:clear 全部;drop 刪一筆
git stash clear
git stash drop
git stash drop stash@{1}
Code
# 範例:drop 一筆
Dropped stash@{0} (a1b2c3d...)

分支列表

Code
# 只看本機分支,目前分支前有 *
git branch
# 本機 + 遠端追蹤分支
git branch -a
Code
# 範例:git branch -a
* main
  remotes/origin/main
  remotes/origin/feature/foo

git config

Code
# 列出目前生效的設定(含 local / global / system)
git config --list
# 只看 global 檔
git config --global --list
# 讀單一 key,例如 user.name
git config user.name
Code
# 範例:git config --list(節錄)
user.name=Your Name
user.email=you@example.com
core.autocrlf=input
Code
# 設定使用者名稱與信箱(第一次裝 Git 常做)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# 僅限目前 repo(寫入 .git/config)
git config user.name "Your Name"
git config user.email "you@example.com"
Code
# 設定成功時通常無輸出;可再用 git config user.name 驗證
Your Name
Code
# init 預設主分支名稱、預設編輯器、pull 時預設用 rebase
git config --global init.defaultBranch main
git config --global core.editor "code --wait"
git config --global pull.rebase true
Code
# 自訂短指令:git st = git status
git config --global alias.st status
git config --global alias.lg "log --oneline -10"
git config --global alias.last "log -1 HEAD"
Code
# 範例:使用 alias
$ git st
On branch main
nothing to commit, working tree clean
Code
# 刪除一筆設定;若同一 key 有多筆(較少見)用 unset-all
git config --global --unset user.somekey
git config --global --unset-all some.multi.key
Code
# 成功時通常無輸出