转速控制与转矩控制差异

Every project is made up of countless little changes. With a little luck, they will finally form a website, an app, or some other product. Your version control system keeps track of these changes. But only once you understand how to read them will you be able to track your project’s progress.

每个项目都是由无数的微小变化组成的。 幸运的是,他们最终将形成一个网站,一个应用程序或其他产品。 您的版本控制系统会跟踪这些更改。 但是只有了解了如何阅读它们之后,您才能跟踪项目的进度。

Using the example of Git, the popular version control system, this article will help you understand these changes.

本文以流行的版本控制系统Git为例,将帮助您了解这些更改。

差异代表变化 (Diffs Represent Changes)

In version control, the differences between one version and another are presented in what’s called a “diff” (or a “patch”). You’ll encounter diffs when working in technical surroundings like the command line, but also in graphical tools like “Tortoise Git” (Win) or “Tower” (Mac) and in code hosting platforms like “GitHub”. Diffs are the most frequently-used methods of visualizing changes.

在版本控制中,一个版本与另一个版本之间的差异以“差异”(或“补丁”)形式表示。 在命令行这样的技术环境中工作时,在“ Tortoise Git” (Win)或“ Tower” (Mac)等图形工具中以及在“ GitHub”之类的代码托管平台中工作时,都会遇到差异。 差异是可视化更改的最常用方法。

Let’s learn to read diffs with an example.

让我们通过一个例子来学习阅读差异。

An example diff

比较文件A / B (Compared Files A/B)

Our example diff compares two items with each other: item A and item B. In theory, A and B can be any file. In most cases, however, you’ll be wanting to compare the same file, but in two different versions.

我们的示例diff比较两个项目:项目A和项目B。理论上,项目A和项目B可以是任何文件。 但是,在大多数情况下,您将需要比较相同的文件,但是要比较两个不同的版本。

To set the scene, a diff output always begins by declaring which files are represented by “A” and “B”.

要设置场景,差异输出始终从声明“ A”和“ B”代表哪些文件开始。

文件元数据 (File Metadata)

Rather technical information, which you’ll rarely need, follows: the file metadata.

相反,您很少需要使用技术信息:文件元数据。

The first two strings represent the object hashes (or “IDs”) of our two files. In the background, Git not only saves versions of the project but also of each file item as an object. Hashes like the ones seen here are used to identify a file from a specific version.

前两个字符串表示我们两个文件的对象哈希(或“ ID”)。 在后台,Git不仅将项目的版本保存,而且还将每个文件项的版本保存为对象。 像这里看到的那样的散列用于从特定版本识别文件。

Finally, the number on the right is an internal file mode identifier; ‘100644’ is just a “normal file”,’100755’ specifies an executable file and ‘120000’ represents a symbolic link.

最后,右边的数字是内部文件模式标识符; “ 100644”只是一个“普通文件”,“ 100755”指定一个可执行文件,“ 120000”代表一个符号链接。

a / b的标记 (Markers for a/b)

A little later in the output, actual pieces of changes are displayed. To be able to identify which come from which version (A or B), each compared item is assigned a symbol: a minus (“-“) sign is used for version A, while a plus (“+”) sign is used for version B.

在输出的稍后部分,将显示实际的更改。 为了能够识别哪个版本(A或B),每个比较的项目都分配了一个符号:版本A使用减号(“-”),而使用加号(“ +”)版本B。

(Chunk)

Inspecting a 10,000 line file with only two modified lines would be a tedious process if the diff showed the entire contents of the file at once. Instead, only the portions that have actually been changed are shown. In Git, such a portion is called a “chunk” (or a “hunk”). The actual changed lines are surrounded by unchanged lines before and after the modification. These are called the “context”, because they help the user understand the context in which these modifications were made.

如果diff仅一次显示文件的全部内容,则仅检查两行修改的10,000行文件将是一个繁琐的过程。 而是仅显示实际已更改的部分。 在Git中,这样的部分称为“块”(或“块”)。 实际更改的线在修改前后被不变的线包围。 之所以称为“上下文”,是因为它们可以帮助用户理解进行这些修改的上下文。

块头 (Chunk Header)

A header prepends each of these chunks. The first part of the header informs you which lines were affected, enclosed in two “@” signs each. Let’s see which lines were modified in the first chunk of our example diff:

标头附加在每个这些块之前。 标题的第一部分告知您受影响的行,每行用两个“ @”符号括起来。 让我们看看在示例diff的第一块中修改了哪些行:

  • From file A (represented by a “-“), 6 lines are extracted, beginning from line no. 34

    从文件A(用“-”表示)中,从第1行开始提取6行。 34
  • From file B (represented by a “+”), 8 lines are displayed, also starting from line no. 34

    从文件B(以“ +”表示)中,显示了8行,也从行号开始。 34

After the closing pair of “@@”, another piece of context is given: Git tries to display a method name or other contextual information about where this chunk was taken from in the file. However, this differs depending on the programming language and doesn’t work in all scenarios.

