星期一, 6月 29, 2026

git-filter-repo 切分子專案小記

git-filter-repo 切分子專案小記



環境:

  • macOS 26.5.1
  • git 2.50.1


有的時候是這樣的, 專案會在測試目錄中發想, 然後慢慢測試, 慢慢就長成可以測試的小專案, 這個時候就會想把它獨立出來, 但是又想要保留當初開發的 git status.

今天要實作的是把 Git repo 裡面的子目錄切出去,變成一個獨立 repo,而且希望 Git history 可以一起帶過去。

我的原本 repo 在這裡:

/Users/max/Downloads/local_lab/lab_temp

要切出來的子目錄是這個:

02_實作中/20260627_chatbot_rag/


工具使用 `git-filter-repo`


Step 1: 先 clone 一份出來

重點是這裡要加 `--no-local`。如果只是從本機路徑直接 clone,`git-filter-repo` 可能會認為這不是乾淨的 fresh clone,然後拒絕改寫 history。

指令如下

cd /Users/max/Downloads/local_lab

git clone --no-local ./lab_temp chatbot_rag_split


  • 如果沒有加 `--no-local`,會遇到以下錯誤:

Aborting: Refusing to destructively overwrite repo history since
this does not look like a fresh clone.
  (expected freshly packed repo)
Note: when cloning local repositories, you need to pass --no-local to git clone to avoid this issue.


Step 2: 執行 git-filter-repo

接著只保留目標子目錄,並把它搬到新 repo 的根目錄:

cd chatbot_rag_split
git-filter-repo
\ --path '02_實作中/20260627_chatbot_rag/' \ --path-rename '02_實作中/20260627_chatbot_rag/':


這邊最容易看錯的是最後的冒號。`--path-rename` 的格式是 `OLD_PATH:NEW_PATH`,所以這行的意思是:

  • `OLD_PATH` 是 `02_實作中/20260627_chatbot_rag/`
  • `NEW_PATH` 是空字串
  • 結果就是把該子目錄底下的內容搬到 repo 根目錄

所以不是寫成 `:/`。Git repo 裡的 path 是相對路徑,不是檔案系統的 `/` 根目錄。要搬到 repo root,就是冒號後面留空。


成功輸出

最後成功時輸出長這樣:

NOTICE: Removing 'origin' remote; see 'Why is my origin removed?'
        in the manual if you want to push back there.
        (was /Users/max/Downloads/local_lab/./lab_temp)
Parsed 36 commits
New history written in 0.14 seconds; now repacking/cleaning...
Repacking your repo and cleaning out old unneeded objects
HEAD is now at 54dab99 build: 加入 Cloud Run 入口與資源保護
Enumerating objects: 296, done.
Counting objects: 100% (296/296), done.
Delta compression using up to 8 threads
Compressing objects: 100% (157/157), done.
Writing objects: 100% (296/296), done.
Total 296 (delta 136), reused 276 (delta 136), pack-reused 0 (from 0)
Completely finished after 0.25 seconds.

  • 這裡看到 `Removing 'origin' remote` 是正常的。`git-filter-repo` 會移除原本的 `origin`,避免不小心把改寫後的 history 推回原 repo。這個設計滿貼心的,is not really useful :)


檢查結果

跑完後可以先看一下目前內容、commit,以及 remote 狀態:

ls
git log --oneline --decorate -n 10
git status
git remote -v

  • 如果 `git remote -v` 沒有輸出,這次反而是正常狀態,因為 origin 已經被移除了。


Step 3: 接到新的 remote

確認內容沒問題後,再把它接到新的 GitLab 或 GitHub repo。

  • 建議新 repo 不要先初始化 README、`.gitignore` 或 license,避免第一推就要處理 unrelated history。

git remote add origin git@github.com:your-org/chatbot_rag.git

git push -u origin HEAD:main

  • 可用 `HEAD:main`,因為不用先管目前 local branch 叫 `main` 還是 `master`,直接把目前所在的 HEAD 推到遠端 `main`。


這次的完整版本

cd /Users/max/Downloads/local_lab

git clone --no-local ./lab_temp chatbot_rag_split

cd chatbot_rag_split

git-filter-repo \
  --path '02_實作中/20260627_chatbot_rag/' \
  --path-rename '02_實作中/20260627_chatbot_rag/':

git remote add origin git@github.com:your-org/chatbot_rag.git

git push -u origin HEAD:main

  • 這次的關鍵就是三個:本機 clone 要加 `--no-local`、實際指令用 `git-filter-repo`、`--path-rename` 最後的冒號要留下來。


又前進一步 ~ enjoy it

Reference

  • https://github.com/newren/git-filter-repo

星期日, 6月 14, 2026

Cline CLI 安裝與 Azure 部署小記

Cline CLI 安裝與 Azure 部署小記



