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 diffshows what has changed in your files.- It compares changes line by line between:
- Your working directory and the staging area
- The staging area and the last commit
- Two commits
- 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 modifiedapp_config.json. Rungit diff app_config.jsonto 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
commit1andcommit2with 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 yourfeature-network-updatesbranch tomainbefore 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:
- Know what
git diffdoes → shows changes. - Know the difference between:
git diff→ working directory vs staginggit diff --staged→ staged vs last commitgit diff commit1 commit2→ compare commitsgit diff branch1 branch2→ compare branches
- Understand the output symbols (
+added,-removed). - Know why it’s used in IT environments → checking code, configs, scripts before applying changes.
7. Tips for Students
- Always run
git diffbefore 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
| Command | Compares | Exam Tip |
|---|---|---|
git diff | Working directory vs staging | Check what is not yet staged |
git diff --staged | Staging area vs last commit | Check staged changes before committing |
git diff commit1 commit2 | Two commits | Review historical changes |
git diff branch1 branch2 | Two branches | Review branch differences before merging |
git diff filename | Specific file | Check changes in a particular file |
