Four examples of how to quickly create a Git branch
The ability to branch and perform risk-free development in isolated programming space is one of Git’s greatest gifts to the programming world. But how do you create a Git branch?
Git branching commands
The four commonly used commands to create a Git branch are:
e.g. git branch alpha-branch
git checkout -b
e.g. git checkout -b beta-branch
e.g. git branch charlie-branch M1
e.g. git branch devo-branch 459615
Simple Git branch creation
The easiest way to branch into Git is to use the branch switch and provide a branch name. The only downside to this approach is that you are not moved to the branch you just created, so further verification is required.
git branch alpha-branch git checkout alpha branch
To avoid having to run the checkout command after branching, you can simply use Git’s checkout switch with the -b flag. The following command will create a Git branch named beta-branch and switch you to the new branch at the same time.
git checkout -b beta
Using the git checkout approach is the most common way for developers to create new Git branches.
Create a branch from the Git tag
Sometimes a developer wants to branch from a commit that has been marked as a milestone or release. To do this, just use the git branch switch and provide the tag name after the new one of the new branch to be created. The following command will create a new Git branch from the M1 tag named charlie-branch:
git branch charlie-branch M1
Create a Git branch from a commit
Likewise, a developer can create a new Git branch from a commit. To do this, use the branch switch and provide the validation ID after the branch name. The following command creates a new Git branch from the commit with ID 4596115:
git branch devo-branch 459615
Git branch lists
Every now and then it’s a good idea to take stock of any Git branches you’ve created. Good development management means you need to prune your workspace every now and then and remove Git branches that are no longer needed. To list the branches, just use the -a or -all options:
git branch -a
And to find out which branch you’re currently on, a quick git status command will do the trick:
The gift of creating and removing lightweight branches is one of the main benefits developers get when moving from traditional version control systems to Git. Never be afraid to create a new Git branch and experiment. If your experiments fail, you can simply delete the branch. And if they are successful, you are just a git merge and you avoid showing all your fellow developers how brilliant you are.