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

Git 目录详解和基本操作(1)

一.添加文件

1.添加操作

上回我们讲到,如果要对文件进行版本控制,需要将它添加到本地仓库。

执行这两条指令即可将filename文件添加至master分支。

git add filename

git commit -m "Modify Message"

我们用上回创建的ReadMe文件举例

wujiahao@VM-12-14-ubuntu:~/gitcode$ git add ReadMe
wujiahao@VM-12-14-ubuntu:~/gitcode$ git commit -m "add first word "
[master (root-commit) f5aa749] add first word1 file changed, 2 insertions(+)create mode 100644 ReadMe

接着我们可以看看提交记录

git log

wujiahao@VM-12-14-ubuntu:~/gitcode$ git log
commit f5aa7498c931ae9bc6cf0edfa400f7ceb28816a9 (HEAD -> master)
Author: wjh <2339023797@qq.com>
Date:   Thu Sep 11 19:49:16 2025 +0800add first word

这里有一些耐人寻味的信息,之后我们来一一剖析

2.另一个添加场景

我们来看一个场景:

wujiahao@VM-12-14-ubuntu:~/gitcode$ git add file4
wujiahao@VM-12-14-ubuntu:~/gitcode$ touch file5
wujiahao@VM-12-14-ubuntu:~/gitcode$ git commit -m "add some files"
[master 1881288] add some files1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 file4

为什么只有一个文件被更改,而不是我们预想的file4和file5两个文件?

很简单,因为file5并没有被add操作添加到暂存区,我们无法对他进行版本管理工作。

3. .git目录详解

由1.添加操作的日志我们可以看到,commit后跟了一串16进制数字,括号里还有一个指向master的HEAD指针。上篇文章我们谈到,当我们将工作区文件推送至暂存区乃至分支中,其实是把索引树进行了修改,真正的git对象在objects库中。我们打印出.git目录查看一下:

wujiahao@VM-12-14-ubuntu:~/gitcode$ tree .git
.git
├── branches
├── COMMIT_EDITMSG
├── config
├── description
├── HEAD
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── fsmonitor-watchman.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── pre-merge-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   ├── pre-receive.sample
│   ├── push-to-checkout.sample
│   └── update.sample
├── index
├── info
│   └── exclude
├── logs
│   ├── HEAD
│   └── refs
│       └── heads
│           └── master
├── objects
│   ├── 44
│   │   └── eee88f3839781bdc49019caa8f72a015a6ee8d
│   ├── a1
│   │   └── 8479ab13c25d12e250ea02dad4612d3f4c1850
│   ├── f5
│   │   └── aa7498c931ae9bc6cf0edfa400f7ceb28816a9
│   ├── info
│   └── pack
└── refs├── heads│   └── master└── tags

发现objects中确实多了很多内容,我们一一解释。

1.先来看HEAD指针。不管是上面的log日志,还是上篇讲解Git的原理,我们都知道HEAD是一个指向master分支的指针,那么它里面到底有什么呢?

cat

wujiahao@VM-12-14-ubuntu:~/gitcode$ cat .git/HEAD
ref: refs/heads/master

噢,HEAD又指向另一个目录,这个目录正是master。我们接着追查这个目录里有什么。

2.查看master目录。

wujiahao@VM-12-14-ubuntu:~/gitcode$ cat .git/refs/heads/master
f5aa7498c931ae9bc6cf0edfa400f7ceb28816a9

嗯?这一串数字是不是有点熟悉?

没错,这正是我们刚刚提交的ReadMe的commit ID,也是用来标识唯一文件的身份码

真相正在浮出水面。。

3.回看objects目录

我们再来看看objects的目录中有什么:

发现,f5开头的这一串文件,跟上面提到的ID是一个东西。这种种证据都表明了我们上篇文章Git初识讲到的git原理是正确的。既然在objects中找到了这个已经被管理的git对象,我们不妨再深入看看,这个对象是什么。

4.查看git对象

git cat-file -p 对象ID

wujiahao@VM-12-14-ubuntu:~/gitcode$ git cat-file -p f5aa7498c931ae9bc6cf0edfa400f7ceb28816a9
tree a18479ab13c25d12e250ea02dad4612d3f4c1850
author wjh <2339023797@qq.com> 1757591356 +0800
committer wjh <2339023797@qq.com> 1757591356 +0800add first word

这个对象又给了我们一个看不懂的东西tree,以及上传者信息。 不妨再看看tree里有什么。

5.查看tree

wujiahao@VM-12-14-ubuntu:~/gitcode$ git cat-file -p a18479ab13c25d12e250ea02dad4612d3f4c1850
100644 blob 44eee88f3839781bdc49019caa8f72a015a6ee8d	ReadMe

这样我们就看到了add的所有对象(这里只有一个ReadMe),根据blob后的id即可看到文件本身的内容

6.查看文件本身内容

这样我们就看到了原原本本的文件的内容。

