Git for Windows
是 Git
官方的 Windows
版本,它是基于 MSYS2-Mingw-w64
的,所以 Git for Windows
也是一个微型的 MSYS2
。
但是 Git for Windows
安装后却没有 MSYS2
的包管理器 pacman
,可以考虑手动安装。
网上大部分博客的描述都已经过时,有的需要去外网下载依赖。折腾之后记录一种简易的安装方式。
下载依赖
pacman
去到 MSYS2
官网下载 pacman
:
主页:https://packages.msys2.org/package/pacman
地址:https://mirror.msys2.org/msys/x86_64/pacman-6.1.0-4-x86_64.pkg.tar.zst
镜像:https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/x86_64/pacman-6.1.0-4-x86_64.pkg.tar.zst
依赖
在 pacman
页面介绍中发现需要许多依赖:
- base
- base-devel
- pacman-contrib
- pactoys
- pacutils
- pacutils-devel
- pkgfile
- python-pyalpm (make)
不过其中大部分的依赖都已经存在了,只需要补充两个必选的子依赖:
安装
将上面的三个软件包下载到本地,然后需要进行“安装”。
使用压缩软件打开上面的三个压缩软件包,将其中的文件夹解压到 Git for Windows
的安装目录下(比如我的安装目录是:C:\Program Files\Git\
)。
可以使用 7-Zip
或 Bandizip
等支持 zstd
压缩格式的软件。
正常情况完成操作后打开 Git bash
,可以运行 pacman -Sy
命令,并且会报错:
$ pacman -Sy
warning: Public keyring not found; have you run 'pacman-key --init'?
error: clangarm64: key "5F944B027F7FE2091985AA2EFA11531AA0AA7F57" is unknown
error: keyring is not writable
error: mingw32: key "5F944B027F7FE2091985AA2EFA11531AA0AA7F57" is unknown
error: keyring is not writable
error: mingw64: key "5F944B027F7FE2091985AA2EFA11531AA0AA7F57" is unknown
error: keyring is not writable
error: ucrt64: key "5F944B027F7FE2091985AA2EFA11531AA0AA7F57" is unknown
error: keyring is not writable
error: clang32: key "5F944B027F7FE2091985AA2EFA11531AA0AA7F57" is unknown
error: keyring is not writable
error: clang64: key "5F944B027F7FE2091985AA2EFA11531AA0AA7F57" is unknown
error: keyring is not writable
error: msys: key "5F944B027F7FE2091985AA2EFA11531AA0AA7F57" is unknown
error: keyring is not writable
error: database 'clangarm64' is not valid (invalid or corrupted database (PGP signature))
error: database 'mingw32' is not valid (invalid or corrupted database (PGP signature))
error: database 'mingw64' is not valid (invalid or corrupted database (PGP signature))
error: database 'ucrt64' is not valid (invalid or corrupted database (PGP signature))
error: database 'clang32' is not valid (invalid or corrupted database (PGP signature))
error: database 'clang64' is not valid (invalid or corrupted database (PGP signature))
error: database 'msys' is not valid (invalid or corrupted database (PGP signature))
初始化
错误提示提示密钥库没有初始化,需要初始化密钥库。
以管理员身份打开 Git bash
,运行以下命令完成密钥库初始化:
pacman-db-upgrad
$ pacman-db-upgrade
pacman-key --init
$ pacman-key --init
gpg: /etc/pacman.d/gnupg/trustdb.gpg: trustdb created
gpg: no ultimately trusted keys found
gpg: starting migration from earlier GnuPG versions
gpg: porting secret keys from '/etc/pacman.d/gnupg/secring.gpg' to gpg-agent
gpg: migration succeeded
/usr/bin/pacman-key: line 631: /usr/bin/gettext: No such file or directory
==>
gpg: Generating pacman keyring master key...
gpg: directory '/etc/pacman.d/gnupg/openpgp-revocs.d' created
gpg: revocation certificate stored as '/etc/pacman.d/gnupg/openpgp-revocs.d/ 4B00C615A704DD5CD68B86F457741B3C5937E708.rev'
gpg: Done
/usr/bin/pacman-key: line 631: /usr/bin/gettext: No such file or directory
==>
gpg: marginals needed: 3 completes needed: 1 trust model: pgp
gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u
pacman-key --populate msys2
$ pacman-key --populate msys2
/usr/bin/pacman-key: line 631: /usr/bin/gettext: No such file or directory
==>
/usr/bin/pacman-key: line 631: /usr/bin/gettext: No such file or directory
==>
-> Locally signed 5 keys.
/usr/bin/pacman-key: line 631: /usr/bin/gettext: No such file or directory
==>
gpg: setting ownertrust to 4
gpg: setting ownertrust to 4
gpg: setting ownertrust to 4
gpg: setting ownertrust to 4
gpg: setting ownertrust to 4
/usr/bin/pacman-key: line 631: /usr/bin/gettext: No such file or directory
==>
-> Disabled 4 keys.
/usr/bin/pacman-key: line 631: /usr/bin/gettext: No such file or directory
==>
gpg: marginals needed: 3 completes needed: 1 trust model: pgp
gpg: depth: 0 valid: 1 signed: 5 trust: 0-, 0q, 0n, 0m, 0f, 1u
gpg: depth: 1 valid: 5 signed: 7 trust: 0-, 0q, 0n, 5m, 0f, 0u
gpg: depth: 2 valid: 4 signed: 2 trust: 4-, 0q, 0n, 0m, 0f, 0u
gpg: next trustdb check due at 2024-10-07
然后就可以使用 pacman
命令了。
问题记录
权限问题
需要注意:pacman
命令会读写 Git for Windows
的安装目录,操作这个目录有可能是需要使用管理员权限的,所以需要以管理员身份打开 Git bash
。
这是没有给管理员权限时候的报错:
$ pacman -Syu
:: Synchronizing package databases...
error: failed to synchronize all databases (unable to lock database)
这是正常情况下的执行结果:
$ pacman -Syu
:: Synchronizing package databases...
clangarm64 is up to date
mingw32 is up to date
mingw64 is up to date
ucrt64 is up to date
clang32 is up to date
clang64 is up to date
msys is up to date
:: Starting core system upgrade...
there is nothing to do
:: Starting full system upgrade...
there is nothing to do
报错文件存在
由于 Git for Windows
自带的软件程序文件不是经过 pacman
安装的,所以后续安装软件的时候,尝尝会因为软件包覆盖安装报错文件已经存在:
$ echo "Y" | pacman -S bash
resolving dependencies...
looking for conflicting packages...
Packages (1) bash-5.2.026-1
Total Installed Size: 11.73 MiB
:: Proceed with installation? [Y/n] Y
(1/1) checking keys in keyring [#####################] 100%
(1/1) checking package integrity [#####################] 100%
(1/1) loading package files [#####################] 100%
(1/1) checking for file conflicts [#####################] 100%
error: failed to commit transaction (conflicting files)
bash: /usr/bin/bash.exe exists in filesystem
bash: /usr/bin/bashbug exists in filesystem
bash: /usr/bin/sh.exe exists in filesystem
Errors occurr
这个时候可以考虑强制覆盖安装,加上 --overwrite="*"
参数:
$ echo "Y" | pacman -S --overwrite="*" bash
warning: bash-5.2.026-1 is up to date -- reinstalling
resolving dependencies...
looking for conflicting packages...
Packages (1) bash-5.2.026-1
Total Installed Size: 11.73 MiB
Net Upgrade Size: 0.00 MiB
:: Proceed with installation? [Y/n] Y
(1/1) checking keys in keyring [#######################################] 100%
(1/1) checking package integrity [#######################################] 100%
(1/1) loading package files [#######################################] 100%
(1/1) checking for file conflicts [#######################################] 100%
(1/1) checking available disk space [#######################################] 100%
warning: could not get file information for usr/share/doc/
warning: could not get file information for usr/share/doc/bash/
warning: could not get file information for usr/share/doc/bash/bash.html
warning: could not get file information for usr/share/doc/bash/bashref.html
warning: could not get file information for usr/share/doc/bash/CHANGES
warning: could not get file information for usr/share/doc/bash/COMPAT
warning: could not get file information for usr/share/doc/bash/FAQ
warning: could not get file information for usr/share/doc/bash/INTRO
warning: could not get file information for usr/share/doc/bash/NEWS
warning: could not get file information for usr/share/doc/bash/POSIX
warning: could not get file information for usr/share/doc/bash/RBASH
warning: could not get file information for usr/share/doc/bash/README
warning: could not get file information for usr/share/info/
warning: could not get file information for usr/share/info/bash.info.gz
:: Processing package changes...
(1/1) reinstalling bash [#######################################] 100%
初始化系统
为了避免日后使用 pacman
冲突,可以考虑“重装”基本系统:
$ echo "Y" | pacman -S --overwrite="*" base
注意这条命令最后可能会一直报错,关闭窗口即可。
使用
1、安装软件包
pacman S package_name:安装指定的软件包。
pacman Sy:自动更新系统并安装所有可升级的软件包。
pacman Syu:自动更新系统、升级所有可升级的软件包以及安装新添加的软件源中的软件包。
2、删除软件包
pacman R package_name:删除指定的软件包及其配置文件。
pacman Rn package_name:仅删除指定的软件包,保留配置文件。
pacman Rsc:删除系统中不再需要的所有软件包及其配置文件。
3、搜索软件包
pacman Qs package_name:搜索系统中是否安装了指定的软件包。
pacman Ql package_name:列出系统中已安装的指定软件包的详细信息。
4、查询软件包信息
pacman Si package_name:显示指定软件包的详细信息,包括版本号、依赖关系等。
pacman Fl package_name:显示指定软件包的文件列表。
5、清理缓存和下载的软件包
pacman Sc:清理Pacman缓存,即清除本地数据库中的无效或过期的软件包信息。
pacman U download_directory/*.pkg.tar.xz:从指定的目录中安装下载的软件包。
6、查看系统信息
pacman Q:显示系统中已安装的所有软件包及其状态。
pacman Qe:显示系统中可用的所有扩展和内核模块。
pacman Qi:显示系统中所有已安装的软件包的详细信息。
7、其他常用命令
pacman H:显示帮助信息,列出所有可用的Pacman命令及其用法。
pacman T:检查系统中是否有未满足的软件包依赖关系。
pacman Sdd:删除系统中所有不再需要的软件包及其配置文件,同时进行磁盘空间清理。