類別:DevOps 工具
| 發布於 2025-06-25 21:04
Git 常用指令筆記
基本 Git 操作
初始化與複製專案
| 指令 |
說明 |
git init |
初始化本地 Git 儲存庫 |
git clone <url> |
複製遠端儲存庫到本地 |
檢查狀態與提交變更
| 指令 |
說明 |
git status |
檢查目前工作區與暫存區的檔案狀態 |
git add <file> |
將指定檔案加入暫存區 |
git add . |
將所有變動檔案加入暫存區 |
git restore --staged <file> |
取消暫存指定檔案(相當於取消 git add) |
git commit |
提交暫存區的內容 |
git commit -m "<message>" |
提交並附上提交訊息 |
git log |
查看提交歷史 |
git log --oneline |
簡潔顯示提交紀錄 |
git log --oneline --all --graph |
以圖形方式顯示所有分支歷史 |
推送與拉取
| 指令 |
說明 |
git push |
將提交推送到遠端儲存庫(需先綁定) |
git push origin <branch> |
推送到遠端指定分支 |
git pull |
從遠端拉取更新並合併 |
git pull --rebase |
拉取遠端並進行 rebase |
分支操作
| 指令 |
說明 |
git branch |
查看本地分支 |
git branch -a |
查看本地與遠端所有分支 |
git branch <name> |
建立新分支 |
git branch -d <name> |
刪除指定分支 |
git branch -D <name> |
強制刪除指定分支 |
git branch -m <old> <new> |
重新命名分支 |
git checkout <branch> |
切換到指定分支 |
git checkout -b <name> |
建立並切換到新分支 |
回復與還原操作
| 指令 |
說明 |
git restore <file> |
還原檔案內容至最後一次提交 |
git checkout <file> |
同上,但為舊版指令(建議使用 restore) |
git clean -fd |
清除未追蹤的檔案與資料夾(小心使用) |
git stash |
暫存目前工作區變更(可還原) |
git stash pop |
還原最近一次 stash 的內容 |
Reset(重設提交紀錄)
| 指令 |
說明 |
git reset <commit> |
重設至指定提交(預設為 mixed 模式) |
git reset --soft <commit> |
保留暫存區與工作區變更 |
git reset --mixed <commit> |
保留工作區,清除暫存區(預設行為) |
git reset --hard <commit> |
清除所有變更(不可恢復) |
git reset HEAD^ |
回到上一次提交 |
git reset HEAD~2 |
回到前兩次提交 |
Rebase 與 Merge
| 指令 |
說明 |
git merge <branch> |
將指定分支合併至當前分支 |
git rebase <branch> |
將當前分支的基底重設為指定分支 |
遠端設定(remote)
| 指令 |
說明 |
git remote add <name> <url> |
新增遠端連結 |
git remote set-url <name> <url> |
修改遠端連結 |
git remote remove <name> |
移除遠端連結 |
git remote -v |
查看遠端連結設定 |
使用者設定(config)
| 指令 |
說明 |
git config --global user.name "<name>" |
設定全域使用者名稱 |
git config --global user.email "<email>" |
設定全域使用者 Email |
git config user.name "<name>" |
設定當前專案使用者名稱 |
git config user.email "<email>" |
設定當前專案 Email |
git config --unset user.name |
移除使用者名稱設定 |
git config --unset user.email |
移除 Email 設定 |
git config --list |
查看所有設定 |
查詢與除錯
| 指令 |
說明 |
git --version |
顯示 Git 版本 |
git help |
顯示說明 |
git help <command> |
查看指定指令的詳細說明 |
which git |
顯示 Git 執行檔位置 |
git reflog |
顯示 HEAD 移動紀錄 |
git blame <file> |
顯示每行的提交紀錄與作者 |
git log -p <file> |
查看指定檔案的修改歷史 |
其他指令
| 指令 |
說明 |
git checkout <commit_hash> |
切換到指定的 commit 狀態(進入 detached HEAD) |
git li |
(錯誤指令,應為 ls 或 ls -al 來查看目錄) |
註解
git stash 用於臨時保存變更,切換分支後可再還原。
git reset --hard 操作會永久刪除未提交的變更,使用前請先確認是否備份。
git restore 與 git switch 為 Git 2.23 之後新增的指令,建議取代舊有的
checkout 用法。
Git 常用指令筆記
基本 Git 操作
初始化與複製專案
git initgit clone <url>檢查狀態與提交變更
git statusgit add <file>git add .git restore --staged <file>git add)git commitgit commit -m "<message>"git loggit log --onelinegit log --oneline --all --graph推送與拉取
git pushgit push origin <branch>git pullgit pull --rebase分支操作
git branchgit branch -agit branch <name>git branch -d <name>git branch -D <name>git branch -m <old> <new>git checkout <branch>git checkout -b <name>回復與還原操作
git restore <file>git checkout <file>restore)git clean -fdgit stashgit stash popReset(重設提交紀錄)
git reset <commit>mixed模式)git reset --soft <commit>git reset --mixed <commit>git reset --hard <commit>git reset HEAD^git reset HEAD~2Rebase 與 Merge
git merge <branch>git rebase <branch>遠端設定(remote)
git remote add <name> <url>git remote set-url <name> <url>git remote remove <name>git remote -v使用者設定(config)
git config --global user.name "<name>"git config --global user.email "<email>"git config user.name "<name>"git config user.email "<email>"git config --unset user.namegit config --unset user.emailgit config --list查詢與除錯
git --versiongit helpgit help <command>which gitgit refloggit blame <file>git log -p <file>其他指令
git checkout <commit_hash>git lils或ls -al來查看目錄)註解
git stash用於臨時保存變更,切換分支後可再還原。git reset --hard操作會永久刪除未提交的變更,使用前請先確認是否備份。git restore與git switch為 Git 2.23 之後新增的指令,建議取代舊有的checkout用法。