- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we diverge two branches in Git?
A branch in git is a series of interrelated commits. If two branches follow a non-linear path then they diverge each other. The diagram shows two diverged branches master and feature.
Consider the above diagram. Let us assume that both the branches were following a linear path before the initial commit. After the initial commit, the branch master has an independent commit c1 and the branch feature has its own commit c2. As the branches now follow a non-linear path, we can say that both the branches are diverged. Let us see this through an example
Step 1 − Create an empty repository with an initial commit.
$git init $echo hello>hello.txt $git add . $git commit -m 'Initial commit'
Output
[master (root-commit) dC3b134] Initial commit 1 file changed, 1 insertion(+) create mode 100644 hello.txt
Step 2 − Create a new branch named feature
$git branch feature $git branch
Output
Below output shows that the repository has two branches available, with master being the current branch (*)
Feature * master
Step 3 − Create a new commit in the master branch and check the history
$echo world>world.txt $git add . $git commit -m ‘world.txt’ $git log --oneline --all --graph
Output
Below output shows that the feature branch is pointing to the initial commit whereas the master branch has an independent commit.
* d6e2f5b (HEAD -> master) world.txt * dC3b134 (feature) Initial commit
Step 4 − Switch to the feature branch and create a new commit
$git switch feature $echo file1>file1.txt $git add . $git commit -m ‘file1.txt’ $ git log --oneline --all --graph
Output
The output displays the history graph of commits.
* e340791 (HEAD -> feature) file1.txt | * d6e2f5b (master) world.txt |/ * dC3b134 Initial commit
The initial commit hash dC3b134 was common to both the branches whereas the commit e340791 and d6e2f5b is specific to the feature and the master branch respectively. This proves that the branches are diverged from each other.