wujiahao@VM-12-14-ubuntu:~/gitcode$ git cat-file -p 44eee88f3839781bdc49019caa8f72a015a6ee8d
hello Linux
hello git

4.修改操作

我们需要注意的是,git追踪的是修改,而并非文件。这里用修改ReadMe文件做例子:

vim filename

wujiahao@VM-12-14-ubuntu:~/gitcode$ vim ReadMe
wujiahao@VM-12-14-ubuntu:~/gitcode$ cat ReadMe
hello Linux
hello git
I made some changes

此时我们可以用下面这个指令帮助我们查看当前仓库的状态,暂存区是否有文件,有哪些文件发生了修改,但修改了什么内容我们是不得而知的

git status

wujiahao@VM-12-14-ubuntu:~/gitcode$ 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:   ReadMe

可以看到,我们当前的代码仓库中,暂存区没有文件,有一个文件(ReadMe)发生了修改。

用这个命令可以查看修改前后的内容差异

git diff filename

wujiahao@VM-12-14-ubuntu:~/gitcode$ git diff ReadMe
diff --git a/ReadMe b/ReadMe
index 44eee88..f195ccb 100644
--- a/ReadMe
+++ b/ReadMe
@@ -1,2 +1,3 @@hello Linuxhello git
+I made some changes

这样我们就基本明确了添加和修改操作时,git为我们做了哪些工作,并且验证了.git各目录的职能。更多内容,下期再见。


文章转载自:

http://cwraICqy.gpxbc.cn
http://IHMNJNFc.gpxbc.cn
http://V0DA2Fts.gpxbc.cn
http://M7TLlY8P.gpxbc.cn
http://f2vABBEC.gpxbc.cn
http://IKOvi6Y7.gpxbc.cn
http://XC1OwtOK.gpxbc.cn
http://2YGbBTJj.gpxbc.cn
http://XgSa1G7Z.gpxbc.cn
http://CUvhXprD.gpxbc.cn
http://L3ul9VHw.gpxbc.cn
http://8hLYohhr.gpxbc.cn
http://lcEPfNkq.gpxbc.cn
http://1g9D0Dwo.gpxbc.cn
http://3kW8yKsv.gpxbc.cn
http://khszAuEh.gpxbc.cn
http://ipwjXpLq.gpxbc.cn
http://erJQylaJ.gpxbc.cn
http://meHuE8r5.gpxbc.cn
http://MY8tql17.gpxbc.cn
http://1AtY9dag.gpxbc.cn
http://KuL2RmZA.gpxbc.cn
http://md4n9Pis.gpxbc.cn
http://HPYmnhQP.gpxbc.cn
http://qWKFt1y4.gpxbc.cn
http://w4Tpu54E.gpxbc.cn
http://12wU3R1P.gpxbc.cn
http://ZMCDo4Gu.gpxbc.cn
http://r37AK8of.gpxbc.cn
http://SZ0SrrV7.gpxbc.cn
http://www.dtcms.com/a/379061.html

相关文章:

  • 机器学习之K折交叉验证
  • Android Gradle Project (AGP) gradle-xxxx-src.zip无法正常下载问题解决方法
  • 图观 应用编辑器 产品介绍
  • 探讨Hyperband 等主要机器学习调优方法的机制和权衡
  • Apple产品发布会拆解:体验下放、设计极限、AI 入耳
  • 如何解决 Spring Bean 循环依赖
  • sdio NOT_AUTOGATING
  • 华为X考拉悠然 联合发布悠然智擎城市交通拥堵治理空间智能体方案
  • 《微服务事务管理》
  • CentOS 7 安装 MySQL 详细教程
  • 分布式锁解决集群下一人一单超卖问题
  • 如何解决pip安装报错ModuleNotFoundError: No module named ‘sweetviz’问题
  • @DateTimeFormat.fallbackPatterns 详解
  • 使用wavesurfer.js自定义波形绘制,集成频谱、时间轴、缩放、区域选择等插件
  • 数据库主从同步
  • leetcode27(两数之和)
  • Gradio全解11——Streaming:流式传输的视频应用(9)——使用FastRTC+Gemini创建沉浸式音频+视频的艺术评论家
  • 单片机 - I2C 总线
  • EasyExcel 实现国际化导入导出
  • 实现联邦学习客户端训练部分的示例
  • 从互联网医院系统源码到应用:智能医保购药平台的开发思路与实操经验
  • 伽马(gamma)变换记录
  • 第3节-使用表格数据-唯一约束
  • 深入浅出 C++20:新特性与实践
  • Java 面向对象三大核心思想:封装、继承与多态的深度解析
  • 蚁群算法详解:从蚂蚁觅食到优化利器
  • 星链计划 | 只赋能、不竞争!蓝卓“数智赋能·星链共生”重庆站沙龙成功举办
  • JavaScript 数组对象的属性、方法
  • vscode选择py解释器提示环境变量错误
  • 【2】标识符