環境:

  • macOS 14.5
  • Node.js v20.12.0
  • Cline CLI 1.0.0

有時候訂閱制的 AI 工具, 像是 Codex / Claude / Gemini 有時候額度不夠, 但是又不想要多訂閱, 保持彈性, 想法上就動到使用 Azure 上的模型來幫忙做事,嘗試幾個工具後, 決定來裝 Cline CLI,把 ChatGPT 5.4 部署上去測試。

Cline 官網 

  • https://github.com/cline/cline
  • 我有用過他的 vscode extension , 但是 extension 有些限制, 所以這次採取 CLI, 之後想要測試他的 Kanban


1. 在 Azure 部署 ChatGPT 5.4

這裡我們直接用 Azure CLI 來建立資源與部署模型,請確保已經使用 az login 登入。

先建立 Cognitive Services Account(假設 Resource Group cline-aoai-eastus2-rg 已經建好):

az cognitiveservices account create -n clineeastus2dbg1 -g cline-aoai-eastus2-rg --location eastus2 --kind OpenAI --sku S0

  • 這邊可以置換成 你的 Resource Group / Region 以及資源名稱


接著部署 gpt-5.4 模型,設定容量為 100K TPM,並且將 Deployment name 取為 gpt54-dev

az cognitiveservices account deployment create \
  -g cline-aoai-eastus2-rg \
  -n clineeastus2dbg1 \
  --deployment-name gpt54-dev \
  --model-name gpt-5.4 \
  --model-version 2026-03-05 \
  --model-format OpenAI \
  --sku-name GlobalStandard \
  --sku-capacity 100

  • 這邊比較重要的是 deployment-name , 因為 cline cli 識認你的 deployment name 不是模型名稱
  • sku-capacity 也很重要 這個如果設定太小就會遇到 Error: too many requests


拿好 Endpoint 跟 API Key:

# 取得 Endpoint (我們只需要前面的 Resource 網址,例如 https://clineeastus2dbg1.openai.azure.com/)
az cognitiveservices account show -n clineeastus2dbg1 -g cline-aoai-eastus2-rg --query properties.endpoint

# 取得 API Key
az cognitiveservices account keys list -n clineeastus2dbg1 -g cline-aoai-eastus2-rg --query key1


2. 安裝 Cline CLI

直接用 npm 全域安裝:

npm install -g cline-cli

裝完後測試一下版本:

cline --version


3. 設定 Cline CLI

Cline 實際讀取的設定檔在  ~/.cline/data/settings/providers.json

我們手動建立或編輯它,讓 Cline 透過 openai-compatible 介接 Azure:

{
  "version": 1,
  "lastUsedProvider": "openai-compatible",
  "providers": {
    "openai-compatible": {
      "settings": {
        "provider": "openai-compatible",
        "apiKey": "YOUR_AZURE_OPENAI_API_KEY",
        "model": "gpt54-dev",
        "baseUrl": "https://clineeastus2dbg1.openai.azure.com/openai/v1/"
      },
      "tokenSource": "manual"
    }
  }
}

  • 小提醒:baseUrl 後面固定接 /openai/v1/ 
  • model 要填你的 Azure Deployment Name,不是底層模型名稱喔。
  • api key 請填上自已的 key


設定完後確認一下目錄結構:

ls -al ~/.cline/data/settings/
total 8
drwxr-xr-x   3 user  staff    96 Jun 14 15:00 .
drwxr-xr-x+ 50 user  staff  1600 Jun 14 15:00 ..
-rw-r--r--   1 user  staff   200 Jun 14 15:05 providers.json

設定好後,進入互動模式看看:

cline -i

有吐出回應就大功告成啦。





又前進一步 ~ enjoy it

星期日, 6月 07, 2026

LINE 聊天紀錄備份自動化小記

LINE 聊天紀錄備份自動化小記



環境:

  • macOS
  • Google Apps Script (GAS)
  • Google Drive

跟朋友聊到一個需求

  • LINE 的對話備份有點難管理,想把一對一私訊丟到自己的 Google Drive 上,備份與管理。麻煩 AI 用 Google Apps Script 寫了一個小工具,自動把匯出的對話記錄整理好。

部署與設定

整個專案不需要伺服器,直接跑在 GAS 上,官網 https://script.google.com。部署步驟滿簡單的:

專案的相關程式碼與介紹我放在 Github


大概的步驟長這樣

# 1. 建立新專案,把 gas/ 裡的 Code.gs 等檔案貼上去
# 2. 手動執行初始化函數
installTrigger()
# 3. 部署成 Web App,存取權限選「只有我自己」


詳細部署步驟就請看 https://github.com/sakanamax/linechat-backup/blob/main/gas/README.md


專案核心的目錄結構大概長這樣:

