Git是代码版本管理工具,没有Git你连项目代码都拉不下来。这节讲Git安装、基本配置和SSH密钥,配好SSH以后推拉代码就不用每次输密码了。
一、下载安装Git
1. 下载
官网:https://git-scm.com/downloads
自动识别系统,点"Click here to download"下载Windows版。
国内镜像(下载快):
- 淘宝镜像:https://registry.npmmirror.com/binary.html?path=git-for-windows/
- 华为镜像:https://mirrors.huaweicloud.com/git-for-windows/
2. 安装
双击.exe,一路下一步,注意几个关键选项:
Select Components(选择组件):
- ✅ Additional icons(创建桌面图标)
- ✅ Windows Explorer integration(右键菜单)
- 其他默认
Choosing the default editor(默认编辑器):
- 选
Use Vim或Use the Nano editor - 如果你用VS Code,可以选
Use Visual Studio Code
Adjusting the name of the initial branch(默认分支名):
- 建议选
Override the default branch name for new repositories→ 填main - 以前默认是
master,现在社区推荐用main
Adjusting your PATH environment:
- 选
Git from the command line and also from 3rd-party software(推荐) - 这样在cmd里也能用git命令
Choosing the SSH executable:
- 选
Use bundled OpenSSH(用Git自带的)
Choosing HTTPS transport backend:
- 选
Use the OpenSSL library
Configuring line ending conversions(换行符处理):
- 选
Checkout Windows-style, commit Unix-style line endings(推荐) - Windows和Linux换行符不一样,这个选项自动处理转换
Configuring the terminal emulator:
- 选
Use MinTTY(Git自带的终端,比cmd好用)
Choose the default behavior of git pull:
- 选
Default (fast-forward or merge)
Choose a credential helper:
- 选
Git Credential Manager(保存密码,不用每次输)
Extra options:
- ✅ Enable file system caching(文件缓存,速度快)
- ✅ Enable symbolic links(符号链接)
点Install,等安装完成。
3. 验证
打开cmd:
git --version
输出类似git version 2.43.0.windows.1就说明装好了。
二、Git基本配置
安装完第一件事:配置你的用户名和邮箱。
1. 配置用户名和邮箱
# 配置全局用户名
git config --global user.name "你的名字"
# 配置全局邮箱
git config --global user.email "你的邮箱@company.com"
这个名字和邮箱会写在每次提交记录里,别乱填。 用你的真名或花名,邮箱用公司邮箱。
2. 查看配置
# 查看所有配置
git config --list
# 查看用户名
git config user.name
# 查看邮箱
git config user.email
3. 其他实用配置
# 设置默认编辑器为VS Code(如果你用的话)
git config --global core.editor "code --wait"
# 设置中文文件名不乱码
git config --global core.quotepath false
# 设置默认分支名为main
git config --global init.defaultBranch main
# 设置长路径支持(Windows路径太长会报错)
git config --global core.longpaths true
# 设置http缓存大小(大仓库拉取更快)
git config --global http.postBuffer 524288000
三、生成SSH密钥
SSH密钥就是一对加密文件:公钥和私钥。你把公钥放到Git服务器上,私钥留在本地。推拉代码时Git自动验证,不用输账号密码。
1. 检查是否已有密钥
# 查看是否已有SSH密钥
ls -al ~/.ssh
如果看到id_rsa和id_rsa.pub(或id_ed25519和id_ed25519.pub),说明已经有了,可以跳过生成步骤。
2. 生成密钥
推荐用ed25519算法(更快更安全):
ssh-keygen -t ed25519 -C "你的邮箱@company.com"
如果系统不支持ed25519(老系统),用RSA:
ssh-keygen -t rsa -b 4096 -C "你的邮箱@company.com"
3. 设置密钥保存路径
提示Enter file in which to save the key时直接回车,默认保存到C:\Users\你的用户名\.ssh\目录。
4. 设置密码(可选)
提示Enter passphrase时:
- 直接回车:不设密码,免密使用
- 输入密码:更安全,但每次用密钥都要输密码
新人建议直接回车,不设密码,省事。
5. 查看生成的密钥
cat ~/.ssh/id_ed25519.pub
输出类似:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAxxxxxxxxxxxxxxxxxxxxxxxxx 你的邮箱@company.com
这串以ssh-ed25519开头的就是你的公钥,复制它。
6. 启动SSH Agent
# 启动SSH Agent服务(Windows需要管理员权限)
eval $(ssh-agent -s)
# 添加密钥
ssh-add ~/.ssh/id_ed25519
四、绑定Gitcode(或GitHub/Gitee)
以Gitcode为例(国内很多公司用),其他平台步骤类似。
Gitcode(Gitcode代码托管平台)
地址:https://gitcode.com/
步骤:
- 登录Gitcode
- 右上角头像 → 设置 → SSH公钥
- 粘贴你刚才复制的公钥
- 填个标题(比如"我的开发电脑")
- 点确认添加
GitHub
- 登录GitHub
- 右上角头像 → Settings → SSH and GPG keys
- 点
New SSH key - Title填个名字
- Key粘贴公钥
- 点
Add SSH key
Gitee(码云)
- 登录Gitee
- 右上角头像 → 设置 → SSH公钥
- 粘贴公钥
- 点确定
公司内部GitLab
- 登录公司GitLab
- 右上角头像 → Preferences → SSH Keys
- 粘贴公钥
- 点
Add key
验证SSH连接
# 测试Gitcode
ssh -T git@gitcode.com
# 测试GitHub
ssh -T git@github.com
# 测试Gitee
ssh -T git@gitee.com
如果输出类似Hi xxx! You've successfully authenticated或者Welcome to GitLab, @username!就说明SSH配好了。
如果提示Are you sure you want to continue connecting (yes/no)?,输入yes回车。
五、配置多个Git账号
如果你同时有公司GitLab和自己的GitHub,需要配置多账号。
1. 为不同账号生成不同密钥
# 公司账号
ssh-keygen -t ed25519 -C "company@company.com" -f ~/.ssh/id_ed25519_company
# 个人账号
ssh-keygen -t ed25519 -C "personal@gmail.com" -f ~/.ssh/id_ed25519_personal
2. 配置SSH Config
在~/.ssh/目录下创建config文件(无后缀):
# 公司GitLab
Host company-git
HostName git.company.com
User git
Port 22
IdentityFile ~/.ssh/id_ed25519_company
PreferredAuthentications publickey
# 个人GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
PreferredAuthentications publickey
3. 克隆时使用不同Host
# 克隆公司项目(用company-git替代原始域名)
git clone git@company-git:group/project.git
# 克隆GitHub项目
git clone git@github.com:username/repo.git
六、Git常用命令速查
# ===== 基础操作 =====
# 克隆项目
git clone git@github.com:user/repo.git
# 查看状态
git status
# 查看提交日志
git log --oneline --graph --all
# ===== 分支操作 =====
# 查看所有分支
git branch -a
# 创建并切换分支
git checkout -b feature/your-branch
# 切换分支
git checkout main
# 合并分支(先切到目标分支,再合并)
git checkout main
git merge feature/your-branch
# 删除本地分支
git branch -d feature/your-branch
# 删除远程分支
git push origin --delete feature/your-branch
# ===== 提交操作 =====
# 添加所有修改
git add .
# 添加指定文件
git add src/Main.java
# 提交
git commit -m "feat: 新增用户登录功能"
# 推送到远程
git push origin feature/your-branch
# 拉取远程更新
git pull origin main
# ===== 撤销操作 =====
# 撤销工作区修改
git checkout -- file.txt
# 撤销暂存区(已add未commit)
git reset HEAD file.txt
# 撤销最近一次提交(保留修改)
git reset --soft HEAD~1
# 撤销最近一次提交(丢弃修改)
git reset --hard HEAD~1
警告:git reset --hard会丢弃所有修改,用之前三思!
七、常见问题
问题1:Permission denied (publickey)
SSH密钥没配好。
排查:
- 确认公钥已添加到Git平台
- 确认私钥文件在
~/.ssh/目录 - 确认密钥权限正确:
看verbose日志,确认使用了正确的密钥Shell
# 查看密钥 ssh -vT git@github.com
问题2:fatal: not a git repository
你不在Git仓库目录里,或者当前目录没有.git文件夹。
# 检查当前目录
git status
# 如果不是仓库,需要初始化或克隆
git init # 初始化新仓库
问题3:git pull时冲突
# 拉取时发现冲突
git pull origin main
# 报错:CONFLICT (content): Merge conflict in src/Main.java
解决:
- 打开冲突文件,找到
<<<<<<<、=======、>>>>>>>标记 - 手动选择保留哪段代码,删掉冲突标记
git add .→git commit -m "merge: 解决冲突"→git push
问题4:git clone速度很慢
# 使用浅克隆(只拉最近一次提交,节省时间)
git clone --depth 1 git@github.com:user/repo.git
# 或者配置代理(如果你有VPN)
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy
问题5:SSL certificate problem
# 关闭SSL验证(不推荐,临时用)
git config --global http.sslVerify false
问题6:每次push都要输密码
没配SSH或者用了HTTPS方式。
检查clone地址:
- SSH方式:
git@github.com:user/repo.git(推荐,免密) - HTTPS方式:
https://github.com/user/repo.git(每次输密码)
如果已经是HTTPS,改成SSH:
git remote set-url origin git@github.com:user/repo.git
八、.gitignore文件
在项目根目录放一个.gitignore文件,告诉Git哪些文件不要提交。
Java项目常用.gitignore:
# IDE
.idea/
*.iml
*.ipr
*.iws
.vscode/
# 编译输出
target/
build/
out/
# 日志
*.log
logs/
# 系统文件
.DS_Store
Thumbs.db
# 配置文件(包含密码等敏感信息)
application-local.yml
application-prod.yml
# 临时文件
*.tmp
*.bak
*.swp
别把target/目录提交到Git里,那是编译生成的文件,每次build都不一样,提交了会导致冲突。
小结
| 步骤 | 关键点 |
|---|---|
| 安装 | 默认选项即可,注意PATH配置 |
| 基本配置 | 用户名和邮箱一定要对 |
| SSH密钥 | 一劳永逸,免密推拉代码 |
| 绑定平台 | 公钥放到Git服务器上 |
| .gitignore | 别提交编译文件和敏感配置 |
Git配好了,下一步做环境搭建收尾测试。