Creating a new branch in Git ensures that changes are made in isolation without affecting the stability of the main branch or other active branches. It enables teams to collaborate more efficiently, manage different project versions, and merge updates smoothly after being tested and approved. Moreover, developers can reduce conflicts, streamline their workflow, and maintain a clean, well-organized codebase using git branches. In this write-up, we’ll discuss different methods of creating git branches locally and remotely.
Why Use Branches in Git
Branches in Git let you work on different tasks without affecting the main code. You can create a branch to add a feature, fix a bug, or test something new, all while keeping the main project safe and stable. Git makes branching fast and easy, so teams can work on multiple things at once and merge changes smoothly. This keeps the workflow organized and helps avoid conflicts.
Related: Learn how to clear cache in Git.
Creating a New Branch in Git
Branches in Git allow you to separate your work from the main codebase, which makes it easier to develop new features or apply fixes independently. You can use the following syntax to create a new branch in Git:
git branch [branchName]
Let’s replace branchName with the actual name you want to use for the new branch:
git branch mte

You can verify the branch creation using the following command:
git branch
This command retrieves the lists of all local branches and highlights the current one with an asterisk *:

The output shows that a new branch named mte has been successfully created. The current branch is still master; however, we can switch to the newly created branch using the following command:
git checkout mte
The output confirms that Git has successfully switched to the new branch:

Creating a New Git Branch and Switching to it Immediately
We can create a new Git branch and switch to it immediately in a single step using the following command:
git checkout -b maketecheasier

Alternatively, we can use the git switch command with the -c option to create a new branch and automatically switch to it immediately:
git switch -c mte2

Both these commands create a new branch from the current branch. If you want to create a new branch from a different branch (other than your current one), you need to specify that branch explicitly using the following command:
git checkout -b [newBranch] [targetBranch]
Replace targetBranch with the actual branch name from which you want to create a new one. For example, if you are currently on the mte2 branch but want to create a new branch from the maketecheasier branch, you must specify the target branch as follows:
git checkout -b mteClone maketecheasier

Creating a Branch from a Specific Commit
In Git, every commit saves a snapshot of your project at a certain point in time. As you make changes and improvements, Git keeps track of them through these commits. Each commit has a unique identifier called a hash, which you can use to create a new branch starting from that exact project version.
To get the list of recent commits along with their hash values, run the following command:
git log --oneline
This command returns a list where each entry includes a short hash and the commit message:

Once you have the hash of the commit you want to branch from, use the following command to create a new branch based on it:
git branch [newBranch] [commitHash]
For example, this command creates a new branch based on the commit hash 990d80c:
git branch mteExample 990d80c

Creating a Remote Branch and Pushing it to GitHub
If you want to make a new branch on your computer based on a branch that already exists on a remote repository (like GitHub), you can use the git branch command with the --track option:
git branch --track newBranch origin/remoteBranch
Replace newBranch with the name you want for your local branch and origin with the name of the remote repository (usually it’s called origin by default). Replace remoteBranch with the name of the branch from the remote repository you want to copy.
For example, if there’s a branch named mteTest on the remote and you want to create a local branch called testExample from it, you can run the following command:
git branch --track testExample origin/mteTest
This command links your local testExample to track changes from origin/mteTest.
Creating a Git Branch in a Remote Repository
Once you’ve created a new branch on your local machine using git branch, you might want to add that branch to your remote repository (like GitHub). To do that, you can use the git push command with the -u option:
git push -u remoteRepo localBranch
Replace remoteRepo with the name of your remote repository and localBranch with the name of the branch you created locally. For example, if you created a local branch called mteBranch and want to push it to the remote repository named origin, you can run the command as follows:
git push -u origin mteBranch
This command creates the mteBranch on the remote repository and links your local branch to the remote one, so future push and pull commands will know where to send or get updates from.
Deleting Git Branches
If you have a local Git branch that you’ve already merged and pushed to the remote repository, you can safely delete it using the -d option with the git branch command:
git branch -d branchName
For example, the following command deletes the mte branch:
git branch -d mte

If you want to delete a branch no matter whether it’s merged or not, you can use the -D option (uppercase D). This forcefully removes the branch:

In short, creating a new branch in Git is a simple way to manage changes without affecting the main project. It helps you work on features, fix bugs, or test ideas independently. This way, it keeps your codebase clean and organized. In this article, we explored how to create branches locally, from specific commits, and push them to remote repositories like GitHub.
If you’re new to Git, build these good habits from day one to strengthen your understanding and avoid common mistakes. You can also check out the beginner’s guide to Git for a complete introduction to using Git effectively.
