Branching is a core feature in Git. It is how you can work on a particular feature or software component without breaking your code.
It is the opportunity for you as a developer to make significant changes in your source code and choose what you’d want to do with the changes. Either way, you may merge them into or discard them from your entire project.
If you have interacted with Git, you may have already noticed that there are multiple ways to create branches. If you are an upcoming developer and haven’t encountered Git yet, knowing how to use version control tools will save you significant time; if not, make your developer experience interesting.
This post is tactically geared to help you create Git branches smoothly as part of your defined workflow. In the end, you’ll have acquired solid skills that you can use to maintain your GitHub repositories.
Note: If you are a new developer, please check out how to set up your GitHub account before moving forward. If you have already done this, move to the how-to section. However, a refresher would be great to solidify your learning.
What are Git Branches?
A branch in Git implies a version of your repository that is divergent from your main project (available in all modern version control systems). Simply put, you are stepping aside from your main line of development and working without messing with the original line.
Many available version control software (VCS) tools use this technique which involves creating a new copy of your source code directory. When dealing with larger projects, replicating the source code may take some time. On the bright side of things, it is your chance to experiment with changes in your code.
Git’s branching model is considered a “killer feature” and places Git at a level of its own among other tools in the VCS community. The Git operation model is simple and makes branching operations nearly instant, and also switching through a couple of several. If you master the Git branching model, you unlock powerful features and tools that could boost your development skills. So, what’s the deal with branches?
Why You Need Git Branches
Git branches play a key role in the Git version control system. Here are some reasons why you need Git branches:
✅ Parallel Development – Modern software is complex, and many developers often work together to build the software. Branches allow different developers to work on different features. In other cases, it may be fixing bugs without work collisions. Each branch is a gateway to a new line of development. You can easily switch through branches while working on particular features.
✅ Collaboration – Git branches allow you to work with other developers on a common code base. You can create new branches, make code changes and push them to the shared repository, where other developers can review your contributions before merging them into the main branch.
✅ Experimentation – You can use git branches to test new features before merging to and without affecting the main branch. It is your chance to try a new approach to solving a problem. And if the new methods in your code work correctly, you can merge them into the main branch.
✅ Release management – You can use branches to manage releases. You can create each branch tied to a specific release in your working project. This allows you to fix bugs and make changes without affecting the main development branch.
✅ Versioning – You can use Git branches for versioning; in this case, each branch will represent a new version of your software. As a developer, it is best to use branches for each software release and track changes, which you use to manage different codebase versions.
Getting Started with Git – A Refresher
Now that you understand the “why,” it’s time to take a new shift and attend to the “how.”
Moving forward, you should have already set up your GitHub account. If you still need to, please do so. This section is hands-on.
The git checkoutcommand in Git entails switching between different versions of your target entity. In some online developer communities, the term ‘checking out’ refers to the execution of the checkout command. This command operates on three entities; branches, files, and commits.
Checking Out a Branch
You can use the git branch command to create branches that you navigate through using git checkout. When you checkout a branch, you update the files in your working directory to match the version stored there.
In other words, you are telling Git to record all your commits in the branch (changing the line of development). Using dedicated branches for new features in your development is a shift from the old subversion (SVN) workflow and makes it easy to work with code in all the instances highlighted in the Why you need branches section.
The command git checkout should not be confused with git clone. The first is used to switch between code versions, while the latter fetches code from a remote repository.
Using Existing Branches
If the repository you’re working on has existing branches, you can visualize them on your command line interface using the command git branch.
The available branches are listed, and the green one is the one you are currently working on, assuming you are using the Windows operating system and Visual Studio Code. To switch between branches, use git checkout branchname. The phrase ‘branchname’ stands for your branch name, and in your operations, you could follow any naming convention.
Creating Git Branches
Assume you are in the middle of your software development and would like to add a new feature. The best way to approach this is to create a new branch using the ‘git branch‘.
Practically, this is what you key in the command line:
git branch branchname
This means you have created a branch off the main/master branch (in most cases, this is where you run your live project). The name of your new branch, in this case, is ‘branchname.’
To switch to your new branch, you use git checkout ; see below:
git checkout branchname
If you are a developer, who likes to save time like me, you can create a branch and immediately switch to it by using ‘git checkout‘ with an argument ‘-b‘ followed by your branch name. Practically, you could have just done this to get similar results as in our previous steps, see:
git checkout -b branchname
The parameter ‘-b‘ tell Git to run the git branch right before checking it out. Let’s see other techniques that you can use to create git branches.
Let’s explore more techniques that you can use to create branches based on your needs:
#1. Creating a Branch From your Current Branch
If you want to create a new branch based on your current one, the best method is to use our newly acquired skills:
git checkout -b <branchname>
This method automatically creates and switches you to the new branch. To confirm that you are on the new branch, your terminal should display the message – switched to a new branch ‘branchname.’
If you are a new developer, you should be keen to key in the commands on your console without the brace (<>). They are vital for illustration and explanations and should not be confused or used wrongly.
#2. Creating a Branch from a Different Branch
You can create a new branch based on another existing branch by adding that branch name as your starting point. Here is the command:
git branch <new branch> <base branch>
And in a practical case, it would be:
git branch new-branch branchname
This implies that ‘new branch‘ is our new branch and ‘branchname‘ is our base (foundation) branch.
#3. Creating a Branch from a Commit
If you want to base your new branch from a commit (and not a branch), you need to provide the commit hash as your starting point. And to find the commit hash, which you’re creating the branch, run git log.
The commit hash is typically a long string of characters starting with ‘commit’. With the commit hash, you can create it by running the command:
git branch <branch name> <commit-hash>
You can then switch to your new branch by checking it out.
#4. Creating a Branch from a Tag
To create a branch from a specific tag, find the tag name from which you’d want to create a branch. Run the command git tag to list all available tags in your repository.
Once you have identified the tag name, run git branch <new branch> <tag name> , after which you can switch to your new branch and start making code changes.
#5. Creating a Branch Using Detached HEAD State
You can use a detached HEAD state to create new branches that start from a specific commit without switching to that branch immediately. This technique is helpful when you want to experiment with your new changes without affecting the branch you are working on. Begin by finding the commit hash you’d want to create the branch- use git log.
With the commit hash, run: git checkout <commit hash>. This command implies that you are in a detached HEAD state, meaning you’re not currently on a branch but pointing to a specific commit. Next, you can use git branch <branch name> to create a branch based on your current commit.
#6. Create a Branch from a Remote Branch
Begin by creating a local branch. By now, you should be good at using: git checkout -b <branchname>. The remote branch is created automatically when you push the locally created one to the remote repository.
You push a branch to remote by: git push origin <branch name> In this command, ‘origin‘ stands for the remote repo you are pushing changes to and creating branches at. You can replace it with the name of your remote repo; it works perfectly fine.
#7. Creating a Branch in a Remote Repository
To create a branch in a remote repository, get the latest changes from your remote repo by running the command git fetch. After getting the latest updates, you can create a new branch by checking it out. And after checking out, you can push the new changes by:
git push -u <remote repo> <new branch name>
In your operations, it would be best to use origin (like in how you created a branch from a remote branch). It saves you time and reduces the chances of an error typing your remote repo names.
GitHub Online Interface
All tasks done on the command line can be replicated using the GitHub Online interface. To create a new branch, head to your project repository page and check for branches on the top left edge – often master/main.
If you click on it, a list of your available branches will be shown below, a text field you can use to find or create a branch.
To create a branch, type its name in the text field. The online interface is automated and allows you to create branches from specific elements like tags, branches, and commits. And if you are new to branching, checking out GitHub’s branch docs could save you troubles in future developer endeavors.
Having learned several techniques to create Git branches, you can now incorporate your newly acquired skills in your software development with a smooth Git workflow.
The best branching option will rely heavily on your team: workflow criteria and event instances. For instance, if you are asking for a collaborator’s input, they can create branches remotely and chip invaluable contributions.
You have seen how Git branching provides you with a powerful mechanism to work more effectively and efficiently on software projects. And while there are other Git control options, this post has shown you how to navigate your way with Git branches on the command line, and I make you feel more confident using Git.
Having solidified your art in creating branches, you’d be more excited to check out our guide on deleting GitHub branches.
Have you ever wondered how it is possible that every time your product owner brings in some new feature topic, the team response is they need to investigate technical possibilities and create some form of design before they can be sure how to develop that feature? Then that’s most likely because you have no Architecture Runway in place.
Google Docs does a great job of keeping things simple. The default page setup works great for most documents, and common formatting options are right on the toolbar. However, when you need to do some advanced formatting, you’ll need to dig a little deeper.