前置準備
- 註冊 GitHub 帳號,創建倉庫(Repository),**不勾選** Initialize README / .gitignore / License(避免首次推送衝突)。
- 本地安裝 Git,配置用戶名/郵箱:`git config --global user.name "Your Name"`、`git config --global user.email "you@example.com"`。
方式一:命令行(通用、可控)
cd /path/to/your/AndroidProject
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/YourName/YourRepo.git
git push -u origin main
後續提交:
git add .
git commit -m "feat: add login feature"
git push
方式二:Android Studio 內建 VCS(圖形化)
- VCS → Enable Version Control Integration → Git。
- VCS → Import into Version Control → Share Project on GitHub → 登錄 GitHub → 填倉庫名/描述 → Share。
- 提交:右鍵項目 → Git → Commit Directory → 勾選文件 → Commit Message → Commit and Push。
.gitignore 關鍵內容(Android 專用)
# 編譯產物
/build/
/.gradle/
/.cxx/
*.apk
*.aar
*.ap_
# IDE 配置
.idea/
*.iml
local.properties
# 系統文件
.DS_Store
Thumbs.db
常見問題
- 推送被拒:遠程有本地無的提交 → `git pull --rebase` 後再 push。
- 大文件超限:GitHub 單文件 100MB,用 Git LFS 存大模型/視頻。
- 敏感信息洩露:誤提交 `google-services.json`、keystore → 用 `git filter-repo` 重寫歷史並撤銷遠程。