Introduction to Git Branches

Learn via video courses
Topics Covered

Overview

Branching means diverging the content from the ongoing mainline content and work without hampering the main line. Almost all the Version Control Systems (VCSs) support the branching facility. A branch is an independent line of development that is used to add certain features and fix bugs without hampering the main project. So, we can develop new features in parallel and when the development is completed, we can add the back to the main project. The git branch command enables us to perform parallel development. The command can create, rename, list, and delete branches.

Pre-requisites

The prerequisites for learning about git branches can be a basic understanding of Version Control Systems, Branching, and Git. Let us discuss them briefly before learning about the git branches and their use cases.

Git

Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. Git is free and one of the most widely used version control systems. We can use Git through the command line as well as through its graphical user interface (GUI). The command line or terminal version of Git is known as Git Bash on the other hand the GUI version of Git is known as Git GUI.

GitHub

GitHub is a cloud-based central repository that can be used to host our code for team collaboration. It is a hosting service that is used to manage the git repository in the central server. GitHub is a free (for certain limits) and easy-to-use platform that enables teammates to work together on projects. GitHub tracks the changes made in the project workflow and we can also revert ba a certain state of the project (as GitHub saves the history of the project).

Introduction to Git Branch

Branching means diverging the content from the ongoing mainline content and working without hampering the mainline. Almost all the Version Control Systems (VCSs) support the branching facility.

A branch is an independent line of development that is used to add certain features and fix bugs without hampering the main project. So, we can develop new features in parallel and when the development is completed, we can add the back to the main project. By default, all the GitHub repository has the master branch which can be used for production.

So, a new branch is a copy of the master branch which is created for bug fixes and for the addition of new features. After the bug is fixed or new features are added, we can merge the branch to the master branch. The git branch command enables us to perform parallel development. The command can create, rename, list, and delete branches.

Let us look at a situation where we do not create branches for development and understand the problem so that we get a clear understanding of the need for branching. Suppose we have started working on a project and developed the project and it is not in production. Now, if there comes a bug or we want to introduce a new feature to our ongoing project then the only thing that we can do is to change the code and fix the bug or add the feature. Now if this bug fixing or feature update introduces any kind of problem, our ongoing project will get hampered and we may lose. Now suppose that we created a branch and perform the changes in the branch. So, we can merge the branch after we have successfully made the changes and tested it. In this way, we will not face any errors and losses in the ongoing main branch (project).

Let us look at some of the advantages that branching provides us with:

  • Whenever we create a new branch, we can independently create a new design or fix any bug without hampering the working main or master branch.
  • A branching can be created to fix bugs and add a new feature, and if we are done with the changes then we can simply push the changes to the main branch and then delete the branch.
  • A branch can be created by a team of developers to test any functionality as well.
  • We can even switch between the various branches and perform different tasks without even hampering the main branch.
  • Branching or branch creation is a very fast and lightweight process in git. So, we can easily create branches for parallel development.
  • To create, delete, update, etc. we have a command named git branch and we can use this command along with numerous flags like -a,-D, etc. to perform several operations related to git branches.
  • The branching is tightly associated with the git checkout and git merging.

Please refer to the image provided below for more clarity. BRANCHING OF GIT

How it Works?

Let us now learn how git branches internally work. As we know that a branch is an independent line of development of a project. So whenever we create a branch, we request a new working directory, project history, and working area (just a copy of the main or master repository). Whenever we make a new commit, the snapshot of the commit is not stored in the master branch but is stored in the current branch which can be seen as a fork in the project's history.

Now as we have earlier discussed the git branch command is capable of creating, listing, renaming, and deleting the branches. (We will be learning about the associated flags to perform these tasks in the next section). The git branch command is associated with the git checkout and git merge commands to perform numerous operations.

Git Branch Options

Let us now look at the several operations of the git branches.

  • git -a: This command is used to list both the remote racking and the local branches.
  • git branch --list: This command is used to list all the branches present on the git repository (it can also be used to activate the list mode of branches).
  • git -c "branch": This command is used to copy a branch.
  • git -C "branch": This command is used to forcefully copy a branch. It is a shortcut to the --copy --force command.
  • git -d or git --delete "branch": This command is used to delete a specific branch. We must know that to execute this command successfully, we should make sure that the current branch is completely merged with the upstream branch.
  • git -D "Branch": This command is used to forcefully delete a branch so even if our current branch is not merged with the upstream branch, we can delete the branch using this command. This command is a shortcut to the --delete --force command.
  • git -m "branch": This command is used to move or rename a branch.
  • git -M "branch": This command is used to forcefully move or rename a branch. This command is a shortcut to the --delete --force command.
  • git -q or –– quiet: This command is used to suppress the error messages during the creation and deletion of branches.
  • git -r or --remote: This command is used to track all the remote-tracking branches. If we use the -d flag then we can also use this command to delete the branch.
  • git -t or --track: This command is used to set up a configuration and create a new branch (and it also marks the starting point of the branch).
  • git --no-track: This command is used to unset the upstream configuration.
  • git --edit-description: This command is used to edit the description of the current branch
  • git branch "branch-name": This command is used to create a new branch with the provided name.
  • git checkout -b "branch-name": This command is used to create a new branch and then checkout to the created branch.

