共计 2748 个字符,预计需要花费 7 分钟才能阅读完成。
本文整理一套 从零开始安装和配置 Git 的完整流程,包括 Git 安装、初始配置、创建 SSH Key、连接 GitHub、本地仓库与远程仓库绑定、常用 Git 命令以及常见问题解决。适用于 Linux / macOS / Windows 的基本开发环境。
相关平台:Git | GitHub
一、安装 Git
Linux(Debian / Ubuntu)
sudo apt update
sudo apt install git
安装完成后查看版本:
git --version
输出示例:
git version 2.43.0
macOS
安装 Homebrew 后执行:
brew install git
Windows
从官网下载安装:https://git-scm.com/downloads
安装完成后打开 Git Bash。
二、Git 初始配置
安装完成后需要配置用户信息,这会写入每次 commit 的作者信息。
设置用户名和邮箱
git config --global user.name "YourName"
git config --global user.email "your@email.com"
例如:
git config --global user.name "zeng zeng"
git config --global user.email "zeng7972@gmail.com"
查看当前配置
git config --list
示例输出:
user.name=YourName
user.email=your@email.com
init.defaultbranch=main
color.ui=auto
推荐配置
设置默认分支为 main:
git config --global init.defaultBranch main
开启彩色输出:
git config --global color.ui auto
设置 pull 策略:
git config --global pull.rebase false
三、创建 SSH Key(用于登录 GitHub)
使用 SSH 可以避免每次 push 输入密码。
生成 SSH Key
ssh-keygen -t ed25519 -C "your@email.com"
例如:
ssh-keygen -t ed25519 -C "zeng7972@gmail.com"
终端会提示:
Enter file in which to save the key
直接回车即可。
随后提示输入密码:
Enter passphrase
可以设置密码(更安全)或直接回车(更方便)。
生成的文件
生成完成后会得到两个文件:
| 文件 | 作用 |
|---|---|
~/.ssh/id_ed25519 |
私钥(不要泄露) |
~/.ssh/id_ed25519.pub |
公钥(上传到 GitHub) |
四、启动 SSH Agent
启动 SSH agent:
eval "$(ssh-agent -s)"
加载 SSH Key:
ssh-add ~/.ssh/id_ed25519
五、把 SSH Key 添加到 GitHub
查看公钥
cat ~/.ssh/id_ed25519.pub
复制输出内容。
进入 GitHub 设置
访问:https://github.com/settings/keys
点击 New SSH Key,填写:
| 字段 | 内容 |
|---|---|
| Title | 任意名称 |
| Key | 粘贴公钥 |
保存即可。
六、测试 SSH 连接
执行:
ssh -T git@github.com
如果看到:
Hi username! You've successfully authenticated
说明 SSH 登录成功。
如果 22 端口被阻断
有些网络环境无法连接 22 端口,可以改用 443 端口 SSH。
创建配置文件:
nano ~/.ssh/config
写入:
Host github.com
Hostname ssh.github.com
Port 443
User git
IdentityFile ~/.ssh/id_ed25519
AddKeysToAgent yes
再测试:
ssh -T git@github.com
即可正常连接。
七、创建 GitHub 仓库
在 GitHub 创建仓库:https://github.com/new
填写 Repository name,创建后 GitHub 会给出仓库地址:
git@github.com:username/project.git
八、本地创建 Git 仓库
进入项目目录:
cd project
初始化仓库:
git init
会生成 .git/ 目录,这是 Git 仓库的核心目录。
九、绑定远程仓库
添加远程仓库:
git remote add origin git@github.com:username/project.git
查看远程仓库:
git remote -v
示例输出:
origin git@github.com:user/project.git (fetch)
origin git@github.com:user/project.git (push)
十、提交代码
添加文件:
git add .
创建提交:
git commit -m "Initial commit"
十一、推送到 GitHub
第一次 push:
git push -u origin main
成功示例:
Enumerating objects
Counting objects
Writing objects
To github.com:user/project.git
Branch 'main' set up to track remote branch 'main'
以后只需要:
git push
十二、常用 Git 命令
| 操作 | 命令 |
|---|---|
| 查看仓库状态 | git status |
| 查看修改内容 | git diff |
| 添加所有修改 | git add . |
| 提交代码 | git commit -m "update" |
| 推送代码 | git push |
| 拉取远程代码 | git pull |
| 查看提交历史 | git log |
| 简洁模式查看历史 | git log --oneline |
| 查看当前分支 | git branch |
| 创建新分支 | git checkout -b new-feature |
| 切换分支 | git checkout main |
十三、恢复代码的方法
Git 的一个重要功能是可以恢复历史版本。
恢复某个文件
git checkout HEAD file.js
撤销所有未提交修改
git reset --hard HEAD
查看历史版本
git log --oneline
示例输出:
a3d91c2 add feature
8f21aa0 fix bug
c1a3920 initial commit
回退到历史版本
git reset --hard 8f21aa0
十四、Git 工作流程总结
Git 基本流程
修改代码
↓
git add
↓
git commit
↓
git push
开发过程中常见操作
git pull
git add .
git commit -m "update"
git push
总结
本文介绍了 Git 从安装到日常使用的完整流程:
- ✅ 安装 Git
- ✅ 配置用户信息
- ✅ 创建 SSH Key
- ✅ 绑定 GitHub
- ✅ 初始化本地仓库
- ✅ 连接远程仓库
- ✅ 提交并推送代码
- ✅ 常用 Git 命令
掌握这些基本操作后,就可以使用 Git 进行日常代码管理和版本控制。