diff

Utilize common version control operations with Git

📘Cisco DevNet Associate (200-901 DEVASC)


Git diff – Compare Changes

Git is a version control system. One of its most useful commands is git diff, which helps you see changes in files before committing them. It’s like looking at the differences between two versions of your code or configuration.

1. Purpose of git diff

  • git diff shows what has changed in your files.
  • It compares changes line by line between:
    1. Your working directory and the staging area
    2. The staging area and the last commit
    3. Two commits
    4. Branches

In IT terms, it’s extremely helpful when:

  • Reviewing configuration files for network devices (like Cisco routers) before applying changes.
  • Checking code differences in scripts (Python, Bash) to ensure nothing breaks.
  • Reviewing YAML or JSON files for automation pipelines in tools like Ansible or Terraform.

2. Basic git diff Usage

2.1 Compare Working Directory vs Staging Area

Command:

git diff
  • Shows changes you made but have not staged yet.
  • Example scenario:
    You updated a network automation script (deploy.yml) and want to see what changed before staging it.

Output example:

- interface GigabitEthernet0/1
+ interface GigabitEthernet0/2

This shows that a configuration line was modified.


2.2 Compare Staging Area vs Last Commit

Command:

git diff --staged

or

git diff --cached
  • Shows what changes are staged and ready to commit.
  • Example scenario:
    You staged a change to a YAML configuration for a CI/CD pipeline. You want to double-check what will actually be committed.

2.3 Compare Specific Files

Command:

git diff filename
  • Shows differences only in that file, not all files in the repo.
  • Example scenario:
    You modified app_config.json. Run git diff app_config.json to see only its changes.

3. Comparing Commits

You can also compare two commits to see how a project evolved over time.

Command:

git diff commit1 commit2
  • Replace commit1 and commit2 with commit hashes.
  • Example scenario:
    Compare the state of your automation scripts before and after a network deployment.

4. Comparing Branches

Command:

git diff branch1 branch2
  • Useful when merging code from different branches to see conflicts or differences.
  • Example scenario:
    Compare your feature-network-updates branch to main before merging.

5. Understanding the Output

Git diff output is structured:

- line removed
+ line added
  • Lines starting with - → removed from the previous version.
  • Lines starting with + → added in the new version.
  • Context lines show unchanged content so you can locate changes easily.

In IT environments, this helps in:

  • Reviewing configuration updates to avoid misconfigurations.
  • Checking Python scripts for automation errors before deployment.
  • Validating JSON/YAML files for CI/CD pipelines.

6. Key Exam Points

For the DevNet Associate exam, you need to:

  1. Know what git diff does → shows changes.
  2. Know the difference between:
    • git diff → working directory vs staging
    • git diff --staged → staged vs last commit
    • git diff commit1 commit2 → compare commits
    • git diff branch1 branch2 → compare branches
  3. Understand the output symbols (+ added, - removed).
  4. Know why it’s used in IT environments → checking code, configs, scripts before applying changes.

7. Tips for Students

  • Always run git diff before committing changes. It prevents mistakes in scripts or network configurations.
  • You don’t need to memorize every option, but staging vs unstaged vs commit comparison is critical.
  • Use it in combination with git status, which shows which files are changed, to make your workflow clear.

Summary Table for Exam

CommandComparesExam Tip
git diffWorking directory vs stagingCheck what is not yet staged
git diff --stagedStaging area vs last commitCheck staged changes before committing
git diff commit1 commit2Two commitsReview historical changes
git diff branch1 branch2Two branchesReview branch differences before merging
git diff filenameSpecific fileCheck changes in a particular file
Buy Me a Coffee