Skip to Content
《使用 Git 进行版本控制》第三版
book

《使用 Git 进行版本控制》第三版

by Prem Kumar Ponuthorai, Jon Loeliger
May 2025
Beginner to intermediate
548 pages
7h 40m
Chinese
O'Reilly Media, Inc.
Content preview from 《使用 Git 进行版本控制》第三版

第 5 章 文件管理和索引 文件管理和索引

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

当你在任何版本控制系统中工作时, ,你在工作目录中进行编辑,并将修改提交到版本库中保存。Git 也是这样工作的,但在工作目录和版本库之间插入了一个中间层--索引。索引又称暂存目录,用于暂存或收集对任何文件的修改,是提交前的最后一步。

索引可被视为工作目录当前状态的 缓存,在创建新提交、查询版本库状态以及在两个分支间执行合并操作时发挥着重要作用。(为了解释基本原理,我们将重点介绍索引目录对提交的重要性,这部分内容将在第 4 章中介绍。

在本章中,我们将解释 Git 的索引或暂存目录与其他版本控制系统相比有何独特之处。我们还将介绍如何管理索引和文件集,以及如何从版本库中添加和删除文件。我们将向你展示如何在 Git 中重命名文件,以及如何对索引的状态进行编目,最后还将介绍如何让 Git 忽略临时文件和其他不需要在版本控制中跟踪的无关文件。

指数的重要性

当你用 Git 管理代码时, ,你在工作目录中编辑,在索引中积累改动,然后将索引中积累的改动作为一个改动集提交。如果从撰写报纸文章的角度来考虑,这个概念就更有意义了。你准备一个草稿版本,你的编辑审核并提出修改意见,你采纳这些修改,最后文章发表。草稿代表你在 Git 中的工作目录,编辑的修改代表你将纳入索引目录的内容,而发表的文章就是最终提交。

从技术上讲,你可以把 Git 的索引看作是一系列预期的或潜在的修改。你可以添加、移除、移动或反复编辑文件,直到最终提交,将仓库中累积的修改变为现实。因此,大部分关键工作实际上都在提交步骤之前。图 5-1展示了这一概念。

vcg3 0501
图 5-1. 索引和文件操作概念

图 5-1 中,"本地历史记录 "块中的 main.o 版本是在结束对文件的编辑操作后,从索引/暂存目录中获取的文件最终版本。

"索引 "一文所述,索引是 Git 中最重要的 数据结构之一(参见Git 技术文档)。它是一个二进制文件,包含虚拟状态下文件路径名的排序列表、权限以及对应 blob 对象的 SHA1 ID。从本质上讲,索引包含虚拟树状态,代表着未来提交将引用的树对象。(Blob 和树对象将在第 2 章中讨论)。

添加、删除或编辑文件的操作通常 ,使用 Git 的瓷器(高级)命令,这些命令对你隐藏了索引的细节,让你的工作更轻松。例如,你可以随时使用git status 命令查询索引的状态。该命令明确指出 Git 认为哪些文件处于暂存状态。它通过比较虚拟树和工作树的状态,并将差异显示为输出结果。

你也可以通过 plumbing(底层)命令窥探 Git 的内部状态 ,比如git ls-files 。下面的代码段展示了使用该命令的索引输出示例:

   $ git ls-files -s
   100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0   README.md
   100644 6ff87c4664981e4397625791c8ea3bbb5f227e69 0   NewFile.md
   

换句话说,Git 的索引不包含任何文件内容,它只是跟踪你想提交的内容。当你运行git commit 时,Git 会检查索引,而不是你的工作目录,以发现要提交的内容。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

学习 GitHub Actions

学习 GitHub Actions

Brent Laster
《Kubernetes 最佳实践》第二版

《Kubernetes 最佳实践》第二版

Brendan Burns, Eddie Villalba, Dave Strebel, Lachlan Evenson
GraphQL 学习指南

GraphQL 学习指南

Eve Porcello, Alex Banks

Publisher Resources

ISBN: 9798341657922