Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Ubuntu 安裝配置 Git 完整指南:從安裝、SSH 金鑰、GitHub 整合到基本操作流程

編輯:關於 Linux

Git 是免費分散式版本控制系統,Ubuntu 下安裝配置 Git 並整合 GitHub 需完成:安裝套件、建立 SSH 金鑰、設定全域使用者資訊、驗證連線、初始化倉庫、提交與推送。本文提供完整步驟與指令範例。

1. 安裝 Git

sudo apt-get install git-core
# 驗證
git --version
# git version 1.7.9.5 (Ubuntu 12.04 預設)

2. 申請 GitHub 帳號(可選,僅本地使用則不需)

  • 訪問 github.com 註冊。
  • 若僅克隆公開專案:git clone git://github.com/user/repo.git 即可,無需帳號。

3. 設定全域使用者資訊

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# 寫入 ~/.gitconfig,後續提交自動帶入

4. 建立 SSH 金鑰並上傳 GitHub

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

5. 基本 Git 工作流程

# 建立倉庫
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

6. 刪除/變更遠端來源

git remote remove origin
git remote add origin https://github.com/new/repo.git

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
Copyright © Windows教程網 All Rights Reserved