快轉到主要內容

Git 常用設定與指令

· 0
目錄

Git 常用設定 #

git config --global user.name "<username>" # 設定使用者名稱
git config --global user.email "<email>" # 設定使用者信箱

git config --global alias.ci "commit --allow-empty-message" # git ci -m "<message>" / 用來提交訊息的指令,且允許空訊息
git config --global alias.co "checkout" # git co <branch-name> / 切換分支
git config --global alias.ignore "!gi() { curl -sL https://www.gitignore.io/api/$@ ;}; gi" # git ignore <language> >> .gitignore / 產生 .gitignore 檔案
git config --global alias.ll "log --oneline" # git ll / 顯示 commit 訊息 ; git ll -n <number> / 顯示最近 n 個 commit 訊息
git config --global alias.ss status # git ss / 確認當前 git 狀態
git config --global alias.zip "archive --format zip --output" # git zip SourceCode.zip <branch-name> / 將指定分支的原始碼打包成 zip 檔

git config --global core.autocrlf input
git config --global core.longpaths true
git config --global svn.rmdir true # 使 git svn dcommit 時會自動刪除 SVN 庫空白的資料夾 (但是不會刪除非空白的資料夾)
git config --global tag.sort version:refname # 使 tag 按照時間正序排列 (預設為反序)
git config --global -l # 列出所有設定

Git 常用指令 #

git init # 初始化 Git 倉庫
git add <filename> # 將檔案加入暫存區
git add . # 將所有檔案加入暫存區
git commit -m "<message>" # 提交暫存區的檔案到倉庫
git commit --allow-empty-message -m "" # 提交空訊息
git commit --amend -m "<message>" # 修改最後一次提交的 commit 訊息
git commit --amend --no-edit --date="now" # 修改最後一次提交的 commit 訊息,但不修改訊息內容,並將時間改為現在

git clone <url> # 從遠端數據庫複製整個倉庫到本地端數據庫
git clone <url> --depth 1 # 從遠端數據庫複製整個倉庫到本地端數據庫,但只複製最新的一次提交
git clone <url> --depth 1 --branch <branchname> # 從遠端數據庫複製指定分支的最新一次提交到本地端數據庫

git push origin <branchname> # 將本地端的分支推送到遠端數據庫
git push -u origin <branchname> # 將本地端的分支推送到遠端數據庫,-u 參數會將本地端的分支與遠端數據庫的分支建立關聯
git push origin --delete <branchname> # 刪除遠端數據庫的分支
git pull origin <branchname> # 從遠端數據庫拉取分支到本地端數據庫

git checkout -b <branchname> # 建立分支,並切換到該分支
git checkout <branchname> # 切換分支
git branch -D <branchname> # 刪除分支
git branch -m <oldbranch> <newbranch> # 修改分支名稱

git rebase <branchname> # 將指定分支的 commit 合併到當前分支 (不會產生 merge commit)
git merge <branchname> # 將指定分支的 commit 合併到當前分支 (會產生 merge commit)

git rebase -i HEAD~<number> # 將最近 n 個 commit 合併成一個 commit
git rebase -i <commit-id> # 將指定 commit 之後的所有 commit 合併成一個 commit

重新設定 .gitignore #

有時候會遇到 .gitignore 沒有生效的情況,有可能是因為 .gitignore 的某些設定是在 已經 track 某個或某類型檔案之後才加入的,所以 git 並不會忽略 .gitignore 裡面新設定的檔案,這時候可以使用以下指令重新設定 .gitignore。

git rm -rf --cached . # 將所有已經 track 的檔案從暫存區移除
git add . # 將所有檔案加入暫存區
git commit -m "Reset .gitignore" # 提交暫存區的檔案到倉庫

創建空的分支 #

git checkout --orphan <branchname> # 創建空的分支
git rm -rf . # 將所有檔案從暫存區移除
# 至少新增一個檔案後執行下列指令
git add . # 將所有檔案加入暫存區
git commit -m "Initial commit" # 提交訊息
git push origin <branchname> # 將本地端的分支推送到遠端數據庫

Git Submodule #

Submodule 新增與更新 #

git submodule add <url> <path> # 新增 submodule,並指定 submodule 的路徑
git submodule update --init --recursive # 初始化 submodule,並將 submodule 更新到最新的 commit
git submodule update --remote --merge # 更新 submodule,並將 submodule 更新到最新的 commit,並合併到當前分支

Submodule 移除 #

git submodule deinit <path> # 移除 submodule
git add .gitmodules # 將 .gitmodules 加入暫存區
git rm --cached <path> # 將 submodule 從暫存區移除
rm -rf .git/modules/<path> # 刪除 submodule 的 .git 資料夾
git commit -m "Remove submodule" # 提交訊息
rm -rf <path> # 刪除 submodule 的資料夾
git push origin <branchname> # 將本地端的分支推送到遠端數據庫

Related

使用 GitHub Actions 自動建立各版本的 Hugo Docker Image
· 0
Git SSH Key 問題解決指南:如何克服 Permission Denied (publickey) 錯誤
· 0
使用 Docker buildx 建立多平台的 Docker Image
· 0

boba-icon
請我喝珍奶!