> 技术文档 > Linux 系统下 Git 的详细安装步骤和基础设置指南_linux 安装git

Linux 系统下 Git 的详细安装步骤和基础设置指南_linux 安装git



在这里插入图片描述


Linux 系统下 Git 的详细安装步骤和基础设置指南—目录

  • 一、安装 Git
    • 1. Debian/Ubuntu 系统
    • 2. CentOS/RHEL 系统
    • 3. Fedora 系统
    • 4. Arch/Manjaro 系统
    • 5. 其他方式:源码编译安装(适用于所有发行版)
  • 二、基础配置
    • 1. 设置全局用户名和邮箱
    • 2. 配置 SSH 密钥(用于 GitHub/GitLab 等)
    • 3. 配置 Git 别名(简化命令)
    • 4. 启用自动换行符转换(解决跨平台换行符问题)
  • 三、高级设置
    • 1. 配置差异工具(如 Meld)
    • 2. 配置 Git 代理(解决网络问题)
    • 3. 配置钩子(Hooks)自动化操作
  • 四、常见问题与解决方法
    • 1. 安装失败:`E: Unable to locate package git`
    • 2. 权限错误:`Permission denied (publickey)`
    • 3. Git 版本过旧
    • 4. 终端提示 `git: command not found`
  • 五、卸载 Git
    • 1. 通过包管理器卸载
    • 2. 手动卸载(源码安装)
  • 六、学习资源推荐

一、安装 Git

1. Debian/Ubuntu 系统

# 更新软件包列表sudo apt update# 安装 Gitsudo apt install git -y# 验证安装git --version

• 输出示例:git version 2.39.0


2. CentOS/RHEL 系统

# 启用 EPEL 仓库(若未启用)sudo yum install epel-release -y# 安装 Gitsudo yum install git -y# 或使用 dnf(CentOS 8+)sudo dnf install git -y# 验证安装git --version

3. Fedora 系统

# 使用 dnf 安装sudo dnf install git -y# 验证安装git --version

4. Arch/Manjaro 系统

# 使用 pacman 安装sudo pacman -Syu git -S# 验证安装git --version

5. 其他方式:源码编译安装(适用于所有发行版)

  1. 安装依赖:

    # Debian/Ubuntusudo apt install curl-devel expat-devel gettext-devel openssl-devel zlib-devel -y# CentOS/RHELsudo yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel -y
  2. 下载并解压 Git 源码:

    wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.39.0.tar.gztar -zxvf git-2.39.0.tar.gzcd git-2.39.0
  3. 编译并安装:

    make prefix=/usr/local allsudo make prefix=/usr/local install# 验证安装git --version

二、基础配置

1. 设置全局用户名和邮箱

git config --global user.name \"Your Name\"git config --global user.email \"your.email@example.com\"# 验证配置git config --global --list

2. 配置 SSH 密钥(用于 GitHub/GitLab 等)

  1. 生成 SSH 密钥:

    ssh-keygen -t ed25519 -C \"your.email@example.com\"

    • 按提示保存密钥到默认路径(~/.ssh/id_ed25519)。
    • 设置密钥密码(可选)。

  2. 将公钥添加到 GitHub/GitLab:
    • 复制公钥内容:

    cat ~/.ssh/id_ed25519.pub

    • 登录 GitHub → Settings → SSH and GPG Keys → 添加新 SSH Key。

  3. 测试 SSH 连接:

    ssh -T git@github.com

    • 成功提示:Hi username! You\'ve successfully authenticated.


3. 配置 Git 别名(简化命令)

git config --global alias.co checkoutgit config --global alias.br branchgit config --global alias.st statusgit config --global alias.lg \"log --oneline --graph --all\"

• 示例:git st 等同于 git status


4. 启用自动换行符转换(解决跨平台换行符问题)

git config --global core.autocrlf input # Linux/macOSgit config --global core.safecrlf warn # 检测混合换行符

三、高级设置

1. 配置差异工具(如 Meld)

  1. 安装 Meld:

    # Debian/Ubuntusudo apt install meld -y# CentOS/RHELsudo yum install meld -y
  2. 配置 Git 调用 Meld:

    git config --global merge.tool meldgit config --global mergetool.meld.path \"/usr/bin/meld\"

2. 配置 Git 代理(解决网络问题)

# HTTP/HTTPS 代理git config --global http.proxy http://127.0.0.1:7890git config --global https.proxy https://127.0.0.1:7890# SOCKS5 代理(如 Clash)git config --global http.proxy socks5://127.0.0.1:7890git config --global https.proxy socks5://127.0.0.1:7890# 取消代理git config --global --unset http.proxygit config --global --unset https.proxy

3. 配置钩子(Hooks)自动化操作

  1. 示例:在提交前运行代码检查
    • 进入仓库的 .git/hooks 目录:
    cd /path/to/repo/.git/hooks

    • 创建 pre-commit 文件:

    #!/bin/shecho \"Running code checks...\"npm test # 示例:运行测试

    • 赋予执行权限:

    chmod +x pre-commit

四、常见问题与解决方法

1. 安装失败:E: Unable to locate package git

• 解决:更新软件源并重试:

sudo apt update && sudo apt install git -y

2. 权限错误:Permission denied (publickey)

• 解决:

  1. 确认 SSH 密钥已添加到 ssh-agent
    eval \"$(ssh-agent -s)\"ssh-add ~/.ssh/id_ed25519
  2. 检查公钥是否正确添加到 GitHub/GitLab。

3. Git 版本过旧

• 升级 Git:

# Debian/Ubuntusudo add-apt-repository ppa:git-core/ppa -ysudo apt update && sudo apt upgrade git -y# Fedorasudo dnf upgrade git -y

4. 终端提示 git: command not found

• 解决:
• 检查是否已安装:which git
• 若未安装,通过上述方法重新安装。
• 确保 Git 路径在环境变量中(echo $PATH)。


五、卸载 Git

1. 通过包管理器卸载

• Debian/Ubuntu:

sudo apt remove git -y

• CentOS/RHEL:

sudo yum remove git -y

2. 手动卸载(源码安装)

sudo rm -rf /usr/local/bin/gitsudo rm -rf /usr/local/share/doc/git

六、学习资源推荐

  1. Pro Git 电子书(免费):
    https://git-scm.com/book/zh/v2
  2. GitHub 官方教程:
    https://guides.github.com/
  3. Git 命令速查表:
    https://education.github.com/git-cheat-sheet-education.pdf

通过以上步骤,您可以在 Linux 系统上快速安装并配置 Git,满足日常开发需求!


手机资讯动态