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 进行版本控制》第三版

第二部分 Git 基础知识

接下来的章节将重点介绍 Git 如何管理文件,解释索引的重要性及其与此过程的关系,并详细介绍提交对象和分支在 Git 仓库中扮演的重要角色。

在继续讨论之前,我们想解释一下为什么我们选择以这样的顺序来讨论这些主题。

"Git 内部:中,我们剖析了 Git 对象存储中的可用对象。 下面我们来看看这些对象是如何创建的,以及它们之间何时建立链接。

首先,我们将创建一个空目录,然后使用tree 命令检查目录内容:

   $ mkdir myrepo && cd myrepo

   $ git status
   fatal: not a git repository (or any of the parent directories): .git

   $ tree .git
   .git [error opening dir]

   0 directories, 0 files
   

由于这不是 Git 仓库,所以没有.git目录。接下来,我们将初始化一个空的 Git 仓库,并重新检查目录内容:

   $ git init -b main
   Initialized empty Git repository in /myrepo/.git/

   $ tree .git
   .git
   ├── HEAD
   ├── config
   ├── description
   ├── hooks
   │   ├── applypatch-msg.sample
   │   ├── commit-msg.sample
   │   ├── fsmonitor-watchman.sample
   │   ├── post-update.sample
   │   ├── pre-applypatch.sample
   │   ├── pre-commit.sample
   │   ├── pre-merge-commit.sample
   │   ├── pre-push.sample
   │   ├── pre-rebase.sample
   │   ├── pre-receive.sample
   │   ├── prepare-commit-msg.sample
   │   ├── push-to-checkout.sample
   │   └── update.sample
   ├── info
   │   └── exclude
   ├── objects
   │   ├── info
   │   └── pack
   └── refs
       ├── heads
       └── tags

   8 directories, 17 files
   

在这里,我们看到.Git文件夹已经创建;git 对象将存放在各自的文件夹中。

初始化时,我们创建了 一个名为main 的分支。在第三章中,我们将了解到分支只是指向提交对象的指针,具体来说,是指向当前分支的HEAD 或 tip commit 的指针。让我们检查一下树输出中HEAD目录的内容:

   $ cat .git/HEAD
   ref: refs/heads/main
   

它指向我们之前创建的分支名称。让我们进一步检查它的内容:

   $ cat .git/refs/heads/main
   cat: .git/refs/heads/main: No such file or directory
   

有趣的是,我们有一个分支,但内容却是空的。我们稍后再讨论这个问题。

现在,我们将创建一个文件并再次运行tree .git 命令:

   $ echo "Hello Git" > file
   $ tree .git .git ├── HEAD ├── config ├── description ├── hooks │ ├── applypatch-msg.sample ...
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