The Wayback Machine - https://web.archive.org/web/20240824142502/https://www.geeksforgeeks.org/introduction-to-git-branch/
Open In App

Introduction to Git Branch

Last Updated : 18 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Branching means diverging from the mainline and continue to work separately without messing with the mainline. Nearly every VCS has some form of branch support. In Git, a branch is simply a reference to the commit, where the following commits will be attached. 

Git vs SVN: In recent years, the use of git has risen tremendously. Unlike SVN, git allows users to work on their own copy of the repository. One of the main reasons for git’s success is its speed. Since all the files are stored locally on the developer’s computer, he/she has access to them all even with a very poor internet connection. Branching in other VCS can be an expensive operation in both time and disk space. 

Git vs Other VCS: Git branching feature is what sets it apart from other VCS tools. Git branch operation is nearly instantaneous, making the flow of switching back to branches and forth very smooth. Following are the few pros why git is preferred over other VCS: 

  • High operation Speed
  • Full history of tree available
  • Branch Operations
  • Sorted Distributed Model

Branches: When you make a commit, Git stores a commit object that contains a pointer to the snapshot of the content you staged. This object also contains the author’s name and email address, the message that you typed, zero parents for the initial commit, one parent for a normal commit, and multiple parents for a commit that results from a merger of two or more branches. A branch as discussed earlier is a separate line of development as git stores branch as a reference to commits. 

Note: Git branch is used to list, create or delete branches and it’s easier to logically divide up your work than have big beefy branches. 

Git Branch- options 
 

Options Description
git -a These options list both remote-tracking branches and local branches.
git branch – – list Activate the list mode or simply git branch list all the branches of the repository.
git -c “Branch” This option is used to Copy a branch.
git -C “Branch” It is a shortcut for – – copy – – force
git -d or – – delete “Branch” This option deletes the specified branch. The branch must be fully merged in its upstream branch.
git -D “Branch” This is a shortcut for – – delete – – force. It deletes the branch even if it has unmerged changes.
git -m “Branch” This option move/rename the branch.
git -M “Branch” It is a shortcut for – – move – – force.
git -q or – – quiet when creating or deleting a branch, this option suppresses non-error messages.
git -r or – – remote This option is used to list all the remote-tracking branches. If used with -d, this can also be used to delete remote branches.
git -t or – – track When you create a new Branch it set up a configuration to mark the start-point branch.
git – -no-track It does not set ip the “upstream” configuration.
git – -edit-description It edits the description of what the branch is for.
git – – – -contains [] Show the list of branches that contain specified commits.
git branch myBranch Creates a new branch-“myBranch”
git checkout -b myBranch Creates a new branch-“myBranch” and then checkout in it
   
  • Git branch listing the one and only branch of the project ie. Master Branch 

  • Renaming Master Branch 

  • Copying “Branch2” and creating “branch3” 

  • Deleting “branch3” 

Summarizing: In this article, we discussed the high operation speed and branch behavior. We learned about the git branch command whose primary function is to list, create, delete branches. We also learned about various git branch options to fully implement the functionality of this command.


Previous Article
Next Article

Similar Reads

