Git知识点:源码安装

554 阅读2分钟

Git是一个高质量的开源项目,从源代码安装Git可以体验不同版本的功能。此外,通过编译、阅读源码可以学习Git许多重要的概念,了解其内部工作原理。

安装最新版本

# 安装依赖
$ sudo apt-get install dh-autoreconf libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev
$ sudo apt-get install asciidoc xmlto docbook2x
$ sudo apt-get install install-info
# 编译安装
$ git clone https://github.com/git/git.git
$ cd git
$ make configure # 通过autoconf生成./configure
$ ./configure --prefix=/usr/local/git/
$ make all doc info
$ sudo make install install-doc install-html install-info
$ /usr/local/git/bin/git --version
git version 2.45.2.561.g66ac6e4bcd

说明1:/usr/local/git/libexec/git-core目录下会生成很多git-<command>子命令,每个子命令其实都是git的软链接、硬链接或副本。

image.png

# 源码安装版本:硬链接(inode相同)
$ ls -li /usr/local/git/bin/git /usr/local/git/libexec/git-core/git /usr/local/git/libexec/git-core/git-add
1221540 -rwxr-xr-x 143 root root 24995536 Jun 21 09:18 /usr/local/git/bin/git
1221540 -rwxr-xr-x 143 root root 24995536 Jun 21 09:18 /usr/local/git/libexec/git-core/git
1221540 -rwxr-xr-x 143 root root 24995536 Jun 21 09:18 /usr/local/git/libexec/git-core/git-add
# 二进制安装版本:软链接
$ ls -li /usr/bin/git /usr/lib/git-core/git /usr/lib/git-core/git-add
929776 -rwxr-xr-x 1 root root 3121744 Jun 14 00:56 /usr/bin/git
929782 -rwxr-xr-x 1 root root 3121744 Jun 14 00:56 /usr/lib/git-core/git
936632 lrwxrwxrwx 1 root root       3 Jun 14 00:56 /usr/lib/git-core/git-add -> git
$ diff -s /usr/bin/git /usr/lib/git-core/git
Files /usr/bin/git and /usr/lib/git-core/git are identical

说明2:可以从Kernel.org网站GitHub网站获取对应版本的源码包。

$ wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.45.2.tar.gz
$ tar -zxvf git-2.45.2.tar.gz
$ cd git-2.45.2
$ make configure
$ ./configure --prefix=/usr/local/git-2.45.2/
$ make all doc info
$ sudo make install install-doc install-html install-info
$ /usr/local/git-2.45.2/bin/git --version
git version 2.45.2

安装初始版本

如果希望深入了解Git内部实现原理,Git官方文档推荐从初始提交开始阅读源码。 image.png

$ git clone https://github.com/git/git.git
$ cd git
$ git log --reverse --format=oneline | head -n 1
e83c5163316f89bfbde7d9ab23ca2e25604af290 Initial revision of "git", the information manager from hell
$ git checkout e83c5163316f89bfbde7d9ab23ca2e25604af290
$ tree --dirsfirst -h --sort=size --noreport
.
├── [ 23K]  read-cache.o
├── [ 21K]  update-cache.o
├── [8.2K]  README
├── [5.5K]  read-cache.c
├── [5.3K]  update-cache.c
├── [4.0K]  commit-tree.c
├── [2.4K]  cache.h
├── [2.0K]  show-diff.c
├── [1.4K]  write-tree.c
├── [1.2K]  init-db.c
├── [ 986]  read-tree.c
├── [ 957]  Makefile
└── [ 503]  cat-file.c
$ mkdir -p $HOME/bin
$ make install
$ ls $HOME/bin
cat-file  commit-tree  init-db  read-tree  show-diff  update-cache  write-tree

image.png 说明:为了正常编译运行代码,需要修改Makefile添加相关的依赖库。

$ git diff Makefile
diff --git a/Makefile b/Makefile
index a6bba79ba1..0b90a82638 100644
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,7 @@ all: $(PROG)
 install: $(PROG)
        install $(PROG) $(HOME)/bin/
 
-LIBS= -lssl
+LIBS= -lssl -lz -lcrypto
 
 init-db: init-db.o

参考资料

Git Source Code Mirror

Git installation

Git User Manual

Git - 安装 Git

源码解析:Git的第一个提交是什么样的

阅读 Git 代码,提升你的编程技能

Boost Your Programming Skills by Reading Git's Code

What Can We Learn from the Code in Git’s Initial Commit?