在结束对的“ @@”之后,给出了另一个上下文:Git尝试显示方法名称或其他上下文信息,这些信息关于从文件中获取此块的位置。 但是,这取决于编程语言,因此并不适用于所有情况。

变化 (Changes)

To understand a change, you must be able to see both of its states – before and after the modification. This is why Git marks the lines that were actually changed with either a “+” or a “-” sign: a line that is prepended with a “-” sign comes from A, while a line with a “+” sign comes from B.

要了解更改,您必须能够看到更改的两个状态–修改前后。 这就是为什么Git用“ +”或“-”符号标记实际上已更改的行:带有“-”符号的行来自A,而带有“ +”符号的行来自A。 B.

Git kindly chooses “A” and “B” in such a way that (in most cases) you can think of A (or “-“) as “old” content and B (or “+”) as “new” content.

Git请选择“ A”和“ B”,以便(在大多数情况下)您可以将A(或“-”)视为“旧”内容,将B(或“ +”)视为“新”内容。

Our example helps us understand this:

我们的示例帮助我们理解了这一点:

Change #1 contains two lines prepended with a “+”. Because no counterpart in A existed for these lines (no lines with “-“), this means that the lines were added.

变更#1包含两行,前面带有“ +”。 因为在A行中没有对应的行(没有带有“-”的行),所以这意味着添加了这些行。

Change #2 is exactly the opposite: we have two lines marked with “-” signs in A. Since B doesn’t have an equivalent (no “+” lines), this means they were deleted.

更改#2完全相反:我们在A中有两行标有“-”的符号。由于B没有等效的行(没有“ +”行),这意味着它们已被删除。

Finally, some lines were actually modified: in Change #3, the two “-” lines were changed to look like the two “+” lines below.

最后,实际上修改了几行:在变更#3中,将两个“-”行更改为看起来像下面的两个“ +”行。

You now know how to read and understand a diff output. Now let’s look at how to generate some of this output in Git.

您现在知道了如何阅读和理解diff输出。 现在让我们看一下如何在Git中生成一些输出。

检查本地变化 (Inspecting Local Changes)

Before committing changes, you’ll want to see what exactly you did. The “git diff” command will show you all of your local changes that haven’t been added to Git’s ‘Staging Area’, yet: $ git diff

提交修改之前,你要清楚地看到你做了什么。 “ git diff”命令将向您显示尚未添加到Git的“ Staging Area”中的所有本地更改:$ git diff

Unstaged diffs

If instead you’d like to see only the changes that you’ve already staged, you can do so by adding the “–staged” parameter to the command.

相反,如果您只想查看已经进行的更改,则可以通过在命令中添加“ –staged”参数来实现。

检查所做的更改 (Inspecting Committed Changes)

Changes that have already been committed to the repository can be inspected with the “git log” command. By default, it only spits out an overview of the commit’s metadata (like author, message, and date). If you add the “-p” flag, you can also request detailed information about the exact changes:

可以使用“ git log”命令检查已提交到存储库的更改。 默认情况下,它仅吐出提交的元数据的概述(例如作者,消息和日期)。 如果添加“ -p”标志,则还可以请求有关确切更改的详细信息:

git log

比较分支和修订版 (Comparing Branches & Revisions)

Diffs are also useful when you want to understand how a certain branch (or even a specific commit) differs from another. For example, you might be interested in seeing all the changes from the “contact-form” branch that you don’t have in “master”, yet. To achieve this, use the “git diff” command as follows: $ git diff master..contact-form

当您想了解某个分支(甚至是一个特定的提交)与另一个分支之间的差异时,差异也很有用。 例如,您可能有兴趣查看尚未在“ master”中进行的“ contact-form”分支中的所有更改。 为此,请使用“ git diff”命令,如下所示:$ git diff master..contact-form

But you’re not limited to comparing branches. You can even compare two arbitrary commits with each other: $ git diff 0023cdd..fcd6199

但您不仅限于比较分支。 您甚至可以相互比较两个任意提交:$ git diff 0023cdd..fcd6199

清晰可视化的工具 (Tools for Clearer Visualization)

When looking at larger and more complicated modifications, you may need all the help you can get. A dedicated diff tool application will offer an improved visualization – and will thereby make understanding changes easier.

在查看更大,更复杂的修改时,您可能需要获得所有帮助。 专用的差异工具应用程序将提供改进的可视化效果-从而使理解更改更加容易。

To help you choose from the many available tools in this area, have a look at this overview of recommended apps.

为了帮助您从该区域的许多可用工具中进行选择,请查看此推荐应用程序概述

结论 (Conclusion)

I hope this article showed you that diffs (although they may suggest otherwise on first examination) are not just obscure ASCII art. Once you’ve learned how to read them, they allow you to understand how your project evolved. This makes them an essential tool for every member of a development team.

我希望本文向您显示,差异(尽管它们可能会在初次检查时提出其他建议)不只是模糊的ASCII艺术。 一旦您学会了如何阅读它们,它们就会使您了解您的项目如何发展。 这使它们成为开发团队中每个成员的必备工具。

翻译自: https://www.sitepoint.com/understanding-version-control-diffs/

转速控制与转矩控制差异

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