Difference Between Branches

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 manage their codebase efficiently. One of the essential features of Git is the ability to create and manage branches. Branches allow developers to work on different features or bug fixes simultaneously without interfering with each other's work. However, at some point, you may need to compare the changes between two branches. In this challenge, you will learn how to view the difference between two branches using Git.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BranchManagementGroup(["`Branch Management`"]) git/BranchManagementGroup -.-> git/branch("`Handle Branches`") subgraph Lab Skills git/branch -.-> lab-12641{{"`Difference Between Branches`"}} end

Difference Between Branches

You have been working on a project with your team, and you have created a branch named feature-1 to work on a new feature. Your colleague has also created a branch named feature-2 to work on a different feature. You want to compare the changes between the two branches to see what has been added, modified, or deleted. How can you view the difference between the two branches?

Tasks

Suppose your GitHub account clones a repository called git-playground from https://github.com/labex-labs/git-playground.git.

  1. Navigate to the repository directory and configure your GitHub identity .
  2. Switch to the feature-1 branch and add "hello" to the README.md file, add it to the staging area and commit, the commit message is "Add new content to README.md".
  3. Switch to the feature-2 branch and add "world" to the index.html file, add it to the staging area and commit, the commit message is "Update index.html file".
  4. View the difference between the two branches.

The output should display the difference between the feature-1 and feature-2 branches.This shows how the final result will look like:

diff --git a/README.md b/README.md
index b66215f..0164284 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,2 @@
## git-playground
Git Playground
-hello
diff --git a/index.html b/index.html
new file mode 100644
index 0000000..cc628cc
--- /dev/null
+++ b/index.html
@@ -0,0 +1 @@
+world

Summary

In this challenge, you have learned how to view the difference between two branches using Git. By using the git diff command with the branch names separated by two dots, you can compare the changes between the two branches. This feature is useful when you want to merge changes from one branch to another or when you want to see what has been modified between two branches.

Other Git Tutorials you may like