Back to: Git Course
What is a Git Branch?
A Git branch is a separate line of development that allows you to work on a specific feature or version of your project without affecting the main codebase. It enables multiple developers to work on different parts of the same codebase simultaneously and merge their changes together later.
In general, a branch means a separate workstation. Like this concept of a separate workstation, we can also create separate branches in Git according to the steps of our workflows, like Development (dev), Testing (test), or Production (prod), or based on the functionality we will add to our project.
We also create branches in Git to not get messed up with our code. It is a common practice to work in a group on any project. Many people work their own way. If they start to write code for the same project in the same branch and if one of them writes the wrong code, it may destroy the code structure or the entire project. So, to protect the original (master) branch, the Git branching concept has come into existence.
Some people write code as developers, while others write code for testing, etc. So, it’s best practice to make separate branches according to work styles. And finally, we can “MERGE” these branches with the master (original) branch.
Creating a new branch
When we create a repository, the default branch is called master or main. We can create additional branches :
git branch <branch_name>
Example:
$ git branch test
This just creates the new test branch, which at this point is exactly the same as our master (Original Branch) which means this branch gets property(I.g. : codes) exactly like the master branch.
Checking available branches
$ git branch
test
* master
Two branches are available, test and master. Master is our default branch and is marked with an asterisk symbol. But if want to work on our newly created branch, we have to switch to this branch.
Switching to another branch
$ git checkout test
Marge Branches
Now, we are in the test branch, we can add new features here and update our code but it won’t affect the master branch.
So, now one question should arise how can the master branch get updated??
Yes, we need to merge our branch with the master branch. After adding new features we can commit and push it to our branch(test) and finally merge it with the master branch.
If new features are successfully added to our branch, we can go back to the master branch.
$ git checkout master
Now marge dev branch with the master branch
$git merge dev
Remove a branch:
The master branch is now up to date. The dev branch is no longer needed and can be removed.
$ git branch -d dev
Delete Local Branch:
To delete the local branch we can use one of the following:
$ git branch -d <branch-name>
$ git branch -D <branch-name>
📝Note: The -d option is an alias for –delete, which only deletes the branch if it has already been
fully merged with its upstream branch.
We can also use -D, which is an alias for –delete –force, to delete the branch irrespective of its merged status.
Delete remote Branch:
$ git push --delete <remote-name> <branch-name>
📝 Note: In most cases, the remote name is “origin”.
Git Branch Summary Table
Concept | Command | Example | Explanation |
---|---|---|---|
Create a branch | git branch <branch_name> | git branch feature | Creates a new branch named “feature” |
Switch to a branch | git checkout <branch_name> | git checkout feature | Switches to the “feature” branch |
Create and switch to a branch | git checkout -b <branch_name> | git checkout -b feature | Creates a new branch named “feature” and switches to it |
List branches | git branch | git branch | Lists all branches in the repository |
Delete a branch | git branch -d <branch_name> | git branch -d feature | Deletes the “feature” branch |
Merge branches | git merge <branch_name> | git merge feature | Merges the “feature” branch into the current branch |
Resolve merge conflicts | – | – | When there are conflicting changes in the branches being merged, you must manually resolve the conflicts |
Push a branch to remote | git push -u <remote_name> <branch_name> | git push -u origin feature | Pushes the “feature” branch to the “origin” remote repository |
Pull changes from remote branch | git pull <remote_name> <branch_name> | git pull origin feature | Pulls the latest changes from the “feature” branch in the “origin” remote repository |
This chart summarizes the basic concepts of Git Branch and provides examples for each concept. It can be used as a quick reference guide for beginners who are learning Git.
See code examples in our GitHub repository.
Follow us on social media
Author