How to Work with Git Branches


Git branches are an excellent way to take your project in other directions, test new ideas, explore alternate versions, new features, etc., and even to share code with others to work on debugging issues without committing those issues to the permanent record.

They’re like a parallel universe separate from your primary branch. They’re contained in their own special little place where you can do anything without worrying about destroying everything in your main working branch.

So let’s get started.

Remember that when we set up Git for the first time, we set the name of our default branch to main by using git config --global init.defaultBranch main. (You may see main, trunk, or master depending on the repo you’re contributing to, just something to keep in mind.)

Create a new git branch

To create a new branch and switch to it we can use one of the following options.

In this first option, we’re creating the branch named name_of_branch on line 1 and switching to it in line 2.

git branch name_of_branch
git checkout name_of_branch

Optionally, you can combine the two commands above into one by using the -b flag to create the branch and switch to it at the same time.

git checkout -b name_of_branch

That’s it, your new branch exists and you can start working with it, so let’s explore other useful commands while we work with branches in git:

Working with git branches

See all your branches

git branch

You can use git branch to see a list of all the branches. Also, the branch with the asterisk is the branch you’re currently in. To get out of this screen, type :q.

See current branch

git branch –show-current

You can use the –show-current flag to see the specific branch you’re in without listing every branch like the first command. This is helpful if you have 100s of branches in a large project.

Switch branches

git checkout main

At any time you can switch back to the main branch using git checkout main (since our branch is named main).

git checkout can be used with any branch name to switch to another one, not only the main branch. Simply use git checkout name_of_branch.

Merge a branch

git merge name_of_branch

Once your new feature is done, and you’re ready to move your commits into your main branch you will perform a merge.

git merge name_of_branch will merge your commits from that branch name into the branch you’re currently in, so if you want to merge your branch into main, be sure to git checkout main first.

Share a branch to GitHub

You can also use branches to share code with others on GitHub without committing to your main branch, which can be helpful to debug an issue and not create a permanent record of said issue. The steps are as follows:

  1. git checkout -b name_of_branch to create and switch to your branch.
  2. Next, you’ll make any commits to your branch that you want to share.
  3. git push origin push name_of_branch to push your branch to your GitHub Repo.
  4. Now when you open your repo in GitHub and click on the main dropdown you’ll see your new branch.

That’s all for today. Here are some extra resources:

Leave a Reply

Your email address will not be published. Required fields are marked *