View Differences in Changes

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Git is a popular version control system that allows developers to track changes made to their codebase. One of the essential features of Git is the ability to view differences between changes made to a file. This feature is useful when you want to see what changes you have made to a file or compare changes between different versions of a file.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BasicOperationsGroup(["`Basic Operations`"]) git/BasicOperationsGroup -.-> git/diff("`Compare Changes`") subgraph Lab Skills git/diff -.-> lab-12777{{"`View Differences in Changes`"}} end

View Differences in Changes

As a developer, you may want to view the differences between your staged or unstaged changes and the last commit. This is useful when you want to review your changes before committing them or when you want to see what changes you have made since the last commit.

To demonstrate how to view differences in changes, we will use the git-playground repository. Suppose you have made some changes to the README.md file and want to view the differences between your changes and the last commit.

  1. Open your terminal and navigate to the git-playground directory:
cd git-playground
  1. Use the git diff command to view the differences between your unstaged changes and the last commit:
git diff
  1. Alternatively, you can use the --staged option to view the differences between your staged changes and the last commit:
git diff --staged

This is the result of completing step 2:

diff --git a/file1.txt b/file1.txt
index bfccc4a..ee23125 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1 +1,2 @@
 This is file1.
+hello,labex

This is the result of completing step 3:

diff --git a/README.md b/README.md
index 0164284..f47591b 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,3 @@
 ## git-playground
 Git Playground
+hello,world

Summary

In this challenge, you learned how to view differences between staged or unstaged changes and the last commit using the git diff command. This feature is useful when you want to review your changes before committing them or when you want to see what changes you have made since the last commit. Keep practicing and exploring Git to become a proficient developer.

Other Git Tutorials you may like