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

第 7 章 分歧 差异

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

在介绍 Git 的diff 命令的强大功能之前,我们不妨先快速了解一下 Unix 和 Linuxdiff 工具的概念,因为git diff 命令与 Unix 和 Linux 中的同类工具比较文件的功能十分相似。diff是两个项目之间差异的简明摘要(因此被称为 "diff")。例如,给定两个文件,Unix 和 Linuxdiff 命令会逐行比较文件,并在 diff 中汇总偏差,如下例片段所示。更具体地说,偏差列出了将一个文件转换成另一个文件所需的更改。在示例中,initial 是某些散文的一个版本,而rewrite 是随后的修订版本。 -u 选项生成统一的 diff,这是一种广泛用于共享修改的标准化格式:

   $ cat initial
   Now is the time
   For all good men
   To come to the aid
   Of their country.

   $ cat rewrite
   Today is the time
   For all good men
   And women
   To come to the aid
   Of their country.

   $ diff -u initial rewrite
   --- initial     1867-01-02 11:22:33.000000000 -0500
   +++ rewrite     2000-01-02 11:23:45.000000000 -0500
   @@ -1,4 +1,5 @@
   -Now is the time
   +Today is the time
    For all good men
   +And women
    To come to the aid
    Of their country.

让我们详细看看差异。 在文件头中,原文件用 表示,新文件用 表示。 行提供了两个文件版本的行号上下文。要生成新文件,必须从原始文件中删除以减号 ( ) 为前缀的行。反之,带前导加号 ( ) 的行必须添加到原始文件中才能生成新文件。以空格开头的行在两个文件中都是一样的,由 选项作为上下文提供。- - - + + + @@+ -u

就差异本身而言,它并不提供更改的原因或理由,也不能证明初始或最终状态的合理性。然而,差值提供的不仅仅是文件差异的摘要。它提供了如何将一个文件转换为另一个文件的正式说明。(此外,diff 还可以扩展到显示多个文件和整个目录层次之间的差异。

Unix 和 Linuxdiff 命令可以计算在两个目录层次结构中发现的所有文件对之间的差异。 diff -r 命令串联遍历每个层次结构,按路径名(例如,原/src/main.c新/src/main.c)将文件分成两对,并总结每对文件之间的差异。使用diff -r -u 可以生成一组统一的差异,对两个层次结构进行比较。

Git 也有自己的 diff 工具,同样可以生成差异摘要。此外,与 Unix 的diff -r 一样,Git 也能遍历两个树对象,并生成差异表示。但git diff 也有自己的细微差别和强大功能,是为 Git 用户的特殊需求量身定制的。Git 默认使用 Eugene W. Myers 开发的Myers diff 算法作为比较算法。

在本章中,我们将介绍git diff 的一些基础知识及其特殊功能。你将学习如何使用 Git 显示工作目录中的编辑改动,以及项目历史中任意两次提交之间的任意改动。你将看到 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