跳至正文
来两杯美式
返回

Git 知识系列(一):安装与基础配置

By 来两杯美式
发布于更新于

初始身份配置

安装好 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/ 下生成两个文件:

把公钥放到 Git 服务器

把公钥内容追加到服务器的 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 这些每天都用的命令。


分享这篇文章:
通过邮件分享这篇文章✓ 链接已复制
所属专题
Git
第 1 / 8 篇
查看系列全部文章
  1. 01.Git 知识系列(一):安装与基础配置
  2. 02.Git 知识系列(二):日常工作流
  3. 03.Git 知识系列(三):撤销与回退
  4. 04.Git 知识系列(四):分支管理与远程协作
  5. 05.Git 知识系列(五):标签、忽略与子模块
  6. 06.Git 知识系列(六):仓库维护与排错
  7. 07.Git 知识系列(七):Submodule 操作指南
  8. 08.Git 知识系列(八):多 Remote 双向同步

上一篇
Git 知识系列(二):日常工作流
下一篇
IDEA 多线程debug干预线程执行顺序