
誒!原來 Git 還能這樣用!?
- Leo
- 技術宅 ( tech geek)
- 2025年4月28日
目錄
Git 除了常見的 git init
、git clone
、git add
、git commit
、git push
等指令外,還有許多不常見但非常實用的指令可以使用。
本篇就來分享我在程式開發過程中,有使用過但不經常使用的指令。然而,一旦用到,就會希望有個字典可以翻。那麼,這裡就是那個字典!
git clone 相關指令
曾經 clone
一包開源專案,剛好在咖啡廳網速比較慢,git clone
到天荒地老 … 如果那時候我知道 --depth
,我一定會感恩到痛哭流涕 (浮誇了XD)。
--depth
可以指定要 clone
下來的深度,也就是說,只會 clone
下來最新的 commit
,而不需要 clone
下來所有的 commit
。另外 --branch
參數可以指定分支,如果只是要查看某個倉庫的特定分支,可以使用此參數。
git clone <url> --depth 1 # 從遠端數據庫複製整個倉庫到本地端數據庫,但只複製最新的一次提交
git clone <url> --depth 1 --branch <branchname> # 從遠端數據庫複製指定分支的最新一次提交到本地端數據庫
git 訊息相關指令
有的時候,某個提交只是為了一個跟程式碼無關的或是很小的變更,並不想特別留下什麼紀錄,與其亂寫一些訊息,不如就留下空白:--allow-empty-message
就是為此存在的。
git commit --allow-empty-message -m "<message>" # 提交暫存區的檔案到倉庫且允許空訊息
啊!我還想補充一些資訊到最新的提交;我想把這次的變更也合併到最新的 commit
;我想 … 變更到最新的 …
你想要的,--amend
都為你準備好了。
git commit --amend -m "<message>" # 修改上一次提交的訊息
git commit --amend --no-edit # 將暫存區的檔案合併到上一次提交的 commit 中
git commit --amend --no-verify # 提交暫存區的檔案到倉庫,並跳過 pre-commit 檢查
git commit --amend --no-edit --date="now" # 將暫存區的檔案合併到上一次提交的 commit 中,並修改提交時間
git commit --amend --no-edit --date="now" --reset-author # 將暫存區的檔案合併到上一次提交的 commit 中,並修改提交時間與作者
什麼!?你說你要變更的不是最新的 commit
?沒問題,用 rebase
吧!
git rebase -i HEAD~n # 修改最近 n 次提交
git rebase -i --root # 修改所有提交
git branch 相關指令
你說,你想修改分支名稱?你說,你想捨棄一切從頭開始?
git branch -m <oldbranch> <newbranch> # 修改分支名稱
git checkout --orphan <branchname> # 創建一個新的分支,且不包含任何歷史記錄
git 遠端倉庫相關指令
讓我們跟 GitHub
建立關係吧!或是切八段?
git remote set-url origin <url> # 修改遠端數據庫的連結
git push -u origin <branchname> # 將本地端的分支推送到遠端數據庫,-u 參數會將本地端的分支與遠端數據庫的分支建立關聯
git push origin --delete <branchname> # 刪除遠端數據庫的分支
你說為什麼你已經把 GitHub
的 branch
都刪掉了,也在本地執行 git pull
了,VSCode
的 git
線圖還是會顯示?
因為有個東西叫 cache
,常常就是他在搞鬼,這就是為什麼重開機治百病!清 cache
也治百病哦~
git fetch --prune # 刪除本地端數據庫中已經不存在於遠端數據庫的分支
git 捨棄檔案
該怎麼捨棄一切呢?
git checkout -- . && git clean -df # 捨棄所有未追蹤的檔案
git rm -rf . # 從暫存區移除所有檔案
打包分支
有的時候,就是會遇到需要分享程式碼,可是對方沒有 GitHub
或是這個專案他不能通通看到,只好用笨方法:壓縮再解壓縮了!
git archive --format zip --output <filename> <branchname> # 將指定分支的原始碼打包成 zip 檔
git archive --format tar.gz --output <filename> <branchname> # 將指定分支的原始碼打包成 tar.gz 檔
.gitignore
重新設定
乾乾乾!為什麼???為什麽我已經把路徑添加到 .gitignore
了還是沒有忽略啊!?
不可能 git
這麼久了還有 bug 欸!
NoNoNo~ 下面這段給他複製貼上,magic 有沒有!
git rm -rf --cached . # 從暫存區移除所有已追蹤的檔案
git add . # 將所有檔案添加到暫存區
git commit -m "chore: reset .gitignore" # 將暫存區的檔案提交到倉庫
git submodule
添加以及移除
你說你想加入其他 git
倉庫嗎?
git submodule add <url> <path> # 添加 submodule,並指定 submodule 的路徑
git submodule update --init --recursive # 初始化 submodule,並將 submodule 更新到最新的 commit
git submodule update --remote --merge # 更新 submodule,並將 submodule 更新到最新的 commit,並合併到當前分支
然後現在你跟我說你要移除?
git submodule deinit <path> # 移除 submodule
git add .gitmodules # 將 .gitmodules 添加到暫存區
git rm --cached <path> # 將 submodule 從暫存區移除
rm -rf .git/modules/<path> # 刪除 submodule 的 .git 資料夾
git commit -m "chore: remove submodule"
rm -rf <path> # 刪除 submodule 的資料夾
git push origin <branchname> # 將本地的分支推送到遠端數據庫
pre-commit 相關指令
這是用來限制那些不夠 J 的人 …
pre-commit run --files $(git diff --name-only --cached) # 對暫存區的檔案進行檢查
git 後悔藥
你說你後悔了?交給 git
吧!
git revert <commit> # 將指定 commit 的更改反向提交