初始身份配置
安装好 Git 之后的第一件事是配置身份,提交时 Git 会用这个身份署名。
$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"
三个作用域
Git 配置有 三层作用域,优先级从高到低:
| 作用域 | 配置文件 | 影响范围 |
|---|---|---|
--local(默认) | .git/config | 仅当前仓库 |
--global | ~/.gitconfig | 当前用户所有仓库 |
--system | /etc/gitconfig | 整台机器所有用户 |
同一个配置项三个文件都设置时,local 覆盖 global,global 覆盖 system。
删除配置
$ git config --global --unset user.name
$ git config --global --unset user.email
--unset删除一个值;如果只是想修改,直接git config --global user.name "NewName"覆盖即可。
配置别名
把常用长命令缩写成一个字符,工作效率提升明显:
$ git config --global alias.st status
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.unstage 'reset HEAD --'
SSH 公钥认证(推荐)
SSH 是日常开发最常用的认证方式,一次配置长期免密。
生成密钥(2026 年推荐 ed25519)
# 推荐:ed25519 算法(更短、更快、更安全)
$ ssh-keygen -t ed25519 -C "your_email@example.com"
# 兼容旧系统:RSA 4096 位
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
按提示一路回车,会在 ~/.ssh/ 下生成两个文件:
id_ed25519(私钥,绝不能泄露)id_ed25519.pub(公钥,放在服务器)
把公钥放到 Git 服务器
把公钥内容追加到服务器的 authorized_keys 文件中:
- 如果使用普通用户
username,文件路径:/home/username/.ssh/authorized_keys - 如果使用
root用户,文件路径:~/.ssh/authorized_keys(即/root/.ssh/authorized_keys)
网上很多介绍没讲清楚这个路径问题,导致配置后还是要求输入密码。
服务器端开启 RSA 认证
修改服务器 /etc/ssh/sshd_config,确保以下三项开启:
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
改完重启 sshd:
$ sudo systemctl restart sshd
多账户管理(同一个机器多个 Git 身份)
通过 ~/.ssh/config 文件区分不同 Git 服务的密钥:
# 默认 GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github
# 公司 GitLab
Host git.company.com
HostName git.company.com
User git
IdentityFile ~/.ssh/id_ed25519_company
关键点:GitHub / GitLab / Gitee 等平台都强制要求用 SSH 协议的
git@xxx用户名,别名只影响本地如何找到私钥。
HTTP 凭证保存
不是所有场景都能用 SSH(CI/CD 临时拉取、某些内网仓库),HTTP 协议下可以让 Git 缓存凭证避免每次输入。
# Linux(明文存 ~/.git-credentials)
$ git config --global credential.helper store
# Mac(存到 Keychain,更安全)
$ git config --global credential.helper osxkeychain
# Windows(Git Credential Manager)
$ git config --global credential.helper manager
⚠️ 2026 年重要变更:GitHub 自 2022 年 8 月起不再支持账号密码推送代码,必须使用 Personal Access Token (PAT) 或 SSH。在 GitLab / Bitbucket 等平台,类似的限制也在收紧。
使用 store 时,第一次输入用户名 + PAT 后会写入 ~/.git-credentials,格式形如:
https://<username>:<token>@github.com
远程仓库的”接收”开关
用 git init --bare 在本地建一个裸仓作为远端,默认是拒绝客户端 push 到当前检出分支的。如果想允许 push,需要在裸仓的 .git/config(或者非裸仓的 .git/config)中加入:
[receive]
denyCurrentBranch = ignore
⚠️ 安全风险:开启后,客户端 push 不会自动同步到工作目录,多人协作时容易混乱。仅推荐个人 dev 环境或临时调试使用,生产环境必须配 webhook 或 post-receive 钩子。
首次推送新项目
最常见的起步流程:
# 1. 在远程仓库(Gitee / GitHub / 公司 GitLab)新建项目,得到地址
# 例如 https://gitee.com/yourname/my-project.git
# 2. 本地项目初始化(如果还没有仓库)
$ cd my-project
$ git init
$ git add .
$ git commit -m "feat: 初始化项目"
# 3. 关联远程
$ git remote add origin https://gitee.com/yourname/my-project.git
# 4. 首次推送(-u 建立追踪关系,以后 push 不用再指定分支)
$ git push -u origin master
如果远程默认分支是 main 而本地是 master,会推送失败。可以改:
$ git branch -M main # 把本地分支改名为 main
$ git push -u origin main # 推送到远程 main
或者 git push -u origin master:main(本地:远程格式)一次性建立映射。
下一篇:Git 知识系列(二):日常工作流,讲解 add / commit / status / diff / log / stash 这些每天都用的命令。