Interpret a unified diff

📘Cisco DevNet Associate (200-901 DEVASC)


What is a Unified Diff?

A unified diff is a format used to show the differences between two files, usually a source code file or a configuration file. It’s a common output when using tools like Git, diff, or other version control systems.

  • The unified diff shows what changed, what was added, and what was removed.
  • It’s called “unified” because it combines context (unchanged lines) with changes in a single view. This makes it easier to understand the difference between file versions.

In IT, this is used for:

  • Reviewing changes in network configurations.
  • Comparing API scripts or automation scripts before deployment.
  • Code reviews in DevOps pipelines.

Anatomy of a Unified Diff

Here’s an example of a unified diff output:

@@ -2,7 +2,7 @@
interface GigabitEthernet0/1
- ip address 192.168.1.1 255.255.255.0
+ ip address 192.168.1.2 255.255.255.0
description Link to Core Switch

Let’s break this down step by step.


1. Hunk Header

@@ -2,7 +2,7 @@
  • @@ marks the start of a hunk (a block of changes).
  • -2,7 → The original file: starts at line 2 and spans 7 lines.
  • +2,7 → The new file: starts at line 2 and spans 7 lines.
  • Essentially, this tells you where the changes occur in both files.

Why important for IT?
It helps you locate exactly which part of a configuration or script was modified.


2. Context Lines

 interface GigabitEthernet0/1
description Link to Core Switch
  • Lines without + or - are context lines.
  • These lines did not change but are included to give context to the changes.
  • In network scripts, this could be interface definitions, routing statements, or firewall rules.

3. Removed Lines

- ip address 192.168.1.1 255.255.255.0
  • Lines that start with a minus (-) indicate that this line was removed from the original file.

Example: Old IP configuration was removed.


4. Added Lines

+ ip address 192.168.1.2 255.255.255.0
  • Lines that start with a plus (+) indicate that this line was added in the new file.

Example: IP was updated to a new value in the network configuration.


How to Read a Unified Diff Step by Step

  1. Look at the hunk header (@@ -2,7 +2,7 @@) to locate where the change is.
  2. Check removed lines (-) to see what was deleted.
  3. Check added lines (+) to see what was inserted.
  4. Look at context lines to understand how the changes fit in the overall file.

Real IT Use Cases

  1. Configuration Management
    • Compare old and new Cisco IOS configurations before applying updates.
    • Prevent mistakes like changing the wrong IP or interface settings.
  2. Automation Scripts
    • Review changes in Python scripts for API calls or Ansible playbooks.
    • Understand which modules or parameters were modified.
  3. Version Control
    • DevOps teams use Git diffs to review pull requests.
    • Unified diffs help detect syntax errors or misconfigurations before deployment.

Tips for the Exam

  1. Remember symbols:
    • - → line removed
    • + → line added
    • No symbol → unchanged/context
  2. Understand hunk headers:
    • @@ -a,b +c,d @@a=start line original, b=lines in original, c=start line new, d=lines in new.
  3. Focus on context:
    • Unified diffs always include some unchanged lines to give perspective.
    • Don’t ignore context—it’s part of interpreting changes correctly.
  4. Check multiple hunks:
    • A file can have multiple hunks, each showing changes in different parts of the file.

Summary Table

SymbolMeaningExample in IT Context
-RemovedOld IP address or firewall rule removed
+AddedNew IP address or firewall rule added
(space)ContextInterface name, VLAN, or route that did not change
@@Hunk headerShows line numbers of changes

Key Takeaway:
A unified diff is just a clear way to see what changed in a file. In IT environments, it’s used for reviewing network configs, automation scripts, or code changes. For the exam, make sure you can read the symbols, interpret the hunk headers, and understand the context of changes.

Buy Me a Coffee