記得會很方便的 Git 指令

Dian
5 min readSep 29, 2022

--

調整 Remote Repo & Branch 相關

檢查 git remote url

$ git remote -v
origin ssh://git@git.host.com/path/to/git (fetch)
origin ssh://git@git.host.com/path/to/git (push)

更新 remote url

$ git remote set-url origin ssh://git@new.host.com/path/to/git

新增一個 remote

$ git remote add another-servier ssh://git@another.host.com/git

checkout 另一個 remote 的 branch

$ git checkout --track another-servier/branch_name

Case

更換 remote repo

# 確認當前設定
$ git remote -v
origin ssh://dian@git.host.com/home/git/project.git (fetch)
origin ssh://dian@git.host.com/home/git/project.git (push)

# 將 newgit.host.com 加入 remote repo
$ git remote add new_git ssh://git@newgit.host.com/git/project.git

# checkout new_git 的 branch,命名為 new_master
$ git checkout --track new_git/master -b new_master

# 將原本未推送的 commit cherry pick 或是 merge 到 new_master
$ git cherry-pick abcde12345

# 因為 new_git沒有 new_master 這個 branch,所以要指定推送的 remote branch
$ git push new_git HEAD:master

# 移除或是 rename 原本 legacy 的 branch
$ git branch -m master legacy_master
$ git branch -D master

# 再把 new_master 改名為 master 與 server 一致
$ git branch -m new_master master

整理 Local branch

更新 git 狀態
$ git fetch --all --prune

列出所有branch
$ git branch -vv

gone] 當 keyword 找到已經不在遠端上的 branch
$ git branch -vv | grep gone]

Git Submodule

$ git submodule add $git.repo $path
// or
$ git submodule add -b $branchName $git.repo $path
$ git submodule update --init --recursive

刪除 Submodule 步驟

// 移除 .gitmodules
$ rm .gitmodules
// 移除 [submodule xyz] section
$ vi .git/config
// 移除 .git/modules 對應目錄
$ rm .git/modules/xyz
// 移除實體目錄
$ rm xyz/

處理大量衝突檔案

在 Merge/cherry pick/Apply Stash 時偶爾會遇到一些已知要覆蓋的檔案,但是 git 因為版本記錄問題而判斷為有衝突狀態。這時可以用下方指令快速套用其中一方的檔案版本。

# 套用本地端檔案內容
$ git checkout --ours .

# 套用來源端檔案內容
$ git checkout --theirs .

# 套用完只有修改案內容而已,別忘了加到暫存區
$ git add .

修正 gitignore 路徑

可以重新指定 `core.excludesfile` 的路徑將 .gitignore 指向不同路徑避免修改到其他人的設定(實務上應該不太需要)。

// 目前專案
git config core.excludesfile .my_gitignore

// 全域設定
git config --global core.excludesfile ~/.my_gitignore

救援相關

使用 reflog 查詢本地過時狀態

$ git reflog
e5eb66c (HEAD -> dian_feat_master_QNEHBM00-458) HEAD@{0}: rebase (finish): returning to refs/heads/dian_feat_master
37d0a02 HEAD@{8}: rebase (squash): [feat][UI] Sync 1.1.0 feature
069d6e5 HEAD@{9}: rebase (start): checkout HEAD~2
2d5c632 HEAD@{10}: commit: [feat][UI] Sync 1.10 feature

回到指定狀態

$ git reset 2d5c632 --hard

查詢檔案記錄

對於已經刪除的檔案,因為不在檔案系統中,所以想透過 UI 介面查詢記錄相對不方便,可以直接用以下的指令直接針對檔案查詢

# 列出指定檔案的所有 commit
$ git log --p -- path/to/your/file

# 查詢檔案的刪除記錄
$ git log --pretty=short --diff-filter=D -- path/to/your/file

首圖來源 🔗

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Dian
Dian

Written by Dian

學習>吸收>實作>再學習

No responses yet

Write a response