Git 是免費分散式版本控制系統,Ubuntu 下安裝配置 Git 並整合 GitHub 需完成:安裝套件、建立 SSH 金鑰、設定全域使用者資訊、驗證連線、初始化倉庫、提交與推送。本文提供完整步驟與指令範例。
sudo apt-get install git-core
# 驗證
git --version
# git version 1.7.9.5 (Ubuntu 12.04 預設)
git clone git://github.com/user/repo.git 即可,無需帳號。git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# 寫入 ~/.gitconfig,後續提交自動帶入
ssh-keygen -C 'you@example.com' -t rsa
# 一路 Enter,產生 ~/.ssh/id_rsa、id_rsa.pub
# 測試連線
ssh -v git@github.com
# 出現 "You've successfully authenticated..." 即成功
# 上傳公鑰:GitHub → Settings → SSH and GPG keys → New SSH key
# Key 內容:cat ~/.ssh/id_rsa.pub
# 建立倉庫
mkdir myproject && cd myproject
git init
# 出現 .git 目錄
# 新增檔案
git add . # 全部檔案
# 或 git add file1.c
# 提交
git commit -m 'initial commit'
# 關聯遠端 GitHub 倉庫
git remote add origin https://github.com/Git-Elite/CodeBase.git
# 或 SSH:git remote add origin git@github.com:Git-Elite/CodeBase.git
# 推送至 GitHub
git push -u origin master
# -u 設定上游追蹤,後續直接 git push
git remote remove origin
git remote add origin https://github.com/new/repo.git

| 操作 | 指令 |
|---|---|
| 查看狀態 | git status |
| 查看差異 | git diff |
| 查看日誌 | git log --oneline -10 |
| 建立分支 | git branch feature-x |
| 切換分支 | git checkout feature-x |
| 合併分支 | git merge feature-x |
| 拉取更新 | git pull origin master |
| 標籤 | git tag v1.0 |