這是本文件的舊版!


Git 命令列操作整理

0. 基本設定

  1. 設定將帳號與 Token 存寫到 store 上

    git config --global credential.helper store

    • 這會把帳密(或 Token)以明文的方式儲存在 ~/.git-credentials 中。請注意:這是明文儲存,不加密,僅適用於安全的開發環境。
  2. 設定 commit 的基本資訊

    git config --global user.name "你的名字"
    git config --global user.email "[email protected]"

    • 這會寫入到 ~/.gitconfig 中。

1. 讓本地端同步刪除遠端已不存在的分支

  1. 透過 git branch -a 可以看到有 remotes/origin/分支 Exp.

      main
    * master
      remotes/origin/main
      remotes/origin/master

  2. 如果在遠端已經遺除掉 main 分支, 要想讓本地端的 remotes/origin/main 可以同步消失, 可以使用 git fetch –prune 來達成

    > git fetch --prune
    From https://gitlab.cloudlab.iiidevops.org/local-templates/cloudlab2024-02
     - [deleted]         (none)     -> origin/main

2. 合併分支檔案衝突批次處理

  1. 假設由 develop 分支合併入 master 後一些檔案出現衝突, 希望完全依照 develop 的版本為主
  2. 在 repo 目錄執行

    git checkout --theirs .

  3. 然後執行:

    git add .

    git commit

3. 希望將 master 分支備份至 gemini, 然後將 develop 分支完全取代 master 分支的作法

Step A:備份現有的 master 分支
  1. bash# 切換到 master 分支

    git checkout master

  2. 創建 gemini 分支作為 master 的備份

    git checkout -b gemini

  3. 推送 gemini 分支到遠端

    git push origin gemini

Step B:用 develop 完全取代 master
  1. bash# 切換到 develop 分支

    git checkout develop

  2. 確保 develop 是最新的

    git pull origin develop

  3. 刪除本地的 master 分支

    git branch -D master

  4. 基於 develop 創建新的 master 分支

    git checkout -b master

  5. 強制推送新的 master 到遠端(會完全覆蓋原有的 master)

    git push origin master --force

4. 誤將某個目錄內入 git 管理, 之後加入 .gitignore 的作法

  • 以 .bmad-core 這目錄為例

    git ls-files | grep .bmad-core
    git rm -r --cached .bmad-core/
    git commit -m "Remove .bmad-core/ from tracking"
    git rm -r --cached .
    git add .
    git commit -m "Update .gitignore"
    git status

  • tech/git_cli.1755565520.txt.gz
  • 上一次變更: 2025/08/19 09:05
  • jonathan