Git 安装与配置
Git 的安装、初始化配置以及常用工具的设置。
安装
Windows
- 官网下载安装包:https://git-scm.com/download/win
- 推荐同时安装 Git Bash(类 Unix 终端)
- 安装时默认编辑器建议选 VSCode 或 Notepad++
# 验证安装
git --versionmacOS
# 方式一:Xcode Command Line Tools(自带)
xcode-select --install
# 方式二:Homebrew(推荐,版本更新)
brew install gitLinux
# Ubuntu / Debian
sudo apt-get update
sudo apt-get install git
# CentOS / RHEL
sudo yum install git
# 源码编译(获取最新版本)
# 从 https://git-scm.com/download/linux 下载源码初始配置
用户身份
# 必配项——每次提交都会记录这些信息
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
# 仅当前仓库配置(去掉 --global)
git config user.name "Project-Specific Name"常用配置
# 默认分支名(GitHub 推荐 main)
git config --global init.defaultBranch main
# 编辑器
git config --global core.editor "code --wait" # VSCode
git config --global core.editor "vim"
# 换行符处理(Windows 推荐)
git config --global core.autocrlf true # Windows
git config --global core.autocrlf input # macOS / Linux
# 大小写敏感(Windows 默认不敏感,建议开启)
git config --global core.ignorecase false
# 彩色输出
git config --global color.ui auto
# 拉取时默认使用 rebase 而非 merge
git config --global pull.rebase true常用别名(Aliases)
# 日志美化
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.lga "log --oneline --graph --decorate --all --date=short --format='%C(auto)%h %C(blue)%ad %C(green)%an %C(auto)%s'"
# 快捷操作
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage "reset HEAD --"
git config --global alias.last "log -1 HEAD"
# 查看别名列表
git config --global --list | grep alias代理设置
# HTTP 代理(公司网络 / 加速 GitHub)
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
# SSH 代理(~/.ssh/config)
# Host github.com
# ProxyCommand connect -H 127.0.0.1:7890 %h %p
# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy查看配置
# 查看所有配置
git config --list
git config --global --list
# 查看单个配置
git config user.name
# 配置文件位置
# 系统级:/etc/gitconfig
# 全局(用户):~/.gitconfig 或 ~/.config/git/config
# 仓库级:.git/configSSH 配置
# 生成密钥(推荐 ed25519 算法)
ssh-keygen -t ed25519 -C "your@email.com"
# 或 RSA(兼容旧系统)
ssh-keygen -t rsa -b 4096 -C "your@email.com"
# 启动 ssh-agent 并添加密钥
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# 测试连接
ssh -T git@github.com
ssh -T git@gitlab.com多账号 SSH 配置
# ~/.ssh/config
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519_gitlab# 使用不同 Host 别名来区分
git clone git@github-work:company/repo.git凭证管理
# 缓存密码(默认 15 分钟)
git config --global credential.helper cache
# 缓存 1 小时
git config --global credential.helper "cache --timeout=3600"
# 永久存储(不推荐——明文保存)
git config --global credential.helper store
# Windows:使用系统凭证管理器
git config --global credential.helper manager
# macOS:使用钥匙串
git config --global credential.helper osxkeychain图形化工具推荐
| 工具 | 平台 | 特点 |
|---|---|---|
| GitKraken | 全平台 | 美观,交互式 rebase 友好 |
| Sourcetree | Win/Mac | 免费,功能全面 |
| GitHub Desktop | Win/Mac | 简洁,GitHub 集成 |
| GitLens(VSCode 插件) | 全平台 | 行级 blame,直观 |
| lazygit | 全平台(终端) | 纯键盘操作,效率极高 |
| tig | 全平台(终端) | 轻量文本模式浏览器 |