Git版本控制实用指南
Git配置基础
基础配置
1
2
3
|
# 设置用户信息
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
|
基础Git命令
仓库状态查看
1
2
3
4
5
|
# 查看工作区状态
git status
# 查看变更详情
git diff
|
分支管理
1
2
3
4
5
6
7
8
|
# 创建并切换分支
git checkout -b feature/new-branch
# 列出所有分支
git branch -a
# 删除分支
git branch -d branch-name
|
3. 提交管理
1
2
3
4
5
6
7
8
|
# 暂存更改
git add .
# 提交更改
git commit -m "描述性的提交信息"
# 修改最后一次提交
git commit --amend
|
4. 远程仓库操作
1
2
3
4
5
6
7
8
9
10
11
|
# 添加远程仓库
git remote add origin git@github.com:username/repo.git
# 推送到远程
git push -u origin master
# 从远程拉取
git pull origin master
# 查看当前仓库的远程URL
git remote -v
|
5. 撤销提交
1
2
3
4
5
6
7
8
|
# 撤销最近一次提交
git reset --soft HEAD^
# 撤销到指定的提交
git reset --soft <commit-hash>
# 回滚到特定提交(创建新的提交)
git revert <commit-hash>
|
Git记住密码
在使用Git时,频繁输入密码是一件麻烦的事情。Git提供了多种方式来记住凭证,避免重复输入。
永久保存凭证
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# 在Linux/Mac上永久保存凭证
## 方法1:使用store(明文存储,安全性较低)
git config --global credential.helper store
## 方法2:使用libsecret(更安全)
# Ubuntu/Debian
apt-get install libsecret-1-0 libsecret-1-dev
cd /usr/share/doc/git/contrib/credential/libsecret
sudo make
# 配置凭证助手
git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret
# 在Windows上永久保存凭证
## 使用Windows凭据管理器
git config --global credential.helper manager-core
|
查看和删除已保存的凭证
1
2
3
4
5
6
7
8
9
10
|
# 查看当前的凭证缓存配置
git config --get credential.helper
# 删除已保存的凭证
# Linux/Mac(使用store时)
rm ~/.git-credentials
# Windows
# 打开控制面板 -> 用户账户 -> 凭据管理器 -> Windows凭据
# 找到并删除与Git相关的凭据
|
GitHub新认证方式(替代账号密码)
从2021年8月13日起,GitHub不再支持使用账号密码直接进行Git操作认证,改为使用以下两种更安全的方式:
方式一:个人访问令牌(PAT)
个人访问令牌是替代密码的首选方式,适用于大多数Git操作。
创建个人访问令牌
- 登录GitHub,点击右上角头像 -> Settings
- 点击左侧 Developer settings -> Personal access tokens -> Tokens (classic)
- 点击 Generate new token -> Generate new token (classic)
- 输入令牌描述,选择有效期,勾选所需权限(至少需要 repo 权限进行代码操作)
- 点击 Generate token,复制生成的令牌并妥善保存(离开页面后将无法再次查看)
使用个人访问令牌
1
2
3
4
5
|
# 克隆仓库时使用令牌
git clone https://<YOUR_TOKEN>@github.com/username/repo.git
# 已存在的仓库,更新远程URL
git remote set-url origin https://<YOUR_TOKEN>@github.com/username/repo.git
|
方式二:SSH密钥认证
对于频繁的Git操作,SSH密钥是更推荐的方式,无需每次操作都输入令牌。
生成SSH密钥
关于SSH密钥的详细生成步骤和配置方法,请参考SSH密钥生成文章。
将SSH密钥添加到GitHub
- 复制公钥内容
- 登录GitHub,点击右上角头像 -> Settings
- 点击左侧 SSH and GPG keys -> New SSH key
- 粘贴公钥内容,添加描述,点击 Add SSH key
使用SSH URL
1
2
3
4
5
|
# 克隆仓库时使用SSH URL
git clone git@github.com:username/repo.git
# 已存在的仓库,更新远程URL
git remote set-url origin git@github.com:username/repo.git
|
常见问题解决方案
1. 版本回滚后同步远程仓库
当本地版本回滚后,需要同步远程仓库的最新状态时,可以使用以下命令:
1
2
3
4
5
6
7
8
|
# 获取所有远程分支的最新状态
git fetch --all
# 强制重置当前分支到远程master分支
git reset --hard origin/master
# 拉取最新更改
git pull
|
注意:使用--hard参数会丢失本地未提交的更改,请确保在执行前已备份重要更改。
2. 处理跨平台差异问题
换行符差异处理
在不同操作系统间协作时,经常会遇到换行符问题(CRLF vs LF):
1
2
3
4
5
6
|
# 配置全局换行符处理
git config --global core.whitespace cr-at-eol
# 推荐:配置自动转换
git config --global core.autocrlf input # 在Linux/Mac上
git config --global core.autocrlf true # 在Windows上
|
文件权限差异处理
在不同系统间切换时,文件权限可能导致不必要的差异:
1
2
3
4
5
|
# 忽略文件权限变化
git config core.filemode false
# 查看当前配置
git config --list | grep filemode
|