Git - Create a Branch From Another Branch
Branching in Git allows developers to work on isolated features or fixes without affecting the main codebase. However, what if you need to create a new branch based on the changes in another branch? That's where the concept of branching from another branch comes into play. In this guide, we'll see the process of creating a branch from another branc
3 min read
How to Compare a Local Git Branch with its Remote Branch ?
When working with Git, it's often necessary to compare your local branch with its remote counterpart to understand the differences in terms of commits and changes. This helps in keeping your local work in sync with the remote repository and managing potential conflicts. This article will guide you through various methods to compare a local Git bran
3 min read
How to Make the Current Git Branch a Master Branch?
Managing branches effectively is a key part of using Git for version control. Occasionally, you might find yourself in a situation where the current branch you're working on should be the new master branch. This could be due to the current branch evolving to become the main line of development, or simply a restructuring of your project's branch str
3 min read
How to Replace Master Branch with Another Branch in GIT?
In Git, the "master" branch traditionally serves as the primary development branch in many repositories. However, there are situations where you might want to replace the contents of the "master" branch with those of another branch. This could be due to a variety of reasons, such as renaming the default branch to something more inclusive or reflect
2 min read
How to Create a Branch In Git from Another Branch?
Branching is a fundamental aspect of version control systems like Git, which helps developers work on multiple features or bug fixes simultaneously without interfering with the main codebase. Creating a branch from another branch is a common practice, especially when you want to build upon existing work or isolate changes for a specific feature. In
3 min read
Git Merge Hotfix Branch Into Feature Branch
When working on a software project, you may encounter situations where a hotfix needs to be applied to a feature branch. A hotfix typically addresses urgent issues in the codebase that need immediate attention and correction. Integrating these changes into a feature branch ensures that the fixes are incorporated into ongoing development, maintainin
4 min read
How To Replace Local Branch With Remote Branch in Git?
Managing branches in Git can sometimes require drastic measures, such as replacing a local branch entirely with its remote counterpart. This can be useful in scenarios where the local branch has diverged too much from the remote branch, leading to complexities that are easier to resolve by completely overriding the local branch. This article will g
4 min read
Difference Between Git remote prune, Git prune and Git fetch --prune
Git is a version control system that helps developers manage and track changes in their codebase. Among its many commands, git remote prune, git prune, and git fetch --prune are essential for maintaining a clean and organized repository. This article will explain the differences between these commands, their syntax, uses, and provide examples to il
4 min read
How To Find Out Which Remote Branch A Local Branch Is Tracking?
Git is a powerful version control system that allows developers to manage and track changes in their code repositories. One of the key features of Git is its ability to track relationships between local and remote branches. Knowing which remote branch a local branch is tracking can help you manage your workflow more efficiently, especially when col
3 min read
How to Create a New Branch in Git and Push the Code?
Branching in Git is a helpful feature for software developers working on a big team project. It allows the team members to work on different aspects of the software by creating a branch from the main branch. The main branch is not affected by the changes in the created branch until it is merged into the main branch. Once, work is complete for the b
8 min read
How to Delete Local Branch in Git?
Git is a widely used distributed version control and source code management system. It effectively tracks changes to source code, enabling effortless branching, merging, and versioning. What are Local Branches? In Git, branches are lightweight pointers to commits in the repository's history. Each branch represents an independent line of development
1 min read
How to Check Branch in Git?
In Git, branches are independent lines of development that allow you to work on features or fixes without affecting the main codebase. Checking branches is important for managing project history, coordinating collaboration, and ensuring smooth development workflows. This article will walk you through the various approaches and commands to check bra
2 min read
How to Rename Branch in Git?
Renaming branches in Git is important for keeping your repository organized and understandable, especially when collaborating with others. Clear and descriptive branch names improve project management and help team members identify the purpose of each branch. Renaming branches also ensures consistency and clarity throughout the development process.
1 min read
How to Checkout Remote Branch in Git?
When collaborating on projects using Git, you'll often need to switch between different versions of the codebase stored in remote repositories. These versions are managed through branches. To work on a specific version (branch) hosted remotely ( on GitHub), you need to check it out in your local Git repository. Table of Content Approach 1: Detachin
3 min read
How to Create a New Branch in Git?
Git is a powerful and widely used version control system that helps developers manage code changes across projects efficiently. One of the fundamental features of Git is branching, which allows developers to diverge from the main line of development and work on different tasks or features independently. This guide will walk you through the process
4 min read
How to Set Upstream Branch on Git?
It is important to Setup an Upstream Branch in Git to make the workflow smooth and manage branches efficiently. When you want to clone a new repository or work with various feature branches, you need to know how to work with upstream branches and how you can set them up. In this article, we will explore how to set up upstream branches and also see
4 min read
How to Delete a Branch in Git?
When working with Git, it’s common to create branches to isolate changes and work on new features or fixes. However, once a branch has served its purpose, it’s often necessary to delete it to keep your repository clean and organized. In this article, we’ll see the process of deleting a Git branch using Git commands locally on your machine. When to
3 min read
How to Merge a Git Branch into Master?
If you're working with Git, merging branches is a fundamental task. Whether you're adding new features, fixing bugs, or improving documentation, merging ensures that your changes are integrated into the main codebase. In this guide, we'll walk you through the process of merging a Git branch into the master, which helps your codebase remain up-to-da
3 min read
How to Change Git Default Branch From Master?
Changing the default branch in Git from master to main has become a common practice in many development teams. This shift is part of a broader movement towards more inclusive and descriptive terminology in software development. This article will guide you through the steps required to change your default branch from master to main, covering both lo
4 min read
How to Reset a Git Branch to a Remote Repository?
Resetting a Git branch to match a remote repository is a common task, particularly when you want to discard local changes and make your branch identical to the remote counterpart. This can be useful in scenarios where your local branch has diverged from the remote, and you want to synchronize it with the remote repository. Steps to Reset a Git Bran
3 min read
How to Show Pretty Git Branch Graphs?
Git branch graphs provide a visual representation of your project's branching structure and history. These graphs help developers understand the flow of commits, branches, merges, and more. Creating pretty Git branch graphs can significantly improve the readability of your project's history, making it easier to track changes, identify conflicts, an
1 min read
How to Fix Git Error: There is No Tracking Information for the Current Branch
Ever tried to `git pull` or `git push` and hit a roadblock with the message "There is no tracking information for the current branch"? This means your local branch isn't connected to a corresponding branch on the remote server. Don't worry, this article will help you fix it in two ways. Table of Content What is the ProblemLink Your Local Branch to
2 min read
How to Create a Remote Git Branch?
Creating a remote Git branch allows multiple developers to work on different features or fixes simultaneously. It ensures a clean and organized workflow, enabling collaboration without affecting the main codebase. This article covers the steps to create a remote Git branch and push it to a remote repository like GitHub. Table of Content Approach 1:
2 min read
How to Git Pull from a Specific Branch?
When working with Git, you often need to update your local repository with changes made in a remote repository. The git pull command is used for this purpose. It fetches changes from a remote repository and merges them into your current branch. Sometimes, you may want to pull changes from a specific branch. This article will guide you through the p
3 min read
How to Clone a Branch in Git?
Git is a popular version control system that allows developers to track changes in their code and collaborate with others. Cloning a branch in Git involves creating a copy of a specific branch from a remote repository. This article will guide you through the process of cloning a branch in Git. Table of Content Understanding Git BranchesSteps to Clo
3 min read
How To Rebase a Local Branch Onto a Remote Master in Git?
Git, a powerful version control system, offers various methods to manage branches and integrate changes. Rebasing is one such technique used to incorporate changes from one branch onto another, creating a linear history. This article will walk you through the process of rebasing a local branch onto a remote master in Git. Steps To Rebase a Local Br
3 min read
How to Push a Local Branch to a Remote Repository in Git?
Git is a popular version control system that allows you to track changes in the codebase and collaborate with others. One of the common tasks in Git is pushing a local branch to a remote repository. This article will guide you through the steps to achieve this. Pushing Local BranchPushing a local branch to a remote repository involves creating a br
2 min read
How to Move Existing, Uncommitted Work to a New Branch in Git?
Git is a widely used distributed version control and source code management system. A common scenario developers face is needing to move their uncommitted work to a new branch. This can be important for maintaining organized workflows, especially when realizing that ongoing changes belong to a separate feature or bug fix. In this article, we will s
2 min read
How to Move the Most Recent Commit(s) to a New Branch With Git?
Git offers powerful features for managing your codebase efficiently. Sometimes, you may find yourself needing to move the most recent commit(s) to a new branch in Git. This could be due to various reasons, such as realizing that changes were made on the wrong branch or wanting to isolate experimental work. Fortunately, Git provides simple ways to a
4 min read
How To Delete Remote Branch in Git?
Git is an important tool in the process of application development and is used widely in the software industry by developers to maintain the codebase. Using this developers are able to organize their codebase and manage the version history of their project. Now, as a developer, you need to know how to delete a Git branch because it will lead to a b
1 min read
Article Tags :
three90RightbarBannerImg