Git

Git 安装配置

以下是 2025 年最新、最完整的 Git 安装与初始配置指南,适用于 Windows、macOS、Linux 三 大平台,一步步教你装好 Git 并配置得舒服好用。

1. 安装 Git(三种系统最新方法)

系统推荐安装方式命令/操作
Windows最推荐:Winget(Win10/11 自带)
次选:Git for Windows 官网
“`powershell
macOS最推荐:Homebrew(程序员必装)
次选:官方安装包
bash<br>brew install git
Ubuntu/Debianbash<br>sudo apt update && sudo apt install git -y
CentOS/RHEL/Rockybash<br>sudo dnf install git -y (或者 yum)
Arch Linuxbash<br>sudo pacman -S git
Fedorabash<br>sudo dnf install git

安装完成后,验证是否成功:

git --version
# 2025 年最新稳定版应该是 git version 2.46.x 或 2.47.x

2. 第一次使用必须做的全局配置(非常重要!)

打开终端(Windows 建议用 Git Bash、Windows Terminal 或 VS Code 终端),依次执行:

# 1. 设置你的名字(全局所有项目生效)
git config --global user.name "你的名字或昵称"
# 示例(中文也完全支持):
git config --global user.name "张三"

# 2. 设置你的邮箱(非常重要!尤其是用 GitHub/GitLab)
git config --global user.email "your_email@example.com"
# 示例:
git config --global user.email "zhangsan@gmail.com"

3. 强烈推荐的舒适化配置(提升 300% 体验)

# 3. 默认使用 main 作为初始分支(2020 年后 Git 官方默认就是 main)
git config --global init.defaultBranch main

# 4. 解决 Windows 下中文文件名乱码
git config --global core.quotepath false

# 5. 启用彩色输出(更好看)
git config --global color.ui auto

# 6. 设置默认编辑器(推荐 VS Code,也可以用 vim、notepad++ 等)
git config --global core.editor "code --wait"

# 7. 推荐的全局 .gitignore(忽略系统垃圾文件)
git config --global core.excludesfile ~/.gitignore_global

# 然后创建这个文件,内容可直接用我这份通用的:
curl -L https://raw.githubusercontent.com/github/gitignore/master/Global/Windows.gitignore -o ~/.gitignore_global
curl -L https://raw.githubusercontent.com/github/gitignore/master/Global/macOS.gitignore -o - >> ~/.gitignore_global
curl -L https://raw.githubusercontent.com/github/gitignore/master/Global/Linux.gitignore -o - >> ~/.gitignore_global

4. 配置 SSH 密钥(连接 GitHub、GitLab、Gitee 必备)

# 生成 ed25519 密钥(2025 年最推荐,最快最安全)
ssh-keygen -t ed25519 -C "your_email@example.com"

# 直接回车使用默认路径,一路回车(可设置密码也可不设)
# 生成的公钥在:~/.ssh/id_ed25519.pub

把公钥添加到托管平台:

# 复制公钥到剪贴板
# Windows
clip < ~/.ssh/id_ed25519.pub

# macOS
pbcopy < ~/.ssh/id_ed25519.pub

# Linux (需要安装 xclip)
xclip -sel clip < ~/.ssh/id_ed25519.pub

然后去:

  • GitHub → Settings → SSH and GPG keys → New SSH key
  • GitLab → Preferences → SSH Keys
  • Gitee → 设置 → SSH 公钥

粘贴进去就行。

测试是否成功:

ssh -T git@github.com
# 出现 Hi xxx! You've successfully authenticated... 就成功了

5. 常用别名(懒人必备,省手指)

git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit
git config --global alias.df diff
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.last "log -1 HEAD"
git config --global alias.unstage "reset HEAD --"

以后你就可以:

  • git stgit status
  • git lg → 超级好看的日志图
  • git cm -m "fix bug" → 提交

6. 最终检查配置是否生效

git config --global --list

看到你的 name、email、别名等都在,就大功告成!

现在你已经拥有一个现代化、高效、舒适的 Git 环境了,接下来就可以愉快地 clone、commit、push 了!

需要我给你一份「Git 日常最常用 20 条命令速查表」吗?只要回复「要」就发你。

分类: Git
文章已创建 2615

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部