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 lab, you will learn how to view the difference between two branches using Git.
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?
Suppose your GitHub account clones a repository called git-playground
from https://github.com/labex-labs/git-playground.git
.Follow the steps below:
- Change to the repository's directory using the command
cd git-playground
. - Configure your GitHub account in this environment using the commands
git config --global user.name "Your Name"
andgit config --global user.email "your@email.com"
. - Create and switch to the
feature-1
branch using the commandgit checkout -b feature-1
, add "hello" to theREADME.md
file, add it to the staging area and commit, the commit message is "Add new content to README.md" using the commandsecho "hello" >> README.md
,git add .
andgit commit -am "Add new content to README.md"
. - Switch back to the
master
branch. - Create and switch to the
feature-2
branch using the commandgit checkout -b feature-2
, add "world" to theindex.html
file, add it to the staging area and commit, the commit message is "Update index.html file" using the commandsecho "world" > index.htm
,git add .
andgit commit -am "Update index.html file"
. - View the difference between the two branches using the command
git diff feature-1..feature-2
.
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 lab, 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.
Want to learn more?
- π Practice Difference Between Branches
- π³ Learn the latest Git Skill Trees
- π Read More Git Tutorials
Join our Discord or tweet us @WeAreLabEx ! π
Top comments (0)