You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Git合并时考虑空格?求Ubuntu安装旧版Git步骤

Git合并时考虑空格的配置方法

全局配置(所有仓库生效)

执行以下命令,让Git在合并时严格处理空格差异,不自动忽略空格变化:

git config --global merge.whitespace error

如果需要更精细的空格规则控制,可设置多参数组合(比如同时检查行尾空格、CR换行符):

git config --global merge.whitespace trailing-space,cr-at-eol,error

参数说明:

  • trailing-space:标记行尾多余空格
  • cr-at-eol:标记以CR作为换行符的行
  • error:遇到空格相关冲突时抛出错误,终止合并

单次合并临时生效

如果仅需对某一次合并启用空格敏感处理,无需全局配置,可在合并时添加参数:

git merge -X whitespace=error <目标分支名称>

Ubuntu系统安装旧版本Git的具体步骤

1. 卸载现有Git(可选)

若系统已安装Git,先彻底移除当前版本:

sudo apt remove --purge git
sudo apt autoremove --purge
sudo apt clean

2. 通过PPA源安装旧版本

大多数旧版本可通过官方维护的PPA获取:

  1. 添加旧版本Git的PPA源:
sudo add-apt-repository ppa:git-core/oldstable
sudo apt update
  1. 查看可用的Git版本:
apt-cache policy git
  1. 安装指定版本(替换版本号为你需要的,比如1:2.25.1-1ubuntu3.1):
sudo apt install git=1:2.25.1-1ubuntu3.1

3. 源码编译安装(PPA无对应版本时)

如果PPA没有你需要的旧版本,可从源码编译:

  1. 安装编译依赖:
sudo apt install build-essential libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip
  1. 下载指定版本的源码包:
    前往Git官方标签页面找到对应版本(比如v2.20.0),下载tar.gz压缩包,然后解压并进入目录:
tar -xf v2.20.0.tar.gz
cd git-2.20.0
  1. 编译并安装:
make prefix=/usr/local all
sudo make prefix=/usr/local install
  1. 验证安装:
git --version

4. 锁定版本防止自动更新

安装完成后,为避免系统更新时自动升级Git,可锁定版本:

sudo apt-mark hold git

如需解锁版本,执行:

sudo apt-mark unhold git

内容的提问来源于stack exchange,提问作者GLEIPH GHIOTTO LIMA DE MENEZES

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 08:35:35