gas/
├── Code.gs           # 後端邏輯(解析、整理、Drive 操作)
├── Index.html        # Web App 管理介面
├── appsscript.json   # GAS 設定權限與範圍
└── README.md         # 完整說明


日常使用流程

設定完以後,用起來就很無腦了。在 LINE 裡面點開對話的「傳送聊天記錄」,分享到 Google Drive 裡的「LINE待處理」資料夾。




接著背後的觸發器每小時會自動掃描,把上傳的 .txt 檔案抓出來解析,防重複處理後,再按月份或聯絡人分類歸檔到「LINE備份」資料夾裡。

整理完的結果看起來很舒壓:

📁 我的雲端硬碟
   📁 LINE備份
      📁 王花花
         📁 2026-06
            📄 對話記錄.txt


資料架構有設定兩種歸檔的邏輯

可以在管理介面上設定, 管理介面設定長這樣





雖然只能備份文字(圖片影片還是要手動存),而且目前解析器只吃 Android 格式,但至少對話紀錄穩穩地躺在完全私密的雲端裡,不用擔心不見,也不用依賴第三方工具。

感覺又向前一步 

~ enjoy it!


星期六, 6月 06, 2026

Claude Code 多帳號切換小記

Claude Code 多帳號切換小記




環境:

  • macOS 26.5
  • Claude Code: 2.1.153

使用 AI 工具時因為同時有個人帳號跟公司帳號,兩邊都要跑 Claude Code, 每次都要 /logout/login 其實很煩。 找了一下解法發現 Claude Code 有個環境變數 CLAUDE_CONFIG_DIR, 讓每個帳號可以有完全獨立的設定目錄,各自存憑證、記憶體、對話記錄, 完全不需要頻繁切換登入。

核心概念

預設情況下 Claude Code 的設定放在 ~/.claude/。 只要在啟動時給定不同的 CLAUDE_CONFIG_DIR,就能完全切開兩個帳號, 甚至同時開兩個終端機視窗,一個跑個人帳號、一個跑公司帳號,互不干擾。

每個獨立目錄裡面會隔離的東西:

  • credentials.json:帳號憑證
  • settings.json:Claude Code 設定
  • Session history:對話記錄
  • Memory:Project memory


macOS / Linux 設定方式

~/.zshrc(macOS)或 ~/.bashrc(Linux)加入:

# 現有帳號:沿用預設 ~/.claude/,什麼都不用動
alias claude-main='claude'

# 第二個帳號:指向新目錄
alias claude-team='CLAUDE_CONFIG_DIR="$HOME/.claude-team" claude'

名稱可以自己取,例如 claude-personal / claude-work,whatever。套用設定:

source ~/.zshrc


初始化第二個帳號(只做一次)

方法 A:瀏覽器 OAuth 登入(macOS 一般狀況)

CLAUDE_CONFIG_DIR="$HOME/.claude-team" claude
# 進入後執行 /login,依照指示完成驗證

方法 B:setup-token(WSL2 推薦,有效期整整一年,很適合公司帳號)

CLAUDE_CONFIG_DIR="$HOME/.claude-team" claude setup-token

WSL2 的瀏覽器登入跳轉有時候會炸掉,直接用 setup-token 比較省事。


日常使用

claude          # 原本用法,完全不變
claude-main     # 同上,明確標示是主帳號
claude-team     # 第二個帳號


兩個終端機視窗可以同時開,一邊跑個人帳號、一邊跑公司帳號,帳號完全獨立互不干擾。


Windows cmd.exe 的話

Windows 沒有 alias,要改用 bat 檔。先建個 bin 資料夾:

mkdir %USERPROFILE%\bin

建立 %USERPROFILE%\bin\claude-team.bat

@echo off
set CLAUDE_CONFIG_DIR=%USERPROFILE%\.claude-team
claude %*

建立 %USERPROFILE%\bin\claude-main.bat

@echo off
set CLAUDE_CONFIG_DIR=
claude %*

把 bin 加進 PATH(永久生效):

setx PATH "%USERPROFILE%\bin;%PATH%"

關掉重開 cmd 後生效。確認一下:

where claude-team
:: 應顯示 C:\Users\你的名字\bin\claude-team.bat


幾個注意事項

  • CLAUDE_CONFIG_DIR 在 macOS / Linux / Windows 都有效,macOS 雖然用 Keychain 存憑證,但不影響目錄切換
  • WSL2 瀏覽器登入若跳轉失敗,改用 claude setup-token
  • Token 過期後重新執行對應帳號的 setup-token 即可,不影響另一個帳號
  • 如果環境有設定 ANTHROPIC_API_KEY,它的優先權高於 OAuth,要先 unset ANTHROPIC_API_KEY 才行

設定完之後兩個帳號可以同時開著跑,再也不用反覆 logout / login


感覺又向前一步 ~ enjoy it