將 JetBrains IDE(如 WebStorm、IntelliJ IDEA、PyCharm) 的專案推到 GitLab,並使用 SourceTree 來管理版本,是一個很常見的開發流程。
下面是完整步驟(適用 macOS / Windows):
✅ 目標流程
- ✅ 在 GitLab 建立一個新的 repository
- ✅ 在 JetBrains IDE 初始化 Git 並推送到 GitLab
- ✅ 在 SourceTree 加入該專案,視覺化管理版本控制
🧱 第一步:在 GitLab 建立新專案
- 登入 GitLab(https://gitlab.com 或你公司的 GitLab)
- 點選 New project
- 選擇:
- ✅ Project name(例如
my-cool-app) - ✅ 可見性(Private / Public)
- ✅ Project name(例如
- 建立完成後,GitLab 會顯示遠端 URL,例如:
https://gitlab.com/your-username/my-cool-app.git
或(若用 SSH):
[email protected]:your-username/my-cool-app.git
⚠️ 建議先確認你已在 GitLab 加入 SSH 金鑰,可免輸入密碼。
💻 第二步:在 JetBrains 專案中初始化 Git 並推送
✅ A. 若尚未建立 Git repo
- 開啟 JetBrains 專案
- 點選上方選單:
VCS → Enable Version Control Integration - 選擇:
Git - 在底部的 Terminal 執行:
git remote add origin https://gitlab.com/your-username/my-cool-app.git
git branch -M main
git add .
git commit -m "Initial commit"
git push -u origin main
✅ B. 若已經有 Git repo,但沒連接 GitLab
在 JetBrains Terminal 中執行:
git remote set-url origin https://gitlab.com/your-username/my-cool-app.git
git push -u origin main
🔧 第三步:在 SourceTree 加入專案
- 開啟 SourceTree
- 點選左上角「New Repository」→ 選擇「Add Working Copy」
- 填寫以下資訊:
- Working Copy Path: 選擇你的 JetBrains 專案資料夾
- Repository Type: Git
- Name: 任意
- 點選「Add」
✅ SourceTree 現在會顯示你的專案檔案、分支、commit 歷史等
📦 Optional:SourceTree 連 GitLab 個人帳戶(以 OAuth 或 SSH)
若你想讓 SourceTree 自動推送(不用每次打密碼):
SSH 金鑰建議步驟:
- 產生 SSH 金鑰(若尚未有):
ssh-keygen -t ed25519 -C "[email protected]"
- 將公鑰加入 GitLab:登入 GitLab →
Settings→SSH Keys→ 貼上~/.ssh/id_ed25519.pub - 在 SourceTree 的 Remote URL 改用 SSH:
[email protected]:your-username/my-cool-app.git
🧪 驗證你的整合是否成功
- 在 JetBrains 中做一次修改、Commit → Push
- 在 SourceTree 中會看到 Commit Log 與 Push 成功記錄
- 在 GitLab 網頁上也會同步顯示你的 Commit 訊息
✅ 總結快速流程
| 步驟 | 工具 | 操作 |
|---|---|---|
| 1 | GitLab | 建立 repo,取得 clone URL |
| 2 | JetBrains | 初始化 Git、設定 remote、commit + push |
| 3 | SourceTree | 匯入本地專案為 working copy,開始圖形化管理 |
| 4(選擇性) | GitLab + SSH | 加入 SSH 金鑰,避免輸入密碼 |