Git is an open-source version-control system for tracking changes during software development. It’s mutually independent branching model makes it stand out. Branches can be based on previous versions of the software to maintain the integrity of current progress while working on a bug fix or new feature.
This guide will detail 4 options to create a new branch in Git.
Note: This guide assumes a Linux environment. Git is also available on Windows and macOS.
Create Git Repository Locally
To create a new Git repository, enter the following command in your terminal:
mkdir git-app cd git-app git init
This creates and initializes a new Git repository in the git-app directory. Create a new markdown file and add a line of text:
echo This is a line of text > git-app.md
Check the status of the file:
You’ll see the file is untracked.
Track it and create the first commit by entering the following:
git add git-app.md git commit –m “First Commit”
Note: If this is the first time you’ve run Git, the operation may fail. Use the commands listed on the screen (Git config) to set your global email and username, then try again.
Create a New Git Branch
There are many ways you can create a new Git branch. In most cases it comes down to whether you are creating a branch from the main branch or, for example, a new commit or tag.
One common method of creating a new branch is with the command:
git branch <new_branch_name>
This doesn’t automatically switch to that branch. To switch Git branches, enter the following command:
git checkout <new_branch_name>
Note: Instead of <new_branch_name> type the name for the new branch.
Create New Git Branch From Current Branch
The easiest and most popular way of creating a Git branch is:
git checkout -b <new_branch_name>
This will create a new branch from your current branch. It will also automatically switch you to the new branch.
Create New Git Branch From a Different Branch
To create a new branch from a different branch, run the following command:
git checkout -b <new_branch_name> <specific_different_branch>
Instead of <new_branch_name> type the name for the new branch, and instead of <specific_different_branch> type the name of the existing branch from which the new one shall be created.
Create a Branch from a Commit
Commit is a command that saves changes you’ve made in the code. A project may have multiple commits as it’s revised and improved. Create a set of commits like in the sample below:
echo New line of text >> git-app.md git commit –a –m “New line added” echo Second line of text >> git-app.md git commit –a –m “Second new line” echo Third new line >> git-app.md git commit –a –m “Third new line” echo Last new line >> git-app.md git commit –a –m “Last new line”
Note: If you receive an error “Nothing added to commit but untracked files present”, enter:
git add git-app.md
Find the hash key for a specific commit:
The log contains the hash key.
Create a branch from an older commit:
git branch <new_branch_name> 89198
Note: 81898 in the example above represents the hash. Replace this with the actual hash from your git log command.
You don’t need to enter the whole hash key, just the first few characters will work. View the git log again, and you’ll see the new branch listed.
This is especially helpful if you need to go back to a previous version of the software to fix a bug without removing any existing features. To switch to the new branch, enter the following:
git checkout <new_branch_name>
Create a Branch from a Tag
A tag is a final, unchangeable version of a commit. Where a commit can be edited, tagged versions are usually permanent. Tags are used for production release versions of the software.
Create a tag in the test project:
git tag –a v1.0 –m “Version 1.0”
In a normal project, you would continue working on the software for the next release.
To create a branch from this tag, use the command:
git branch <new_branch_from_tag> v1.0
To switch to this branch:
git checkout <new_branch_from_tag>
Create a Branch Using Detached HEAD State
Detached HEAD state happens when you check out a commit that’s not formally part of a branch.
To test, use git log to get the hash for one of your commits, then enter:
git checkout d1d307
Replace d1d07 with the actual hash value from your system. You’ll get a message that says:
You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.
Just like the warning outlines, you can make changes based on this commit. Any changes will be lost, unless you save them.
To save your progress, stage it then enter the following:
git commit –m “test_case” git branch <test_case> git checkout <test_case>
To add the changes into the master, use the following:
git checkout master git merge <test_case>
How to Delete a Git Branch
To delete a git branch use the command:
git checkout master git branch –d <branch_name>
The output confirms that “first-branch” has been deleted.
You now know how to create branches in Git. Branches can be used to test-optional features before integrating them. Use small, short-term branches, so that the branch doesn’t stray too far from the central project.