当前位置: 首页 > news >正文

Linux架构篇、第五章git2.49.0部署与使用

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/binsource /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
EOFsource ~/.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 masterNo commits yetUntracked files:(use "git add <file>..." to include in what will be committed)test.txtnothing 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 masterNo commits yetChanges to be committed:(use "git rm --cached <file>..." to unstage)new file:   test.txt

2.3.1配置用户邮箱与名称

#在全局层面配置 Git 的用户邮箱
git config --global user.email "laowang@laowang.com"#在全局层面设置 Git 的用户名称
git config --global user.name "laowang"#将暂存区的文件提交到本地仓库
git commit -m "add test file"
#输出如下: 
[master (root-commit) 910a5fd] add test file1 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
EOFgit add .
git commit -m "add test 1"#结果如下: 
[master 3bf977a] add test 11 file changed, 1 insertion(+)#再次添加2,3,4,5
cat>>/mygit/test.txt<<EOF
2
EOFgit add .
git commit -m "add test 2"#添加3如下:
cat>>/mygit/test.txt<<EOF
3
EOFgit add .
git commit -m "add test 3"#添加4如下:
cat>>/mygit/test.txt<<EOF
4
EOFgit add .
git commit -m "add test 4"#添加5如下:
cat>>/mygit/test.txt<<EOF
5
EOFgit add .
git commit -m "add test 5"

2.4查看log日志

git log#结果如下: 
commit 4b57522c59b2f5d2a4b65f641e00ba2caf35faa8 (HEAD -> master)
Author: laowang <laowang@laowang.com>
Date:   Mon May 5 22:15:41 2025 -0400add test 5commit 6abbc81761b389869fb83ba2d85757cde32bf157
Author: laowang <laowang@laowang.com>
Date:   Mon May 5 22:15:39 2025 -0400add test 4commit d261d070183799248aada48bc05f42e7fd49fddd
Author: laowang <laowang@laowang.com>
Date:   Mon May 5 22:15:39 2025 -0400add test 3commit 706a2758ad76698b159401b92adcb05b132f5a26
Author: laowang <laowang@laowang.com>
Date:   Mon May 5 22:15:39 2025 -0400add test 2commit 3bf977a733283a40c622c50a06e4bbdd9bccfb89
Author: laowang <laowang@laowang.com>
Date:   Mon May 5 22:12:08 2025 -0400add test 1commit 910a5fd6daf83c8b7b33f5bf0f69d446102900ef
Author: laowang <laowang@laowang.com>
Date:   Mon May 5 22:07:16 2025 -0400add 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.txtgit 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.txtno 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 test1 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 test11 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.txt1 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 test21 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.txtno changes added to commit (use "git add" and/or "git commit -a")

2.9.3恢复删除文件

#恢复 test2.txt 文件
git restore test2.txtgit 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

相关文章:

  • Unity3d 打包安卓平台(Android apk)报错Gradle build failed解决方法
  • 2. 盒模型/布局模块 - 响应式产品展示页_案例:电商产品网格布局
  • JavaScript编译原理
  • 数据结构(七)——图
  • Kingston FURY全新推出高性能PCIe 5.0 NVMe固态硬盘
  • AI+可视化:数据呈现的未来形态
  • 微信小程序 自定义图片分享-绘制数据图片以及信息文字
  • 位运算【入门-->精通】
  • Github 2025-05-13 Python开源项目日报 Top10
  • 我喜欢的vscode几个插件和主题
  • Datawhale 5月llm-universe 第1次笔记
  • 武汉大学无人机视角下的多目标指代理解新基准!RefDrone:无人机场景指代表达理解数据集
  • SpringBoot的外部化配置
  • 无人机避障——如何利用MinumSnap进行对速度、加速度进行优化的轨迹生成(附C++python代码)
  • API的学习总结(上)
  • 设计模式系列(03):设计原则(二):DIP、ISP、LoD
  • 记录算法笔记(2025.5.13)二叉树的最大深度
  • 【Qt】pro工程文件转CMakeLists文件
  • .NET8关于ORM的一次思考
  • MapReduce 入门实战:WordCount 程序
  • 经济日报整版聚焦:上海构建法治化营商环境,交出高分答卷
  • 俄官员说将适时宣布与乌克兰谈判代表
  • 这个“超强致癌细菌”,宝宝感染率高达40%,预防却很简单
  • 人才争夺战,二三线城市和一线城市拼什么?洛阳官方调研剖析
  • 多地警务新媒体整合:关停交警等系统账号,统一信息发布渠道
  • 西藏日喀则市拉孜县发生5.5级地震,震感明显部分人被晃醒