- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why is git branching fast compared to other version control systems?
Branching allows us to diverge from the main line of work and work on something else in isolation. Conceptually, we can think of a branch as a separate isolated workspace. We have a main workspace called the master.
We can create a feature branch and work separately on the feature branch to add more features to the project without affecting the main line of work. If there is some error in the feature branch, we can fix it without affecting other collaborator’s work. Once everything is working properly in the feature branch, we can merge it with the main line of work, that is the master branch.
The above diagram shows there are two branches the master and one feature branch. The ‘commit#21’ is derived from the ‘commit#2’, so the feature branch contains all commits from ‘commit#1’ to ‘commit#23’ and the master branch contains ‘commit#1’,’commit#2’ and ‘commit#3’. The master and feature branches can work in isolation.
Branching allows us to work on different work items without messing up the code base of the main line of work. We keep the main line of work as stable as possible, so we can release it to production at any time. If a new developer joins the team, the new member can start using the stable code base from the master. That is the idea of branching.
How does Git manage branches differently?
If we use a Centralized Version Control System (CVCS) like ‘subversion’, when a new branch is created it will copy all contents from the main branch. This will result in a performance bottleneck if we have a large code base. Many people don't use branching in CVCS due to the fact that branching is slow and takes a lot of disk space.
Git branching is super−fast and cheap. In Git, branching is just a symbolic name pointing to the last commit. So, the master branch is always a pointer to the last commit in the main line of work. As we perform a new commit, Git moves the master reference forward automatically similar to a linked list.
When we create a new branch, Git will create a new pointer (here, feature) pointing to the last commit. From the below diagram we can see that featureis just a pointer to the last commit. So, git branches are not copying any commit data, it just creates a pointer.
Now when we commit new changes in a newly created feature branch, the master pointer will remain intact, but the feature pointer will move ahead. This is shown in the diagram below. Initially, both the master and feature pointed to ‘commit#3’. Then we switched to the feature branch and performed a commit ‘commit#4’. So, the feature pointer moved ahead but the master branch pointer remained where it is. The HEAD pointer points to the current working branch.
- What is a fast-forward merge in Git?
- Difference between Centralized Version Control and Distributed Version Control
- Why Qries is a better platform compared to Quora?
- Why is the fare of Rajdhani express more as compared to other trains in the same category?
- What is .git folder and why is it hidden?
- Difference between a Centralised Version Control System (CVCS) and a Distributed Version Control System (DVCS)
- What is delayed branching?
- What is Multiway Branching?
- What is Conditional Branching?
- Why is Python slower than other languages?
- How to disable fast forward merges? What are its pros and cons in Git?
- Why should you use git add command before using git commit command?
- What is design space of delayed branching?
- How to install the latest version of Git on CentOS 7.x/6.x?
- How to check the PowerShell version installed in local and remote systems?