Create Branch

For creating a branch we can use the git branch command. After creating the branch, we can use the git checkout command to move to the created branch.

What is the git checkout command? The git checkout is one of the most important commands used in git. It is used to switch between various versions of any target entity. The git checkout command mainly works with three distinct entities i.e. files, commits, and branches. The git checkout command lets us switch between the branches. The git checkout command is usually used with the git branch command.

Example: In the image provided below, we have listed the current branch and then created a test branch. TEST BRANCH CREATION

If we want to create a branch and move to the created branch using a single command, we can use the git checkout -b "branch-name" command for the same.

Deleting Branches

Deletion of the branch is a very crucial command, we must delete a branch after it is no longer used and we have merged the contents of the branch in the other working branch.

To delete a branch, we first need to move to the other branch (switch to another branch) and then we can easily delete the branch by specifying the name. We have two commands to delete a local branch namely git branch d and git branch D to delete the branch.

Example: In the image provided below, we have deleted a test branch. DELETION OF TEST BRANCH IN GIT

Delete a Remote Branch

We can use the push command along with the name of the remote server (origin) and the -delete flag to delete a remote branch. The overall command for the same is:

List Branch

To list down all the branches on the current repository, we have two commands:

  1. git branch --list
  2. git branch.

We can simply run any one of the above commands to list down all the available branches of the current working repository.

Example: In the image provided below, we have listed all the branches. LISTING BRANCHES IN GIT

In the above image, we can see an astrick sign (*) that is highlighting a branch i.e. our currently active branch.

Rename Branch

We can rename the branch using the git branch command along with the -m flag to rename the branch. The overall command for the same is:

Example: In the image provided below, we have renamed the test branch to the TESTING branch. RENAMING BRANCHES IN GIT

Merge Branch

If we want to merge other branches to the current active branch, we can use the git merge command. What is a git merge? Well, Git merge is used to merge multiple commits of various branches into a single branch. We perform branching for parallel development of new feature(s) and bug fixing. Once the bug is fixed or the new feature is added, we can merge these branches into a single branch. The git merge command can also be considered as an alternative to the git rebase command which is also used to merge branches. For merging branches, git takes the head points of the commits and it will first find the common base commit among the branches and will perform a merge commit to combining the changes of each commit sequence.

the overall command for the same is:

Switching Branches

For switching between the branches, we can use the git checkout command. We can specify the name of the branch that we want to switch to along with the git checkout command. The overall command for the same is:

Example: In the image provided below, we have switched the test branch to the master branch and vice versa. SWITCHING BRANCHES IN GIT

Conclusion

  • Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base, etc.
  • Branching means diverging the content from the ongoing mainline content and work without hampering the mainline.
  • A branch is an independent line of development that is used to add certain features and fix bugs without hampering the main project.
  • A new branch is a copy of the master branch which is created for bug fixes and for the addition of new features. After the bug is fixed or new features are added, we can merge the branch to the master branch.
  • A branching can be created to fix bugs and add new features, and if we are done with the changes then we can simply push the changes to the main branch and then delete the branch.
  • A branch can be created by a team of developers to test any functionality as well. Branching is a very fast and lightweight process in git. So, we can easily create branches for parallel development.
  • Whenever we create a branch, we request a new working directory, project history, and working area (just a copy of the main or master repository)
  • For creating a branch we can use the git branch command. After creating the branch, we can use the git checkout command to move to the created branch.
  • Deletion of the branch is a very crucial command, we must delete a branch after it is no longer used and we have merged the contents of the branch in the other working branch. We can use the git branch d and git branch D commands to delete the branch.
  • We can use the push command along with the name of the remote server (origin) and the -delete flag to delete a remote branch.
  • To list down all the branches on the current repository, we have two commands i.e. git branch --list and git branch.
  • We can rename the branch using the git branch command along with the -m flag to rename the branch.