Git工具使用
-
GIT(分布式版本控制系统)
1.安装git
1 2 | [root@controller ~]# rpm -aq git git -1.8 . 3.1 -12 .el 7 _ 4 .x 86 _ 64 //可以看到该系统上已经安装了git |
如果未安装则运行以下命令即可:
1 | yum install -y git |
2.版本库创建
创建一个版本库非常简单,首先,选择一个合适的地方,创建一个空目录:
1 2 3 4 5 6 | [root@controller ~]# mkdir git_db [root@controller ~]# cd git_db/<br> [root@controller git_db]# git init Initialized empty Git repository in /root/git_db/.git/ [root@controller git_db]# ls -a . .. .git |
git init命令生成一个版本库,我们可以看到提示”Initialized empty Git repository“ 是说这是一个空的仓库,可以通过ls -a命令查看隐藏的目录。
接下来我们在当前目录(/root/git_db/)下编写一个文档,记住一定要到git init 生成版本库的目录下创建,不然git无法识别。
1 2 3 | [root@controller git_db]# cat first_file.txt hello git 这是第一个版本 |
下面将一个文件放到Git仓库只需要两步。
第一步:用命令git add
告诉Git,把文件添加到仓库:
1 2 3 | [root@controller git_db]# git add first_file.txt You have new mail in /var/spool/mail/root [root@controller git_db]# |
没有输入其他内容说明添加成功。
第二步:用命令git commit
告诉Git提交说明,把文件提交到仓库:(这一步是必须的)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [root@controller git_db]# git commit -m "this is my fisrt git file" *** Please tell me who you are. Run git config --global user.email "you@example.com" git config --global user.name "Your Name" to set your account's default identity. Omit --global to set the identity only in this repository. fatal: unable to auto-detect email address (got 'root@controller.(none)' ) You have new mail in /var/spool/mail/root |
我们可以看到提示出错无法提交,原因是没有设置名字和邮件地址Git仓库无法发送,我们需要做的是按照Run下提示的代码运行即可。
1 2 3 4 5 6 7 | [root@controller git_db]# git config --global user.email "xxxx@qq.com" [root@controller git_db]# git config --global user.name "alai" [root@controller git_db]# git commit -m "this is my fisrt git file" [master (root-commit) a 86 e 828 ] this is my fisrt git file 1 file changed, 2 insertions(+) create mode 100644 first_file.txt |
再次运行commit 命令可以看到已经成功上传Git仓库。
3.代码回滚设置
修改文档
1 2 3 | [root@controller git_db]# cat first_file.txt hello git 这是第二个版本 |
现在,运行git status
命令看看结果:
1 2 3 4 5 6 7 8 9 | [root@controller git_db]# git status # On branch master # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: first_file.txt # no changes added to commit (use "git add" and/or "git commit -a" ) |
虽然Git告诉我们first_file.txt被修改了,但是不清楚修改了什么内容。需要用git diff
这个命令看看:
1 2 3 4 5 6 7 8 9 | [root@controller git_db]# git diff first_file.txt diff --git a/first_file.txt b/first_file.txt index 680655 c.. 407 ee 40 100644 --- a/first_file.txt +++ b/first_file.txt @@ -1 , 2 + 1 , 2 @@ hello git -这是第一个版本 +这是第二个版本 |
输出中+号显示的就是修改或新增的内容,-号显示的就是去掉或被修改的内容
我们再次提交到Git仓库上:
1 2 3 4 5 6 7 8 9 | [root@controller git_db]# git add . //.表示当前目录下所有改变的文档 You have new mail in /var/spool/mail/root [root@controller git_db]# git commit -m "changes" [master 25 d 561 b] changes 1 file changed, 1 insertion(+), 1 deletion(-) [root@controller git_db]# git status # On branch master nothing to commit, working directory clean |
在Git中,我们用git log
命令查看历史记录:
1 2 3 4 5 6 7 8 9 10 11 12 | [root@controller git_db]# git log commit 25 d 561 b 0 a 53 a 764 acf 15 b 06 a 53 ffab 34598 d 455 e Author: alai < 1259525 @qq.com> Date: Sun Feb 11 16: 41: 56 2018 + 0800 changes commit a 86 e 82810819202830 f 618440 fbf 4 acaf 3114498 Author: alai < 1259525 @qq.com> Date: Sun Feb 11 16: 28: 56 2018 + 0800 this is my fisrt git file |
加上--pretty=oneline
参数简化输出:
1 2 3 | [root@controller git_db]# git log --pretty=oneline 25 d 561 b 0 a 53 a 764 acf 15 b 06 a 53 ffab 34598 d 455 e changes a 86 e 82810819202830 f 618440 fbf 4 acaf 3114498 this is my fisrt git file |
第一列是commit id
(版本号),第二列是commit。
开始回滚
首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD
表示当前版本,上一个版本就是HEAD^
,上上一个版本就是HEAD^^
,当然往上100个版本写100个^
比较容易数不过来,所以写成HEAD~100
。使用git reset --hard命令:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #查看当前版本为 25 d。。 [root@controller git_db]# git reset --hard HEAD HEAD is now at 25 d 561 b changes You have new mail in /var/spool/mail/root [root@controller git_db]# cat first_file.txt hello git 这是第二个版本 #退回上一个版本 [root@controller git_db]# git reset --hard HEAD^ HEAD is now at a 86 e 828 this is my fisrt git file [root@controller git_db]# cat first_file.txt hello git 这是第一个版本 |
这是我们又想回到最新的那个版本,你通过命令git log --pretty=oneline查看已经没有了,这时候该怎么办呢,翻日志找到该版本的commit id 命令如下:
1 2 3 4 5 | [root@controller git_db]# git reset --hard 25 d 561 //commit id不必写全 git会自动查询 HEAD is now at 25 d 561 b changes [root@controller git_db]# cat first_file.txt hello git 这是第二个版本 |
微信

支付宝

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类