Branching is a feature in almost all modern version control systems. Branches allow developers to move away from the main version of their code and make changes to their code without updating the main version of the code.
In other version control systems, branching can be a difficult process. Some version control systems ask that you create a new copy of your code, which can be laborious for larger projects. Git, on the other hand, has a simple branching feature that allows you to easily navigate between different branches.
This tutorial will discuss, with examples, the basics of branching in Git and how you can use the git branch command. By the end of reading this tutorial, you’ll be an expert at branching in Git.
Branches are an essential part of working with Git.
In professional projects, branches are often used for every feature or fix that is being made. This is because branching allows a developer to work on a repository without affecting the main codebase, so the developer can work on a feature or fix a bug with others before it is pushed to the main copy of the project’s code.
This approach to software development reduces the chance that code containing errors is merged into the main version of a codebase.
In Git, a branch is a new line inside a repository. This new line will include its own version of the codebase’s code and will evolve independently from the main branch. In most codebases, this branch is referred to as the “master branch”.
Because branches are independent from the master branch, they can be used to work on different features in a codebase in parallel. Then, when a change is ready to become part of the main codebase, the change can be merged into the master branch or the main version of the code.
Git Branch Commands
User-created branches are independent from the master branch. These branches have their own commit history, and when you push changes to the branch, those changes will only appear on the branch to which they were pushed.
Think about branches as a way to create multiple versions of the same codebase. You could have one branch that includes the code for a new feature you’re working on, and another branch which stores the updated version of a repo that solves an open issue.
The git branch command is used to work with branches in Git. The command allows you to create, rename, and delete branches. You can also retrieve a list of branches using git branch.
In addition, the git branch is commonly used in addition to the git checkout and git merge commands, which are used to switch between versions of a codebase and to merge different versions of a codebase, respectively. We discuss these topics in their own tutorials.
Now we know the basics of branching in Git, we can explore how to use the git branch command with its supported flags and parameters.
Retrieve a List of Branches
To retrieve a list of branches in a Git repository, you can use the git branch command. Here’s the syntax for the git branch command:
Here’s an example output from this command:
* master * v0.9 * v0.8
As you can see, the git branch command returns a list of the branches in our Git repository. Another way to write this command is to use git branch –list, which also returns a list of branches in a Git repository.
Create a Branch
Suppose we want to create a branch called “v0.9.1” in our codebase. We could do so using this code:
git branch v0.9.1
This command creates a new branch with the name v0.9.1. When this command is run, your development environment will remain attached to the HEAD of the repo. So, if you want to see your new branch, you’ll need to use the git checkout command.
When a new branch is created, the repository is not changed in any other way aside from the fact a new branch exists. The history of the repository will stay the same. This is because when you create a branch, Git only creates a pointer to the new branch.
Delete a Branch
To delete a branch in a Git repository, you can use the -d flag.
Suppose we have decided that we want to delete the v0.9.1 branch from our code. Here’s the command we would use to delete the branch:
git branch -d v0.9.1
This command deletes our branch. However, the -d flag performs a safe delete operation, which means if there are unmerged changes (changes on the branch that have not been merged into another branch), then the branch will not be deleted. This ensures that you don’t lose access to a branch with code you want to merge by accident.
If you want to force a branch to delete, even if it has pending unmerged changes, you can use the -D flag. This will permanently delete a branch. Here’s the syntax for the -D flag:
git branch -D v0.9.1
After running this command, our branch has been deleted. The -D flag should only be used in cases where you are absolutely sure you want to delete a branch. This is because the -D flag shows no warnings when it is executed.
Once you have executed the git branch -d/-D command, the local copy of the branch will be deleted. However, the branch may still exist in the main copy of the repo, if your repo is stored remotely.
If you want to delete the remote copy of the branch, you’ll need to run another command. Suppose we want to tell our remote repository that the branch v0.9.1 should be deleted. We could do so using this command:
git push origin :v0.9.1
The above command will instruct our remote repository to delete the v0.9.1 branch.
Rename a Branch
You can rename a Git branch using the -m flag.
The -m flag renames the branch you are currently viewing. Suppose we want to rename the branch v0.9.2 (which we are currently viewing) to v1. We could do so using this code:
git branch -m v1
This command renames the branch we are viewing to v1.
The git branch command is used to create, rename, and delete branches. The command can also be used to retrieve a list of the branches that are associated with a git repo.
However, if you want to commit code to a branch, you’ll need to use commands such as git checkout, git add, and git commit, which are used to save changes to a codebase.
This tutorial discussed, with examples, the basics of branching in Git and how to use the git branch command. You’re now equipped with the knowledge you need to start branching code in Git like a professional developer!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more