Git

Local Repo Connect to a Remote Repository

First Pull & Push Note

Basic Git Workflow: Add, Commit, and Push

Create and Edit a .gitignore File

.gitignore content example

# .gitignore
*.tsv
*.geojson

Remove a File from Tracking (Keep It Locally)

Scenario: Already pushed a file to a GitHub repo but now want to remove it from version control - keeping it only on local maching and ignoring it in the future.

  1. git rm --cached {file_name}: Stop tracking the file(but keep it locally).
  2. echo "{file_name}" >> .gitignore: Add the file to .gitignore so it’s not tracked again.
  3. git add .gitignore: Stage the updated .gitignore file.
  4. git commit -m "{commit_message}"
  5. git push

Retrieving a File from an Older Git Commit (Without Overwriting Current Version)

  1. Check file history: git log -- Multinomial_naive_Bayes/main.py → find the commit hash where the file was modified (e.g. 050e8222).
  2. Extract old version: git show 050e8222:Multinomial_naive_Bayes/main.py > Multinomial_naive_Bayes/main_old.py → save the historical version as main_old.py without overwriting the current main.py.
  3. (Optional) Add and commit the retrieved file: git commit -m "Add historical version of main.py from commit 050e8222"

Git Branch Management

Force pull from a Git remote repository & discard all local changes

To force pull from a Git remote repository and discard all local changes, follow these steps carefully. This will overwrite your local files and history with the latest from the remote, so make sure you’re okay with losing any uncommitted local work.

Compare difference


🚨 WARNING:

This process will delete all local changes, including uncommitted and committed but not pushed changes.


✅ Steps to Force Pull and Recover Remote Repository:

git reset --hard
git clean -fd

2. Fetch latest remote data:

git fetch --all

SSH & Personal Access Token

1. 使用 HTTPS + Personal Access Token(建議)

如果你的 GitLab 是透過 HTTPS 存取(網址開頭為 https://),你需要在 clone 時提供:

🔧 步驟:

  1. 登入 GitLab 網站 → 點右上角頭像 → Edit ProfileAccess TokensPersonal Access Tokens
  2. 建立一組 Token,至少包含:
    • read_repository
    • write_repository(如果有 push 需求)
  3. 使用指令 clone:
    git clone https://gitlab.company.com/your_group/your_repo.git
    
  4. 系統會提示輸入帳號與密碼:
    • Username → 你的 GitLab 帳號
    • Password → 剛剛建立的 Token

💡 可將 Token 儲存在 Git Credential Manager 或 .netrc 中,避免每次輸入。


2. 使用 SSH 金鑰(最方便)

如果你公司允許使用 SSH,你可以設定 SSH 金鑰來避免每次輸入密碼。

🔧 步驟:

  1. 在本機產生 SSH 金鑰(如果還沒有):
    ssh-keygen -t ed25519 -C "[email protected]"
    

    預設儲存於 ~/.ssh/id_ed25519

  2. 查看公開金鑰內容:
    cat ~/.ssh/id_ed25519.pub
    
  3. 登入 GitLab,新增金鑰:
    • Edit ProfileSSH Keys → 貼上 id_ed25519.pub 的內容
  4. 使用 SSH 方式 clone:
    git clone [email protected]:your_group/your_repo.git
    

💡 第一次 clone 可能會提示確認主機指紋,輸入 yes 以信任該主機。

✅ 一、檢查現有 SSH 金鑰

打開終端機(Terminal)後,輸入以下指令:

ls ~/.ssh

常見的金鑰檔案名稱:

檔案名稱 說明
id_rsa 傳統 RSA 私鑰
id_rsa.pub 對應的公鑰
id_ed25519 較新、建議使用的 ED25519 私鑰
id_ed25519.pub 對應的公鑰
known_hosts 連過的主機指紋
config SSH 設定檔

✅ 二、查看特定公鑰內容(你要貼到 GitLab 的)

如果你看到像是 id_ed25519.pubid_rsa.pub,可以用以下指令查看內容:

cat ~/.ssh/id_ed25519.pub

cat ~/.ssh/id_rsa.pub

提示:輸出的內容是你要複製並貼到 GitLab SSH Keys 頁面的公鑰文字。

🧾 查看曾經連過的 SSH 主機列表

你可以透過查看 ~/.ssh/known_hosts 檔案來列出曾經使用 SSH 連線過的主機。

✅ 指令

cut -f1 -d' ' ~/.ssh/known_hosts | sed 's/,.*//'

🔍 指令說明

Note