Version Control
Git
Code Changes
Software Development
Commit Comparison

See diff between current state and last commit

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

In the world of version control, particularly when using Git, the ability to compare differences between versions of a codebase is crucial for effective software development. Among the various needs, examining the changes between your current state of the working directory and the last commit is a frequent task. This article delves into the specifics of how this comparison works, why it’s vital, and ways to check differences using Git commands.

Understanding Git and Commit

Before proceeding to the comparison, it's essential to grasp some foundational concepts in Git:

  1. Repository: A container for your project. It holds a history of changes made to the files within a project.
  2. Commit: A snapshot of your code at a specific point in time. Commits are used to bookmark states of a codebase and usually include a message explaining what changes were made.
  3. Working Directory: The current state of your development environment, including any modifications before staging or committing.
  4. Staging Area (or Index): A temporary area where changes are stored to prepare for a commit.

Comparing the Current State to the Last Commit

When developing software, you might want to see what changes you've made since the last time you committed your code. This is often done for:

  • Reviewing changes: To verify modifications before committing.
  • Identifying errors: To locate where potential bugs were introduced.
  • Collaborating: To communicate changes to colleagues or team members.

Git Commands for Showing Differences

Git provides a versatile command, git diff , which facilitates the examination of these differences. Here are common usages:

  1. Difference between the working directory and the last commit:
  • Headers: Lines starting with index , --- , and +++ refer to the file statistics before and after the changes.
  • Hunks: Represent individual changes, indicated by lines starting with @@ . Each hunk specifies the location of changes in the file.
  • Content Changes: Lines prefixed with - show content removed from the last commit, while + indicates added content.
  • The + and - signs denote added and removed lines, respectively.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.