Git 的常用命令与配置整理,涵盖日常开发中的基本操作。
基本配置
安装 Git 后,首先设置用户名和邮箱:
1 2
| git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
|
配置级别
Git 配置有三个级别:
| 级别 |
作用范围 |
配置文件 |
--local |
当前仓库(默认) |
.git/config |
--global |
当前用户所有仓库 |
~/.gitconfig |
--system |
系统所有用户 |
/etc/gitconfig |
优先级:local > global > system
其他常用配置:
1 2 3 4 5 6 7 8 9 10 11
| git config --global init.defaultBranch main
git config --global core.editor "code --wait"
git config --global core.autocrlf false
git config --list
|
基本工作流
初始化 / 克隆仓库
1 2 3 4 5 6
| git init
git clone https://github.com/user/repo.git git clone git@github.com:user/repo.git
|
日常提交流程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| git status
git diff
git add <file> git add .
git commit -m "commit message"
git push origin main
|
commit message 规范
推荐使用 Conventional Commits 规范:
1 2 3 4 5 6 7 8
| <type>(<scope>): <subject>
feat: 新功能 fix: 修复 bug docs: 文档更新 style: 代码格式调整 refactor: 重构 chore: 构建/工具变更
|
分支管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| git branch git branch -r git branch -a
git branch <branch-name>
git checkout <branch-name> git switch <branch-name>
git checkout -b <branch-name> git switch -c <branch-name>
git merge <branch-name>
git rebase <branch-name>
git branch -d <branch-name> git branch -D <branch-name>
git push origin --delete <branch-name>
|
merge vs rebase
- merge:保留完整的分支历史,会产生合并提交
- rebase:使提交历史更线性整洁,但会改写提交历史
gitGraph
commit id: "A"
commit id: "B"
branch feature
checkout feature
commit id: "C"
checkout main
commit id: "D"
checkout feature
commit id: "E"
查看历史
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| git log
git log --oneline
git log --oneline --graph --all
git log -p <file>
git show <commit-hash>
git blame <file>
|
撤销操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| git restore <file> git checkout -- <file>
git restore --staged <file> git reset HEAD <file>
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
git revert <commit-hash>
|
reset --hard 警告
git reset --hard 会永久丢失工作区和暂存区的未提交改动,使用前请确认。
暂存改动
当需要切换分支但当前改动又不想提交时,使用 stash:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| git stash
git stash push -m "描述信息"
git stash list
git stash apply
git stash pop
git stash drop
git stash clear
|
远程仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| git remote -v
git remote add origin <url> git remote add upstream <url>
git remote set-url origin <new-url>
git remote remove upstream
git fetch origin
git pull origin main
git pull --rebase origin main
|
标签
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| git tag v1.0.0
git tag -a v1.0.0 -m "Release v1.0.0"
git tag
git push origin v1.0.0 git push origin --tags
git tag -d v1.0.0 git push origin --delete v1.0.0
git checkout v1.0.0
|
.gitignore
在项目根目录创建 .gitignore 文件,指定不需要版本控制的文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| # 依赖目录 node_modules/
# 构建输出 public/ dist/
# 环境变量 .env .env.local
# 系统文件 .DS_Store Thumbs.db
# IDE 配置 .vscode/ .idea/
|
代理
先决条件
- 已有代理服务器(http/socks 其一)
- http:
http://proxy.address:8080
- socks:
socks5://proxy.address:1080
拉取 http(s)链接远程仓库地址
1 2 3 4 5 6 7 8 9 10 11
| git config --global http.proxy 'http://proxy.address:8080' git config --global https.proxy 'http://proxy.address:8080'
git config --global http.proxy 'socks5://proxy.address:1080' git config --global https.proxy 'socks5://proxy.address:1080'
git config --global --unset http.proxy git config --global --unset https.proxy
|
命令执行完成后,可以在$HOME/.gitconfig文件发现多了 2 条配置项目
1 2 3 4
| [http] proxy = socks5://proxy.address:1080 [https] proxy = socks5://proxy.address:1080
|
拉取 ssh 链接远程仓库地址
macOS/Linux: 修改~/.ssh/config,不存在则新建
1 2 3 4 5 6 7 8 9
| Host github.com HostName github.com User git # 走 HTTP 代理 ProxyCommand nc -X connect -x roxy.address:8080 %h %p # 走 socks5 代理(写法1) ProxyCommand nc -X 5 -x roxy.address:1080 %h %p # 走 socks5 代理(写法2) ProxyCommand nc -x roxy.address:1080 %h %p
|
解释
Host后面接的github.com是指定要走代理的仓库域名。
- 在
ProxyCommand中,Linux 和 macOS 用户用的是 nc。
-X选项后面接的是connect的意思是 HTTPS 代理。
- 如果
-X选项后面接的是数字 5,那么指的就是 socks5 代理。
- 当然直接不写上
-X选项也是可以的,因为在没有指定协议的情况下,默认是使用 socks5 代理的。
-x选项后面加上代理地址和端口号。
- 在调用
ProxyCommand时,%h和%p将会被自动替换为目标主机名和SSH 命令指定的端口
Windows: 修改%HOMEPATH%\.ssh\config,不存在则新建(connect.exe 已随 git 一同安装)
1 2 3 4 5 6
| Host github.com User git # 走 HTTP 代理 ProxyCommand connect.exe -H proxy.address:8080 %h %p # 走 socks 代理 ProxyCommand connect.exe -S proxy.address:1080 %h %p
|
解释
Host后面接的github.com是指定要走代理的仓库域名。
- 在
ProxyCommand中,Windows 用户用的是connect。
-H选项的意思是 HTTP 代理。
- 在调用
ProxyCommand时,%h和%p将会被自动替换为目标主机名和SSH 命令指定的端口
windows 使用需要使用用户名密码的 HTTP 代理服务情况下
1 2 3
| Host github.com User git ProxyCommand env CONNECT_USER=username CONNECT_PASSWORD=password connect.exe -H proxy.address:8080 %h %p
|