Linux_架构篇
欢迎来到Linux的世界,看笔记好好学多敲多打,每个人都是大神!
题目:git2.49.0部署与使用
版本号 : 1.0,0
作者 : @老王要学习
日期 : 2025.05.13
适用环境: Centos7
文档说明
这份文档聚焦于在 CentOS 7 环境下部署和使用 Git 2.49.0,详细阐述了从环境准备到 Git 基础操作的全流程。环境准备部分明确了硬件和软件要求,确保服务器具备部署条件。安装 Git 环节提供了从下载安装包、安装依赖、源码编译到配置环境变量等一系列操作步骤。Git 基础操作涵盖了创建仓库、添加文件、提交更改、查看日志、版本回退、撤销修改、文件删除与恢复等常见操作,每个操作都配有具体的命令和预期输出,为用户提供了全面且详细的指导
环境准备
硬件要求
- 服务器: 2核CPU、2GB内存,20GB硬盘空间
- 网络: 确保服务器具有固定的IP地址,并且防火墙允许FTP端口(默认22端口)的通信
软件要求
- 操作系统:Centos7
- FTP软件:SecureCRT
- 软件包:git-2.49.0.tar.xz 与 git-manpages-2.49.0.tar.xz
一、安装git
1.1下载git2.49.0安装包并安装
#下载git2.49.0安装包
wget https://www.kernel.org/pub/software/scm/git/git-2.49.0.tar.xz
#安装依赖文件
yum -y install gcc make vim wget zlib-devel
#解压git包到/usr/local/src
tar xf git-2.49.0.tar.xz -C /usr/local/src
#进入git目录
cd /usr/local/src/git-2.49.0
#源码编译
./configure --prefix=/usr/local/git
#安装
make && make install
1.2查看版本如果有旧版本将其卸载
#查看git版本
git --version
#版本是1.8.3将其卸载
yum remove git
1.3写入环境变量
vim /etc/profile
#添加如下:
export PATH=$PATH:/usr/local/git/bin
source /etc/profile
1.4制作自动补全
#进入completion目录
cd /usr/local/src/git-2.49.0/contrib/completion
#拷贝文件并赋权
cp git-completion.bash ~/.git-completion.bash
chmod +x ~/.git-completion.bash
#写入环境变量
cat>>~/.bashrc<<EOF
if [ -f ~/.git-completion.bash ];then
. ~/.git-completion.bash
fi
EOF
source ~/.bashrc
#reboot虚拟机进行测试
reboot
#测试可以自动补全
git --version
#输出如下:
git version 2.49.0
1.5添加man手册
#下载man安装包
wget https://www.kernel.org/pub/software/scm/git/git-manpages-2.49.0.tar.xz
#解压man安装包
tar xf git-manpages-2.49.0.tar.xz -C /usr/local/src/
#拷贝man1、5、7到/usr/share/man下
cp /usr/local/src/man1/* /usr/share/man/man1
cp /usr/local/src/man5/* /usr/share/man/man5
cp /usr/local/src/man7/* /usr/share/man/man7
二Git基础
2.1创建目录写入文件
mkdir /mygit
cd /mygit
#写入数据
cat>/mygit/test.txt<<EOF
a
b
c
d
e
f
EOF
2.2创建Git仓库
#创建一个全新的 Git 仓库
git init
#结果如下:
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in /mygit/.git/
#查看当前暂存区的状态
git status
#结果如下: (你当前位于 `master` 分支,仓库里还没有任何提交记录,test.txt 是未被追踪的文件,也就是说,Git 尚未将其纳入版本控制体系)
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt
nothing added to commit but untracked files present (use "git add" to track)
2.3test.txt
文件添加到暂存区
#添加到暂存区
git add test.txt
#查看当前暂存区状态
git status
#结果如下: (你当前处于master分支,此仓库还没有任何提交记录,test.txt作为新文件已被添加到暂存区,等待你提交到本地仓库)
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test.txt
2.3.1配置用户邮箱与名称
#在全局层面配置 Git 的用户邮箱
git config --global user.email "[email protected]"
#在全局层面设置 Git 的用户名称
git config --global user.name "laowang"
#将暂存区的文件提交到本地仓库
git commit -m "add test file"
#输出如下:
[master (root-commit) 910a5fd] add test file
1 file changed, 6 insertions(+)
create mode 100644 test.txt
#查看当前暂存区状态
git status
#结果如下:
On branch master
nothing to commit, working tree clean
2.3.2逐步向文件追加内容并提交到 Git 仓库
cat>>/mygit/test.txt<<EOF
1
EOF
git add .
git commit -m "add test 1"
#结果如下:
[master 3bf977a] add test 1
1 file changed, 1 insertion(+)
#再次添加2,3,4,5
cat>>/mygit/test.txt<<EOF
2
EOF
git add .
git commit -m "add test 2"
#添加3如下:
cat>>/mygit/test.txt<<EOF
3
EOF
git add .
git commit -m "add test 3"
#添加4如下:
cat>>/mygit/test.txt<<EOF
4
EOF
git add .
git commit -m "add test 4"
#添加5如下:
cat>>/mygit/test.txt<<EOF
5
EOF
git add .
git commit -m "add test 5"
2.4查看log日志
git log
#结果如下:
commit 4b57522c59b2f5d2a4b65f641e00ba2caf35faa8 (HEAD -> master)
Author: laowang <[email protected]>
Date: Mon May 5 22:15:41 2025 -0400
add test 5
commit 6abbc81761b389869fb83ba2d85757cde32bf157
Author: laowang <[email protected]>
Date: Mon May 5 22:15:39 2025 -0400
add test 4
commit d261d070183799248aada48bc05f42e7fd49fddd
Author: laowang <[email protected]>
Date: Mon May 5 22:15:39 2025 -0400
add test 3
commit 706a2758ad76698b159401b92adcb05b132f5a26
Author: laowang <[email protected]>
Date: Mon May 5 22:15:39 2025 -0400
add test 2
commit 3bf977a733283a40c622c50a06e4bbdd9bccfb89
Author: laowang <[email protected]>
Date: Mon May 5 22:12:08 2025 -0400
add test 1
commit 910a5fd6daf83c8b7b33f5bf0f69d446102900ef
Author: laowang <[email protected]>
Date: Mon May 5 22:07:16 2025 -0400
add test file
#输出格式变为每行显示一个提交
git log --pretty=oneline
#结果如下:
4b57522c59b2f5d2a4b65f641e00ba2caf35faa8 (HEAD -> master) add test 5
6abbc81761b389869fb83ba2d85757cde32bf157 add test 4
d261d070183799248aada48bc05f42e7fd49fddd add test 3
706a2758ad76698b159401b92adcb05b132f5a26 add test 2
3bf977a733283a40c622c50a06e4bbdd9bccfb89 add test 1
910a5fd6daf83c8b7b33f5bf0f69d446102900ef add test file
2.5回退与重置
#回退到上一个提交
git reset --hard HEAD^
#结果如下:
HEAD is now at 6abbc81 add test 4
#强制重置到指定哈希值
git reset --hard 910a5fd6da
#结果如下:
HEAD is now at 910a5fd add test file
#查看文件内容变化
cat test.txt
a
b
c
d
e
f
#查看提交历史的 Git 命令(显示一行)
git log --pretty=oneline
910a5fd6daf83c8b7b33f5bf0f69d446102900ef (HEAD -> master) add test file
#查看引用日志(reflog)的详细历史记录
git log --reflog --pretty=oneline
#输出如下:
4b57522c59b2f5d2a4b65f641e00ba2caf35faa8 add test 5
706a2758ad76698b159401b92adcb05b132f5a26 add test 2
d261d070183799248aada48bc05f42e7fd49fddd add test 3
6abbc81761b389869fb83ba2d85757cde32bf157 add test 4
3bf977a733283a40c622c50a06e4bbdd9bccfb89 add test 1
910a5fd6daf83c8b7b33f5bf0f69d446102900ef (HEAD -> master) add test file
2.6add ?提交方式
# 追加如下内容
cat>>/mygit/test.txt<<EOF
6
7
EOF
#当前目录下所以文件,添加到缓存区(可称为索引)
git add .
# 提交缓存区所以文件
git commit -m "add ?"
#输出如下:
[master d9f350a] add ?
1 file changed, 2 insertions(+)
# 查看当前状态
git status
#输出如下:
On branch master
nothing to commit, working tree clean
2.7撤销写入缓存区文件
# 追加如下内容
cat >>/mygit/test.txt<<EOF
> my stuoid boss
> EOF
#写入缓存区
git add .
#撤销文件缓存状态
git restore --staged test.txt
#彻底撤销文件修改
git restore test.txt
git status
#输出如下:
On branch master
nothing to commit, working tree clean
# 查看文件数据(撤销成功)
cat test.txt
a
b
c
d
e
f
1
2
6
7
#查看最新的提交(撤销成功,没有提交)
git log --pretty=oneline
#输出如下:
d9f350a7936971176fde9d3e0eb0ebefdb469099 (HEAD -> master) add ?
706a2758ad76698b159401b92adcb05b132f5a26 add test 2
3bf977a733283a40c622c50a06e4bbdd9bccfb89 add test 1
910a5fd6daf83c8b7b33f5bf0f69d446102900ef add test file
2.7.1丢弃未提交的修改
# 在f后面添加空白字符
sed -i 's/f/f /' /mygit/test.txt
#查看文件显示正常(鼠标选择文件发现多出空白符)
cat test.txt
a
b
c
d
e
f
6
7
# 查看当前状态(因为有空白符,发生报错)
git status
#输出如下:
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
#丢弃工作区中未提交的修改
git restore test.txt
#再次查看状态(正常状态)
git status
On branch master
nothing to commit, working tree clean
2.7.2文件的回滚
#修改文件内容如下:
sed -i 's/a/asdfghj/' /mygit/test.txt
# 查看文件内容
cat test.txt
asdfghj
b
c
d
e
f
6
7
#写入缓存区
git add .
#查看状态
git status
#输出如下:
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: test.txt
#将暂存区中的更改提交到本地仓库
git commit -m "modify test"
#输出如下:
[master 142caf9] modify test
1 file changed, 1 insertion(+), 1 deletion(-)
git status
# 回滚上一个版本
git reset --hard HEAD^
#输出如下:
HEAD is now at 5cfad3e add ?
# 查看文件内容
cat test.txt
a
b
c
d
e
f
6
7
2.8删除彻底删除
2.8.1添加文件test1
# 添加一个test1.txt文件
cat>>/mygit/test1.txt<<EOF
aaaaa
bbbbb
ccccc
ddddd
eeeee
adffaf
afaaga
EOF
#写入缓存区
git add .
#提交缓存区内容
git commit -m "add test1"
#结果如下:
[master eccf14a] add test1
1 file changed, 7 insertions(+)
create mode 100644 test1.txt
#查看状态
git status
#结果如下:
On branch master
nothing to commit, working tree clean
2.8.2删除与彻底删除
# 删除文件
rm -f test1.txt
# 写入缓存区
git add .
# 查看状态
git status
#结果如下:
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: test1.txt
# 彻底删除文化
git commit -m "remove test1.txt"
#输出如下:
[master a2c2416] remove test1.txt
1 file changed, 7 deletions(-)
delete mode 100644 test1.txt
# 查看状态
git status
#输出如下:
On branch master
nothing to commit, working tree clean
2.9删除再恢复
2.9.1创建文件test2
# 添加文件 test2
cat>>/mygit/test2.txt<<EOF
111
222
33
44
55
aaaf
sff
EOF
# 写入缓存区
git add .
# 提交缓存区
git commit -m "add test2"
#输出如下:
[master 9116daf] add test2
1 file changed, 7 insertions(+)
create mode 100644 test2.txt
2.9.2删除test2文件
rm -f test2.txt
git status
#结果如下:
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: test2.txt
no changes added to commit (use "git add" and/or "git commit -a")
2.9.3恢复删除文件
#恢复 test2.txt 文件
git restore test2.txt
git status
#结果如下:
On branch master
nothing to commit, working tree clean
#查看文件目录
ll
#输出如下:
total 8
-rw-r--r-- 1 root root 26 May 6 00:25 test2.txt
-rw-r--r-- 1 root root 16 May 5 23:58 test.txt