Home Learn Linux Top 10 essential branch command usages in Linux

Top 10 essential branch command usages in Linux

The 'branch' command, commonly associated with Git in the Linux environment, is crucial for handling codebases. This guide will walk you through the top 10 usages, enabling you to better manage and navigate your repositories.

by John Horan
git branch command

Git is an essential tool for developers as it offers unmatched flexibility and functionality for managing different versions of codebases. One of Git’s fundamental features is its ability to create branches, which enables developers to work on different versions of a project simultaneously without affecting the main or “master” branch.

In this article, I will delve deep into ten distinct ways to use the “git” command in Linux. Believe me, some of these have been lifesavers for me! Below is a summary of the commands that I will be discussing in detail in this article.

Git Branch commands summary

Command Description
git branch Display all local branches
git branch -a Display all branches (local + remote)
git branch [branch-name] Create a new branch
git branch -d [branch-name] Delete a local branch
git branch -m [old-name] [new-name] Rename a branch
git branch -v Display the last commit on each branch
git branch --contains [commit] Filter branches by commit
git branch -u [remote/branch] Track a new remote branch
git branch --merged View branches that have been merged into the current
git branch --no-merged View branches that haven’t been merged yet
git checkout -b [branch-name] Create a branch and switch to it simultaneously

10 Git Branch commands in Linux

Now let’s dig deeper into each of the branch commands.

1. Displaying all local branches

Syntax: git branch

This simple command is the quickest way to list all the local branches in your repository. I often use this command to get an overview of my work.

Output:

* master
  feature/login
  fix/button-styling

The asterisk (*) indicates the current branch you are on.

2. Displaying all branches (local + remote)

Syntax: git branch -a

There have been instances when I wanted a full picture, including the remote branches. This command helps you achieve that.

Output:

* master
  feature/login
  fix/button-styling
  remotes/origin/master
  remotes/origin/feature/navbar

It’s always a delight to see the harmony between local and remote branches!

3. Creating a new branch

Syntax: git branch [branch-name]

I find this extremely useful when I start working on a new feature or a bug fix. It ensures my master branch remains untouched.

Output:

$ git branch feature/signup
$ git branch
* master
  feature/login
  feature/signup
  fix/button-styling

Creating branches like this keeps your code organized and makes collaboration a breeze.

4. Deleting a local branch

Syntax: git branch -d [branch-name]

After merging my changes, I like to keep things tidy. This command helps me get rid of branches I no longer need.

Output:

$ git branch -d feature/signup
Deleted branch feature/signup.

Just a heads-up: if the branch has changes not yet merged, Git will prevent the deletion. For such cases, use -D but with caution.

5. Renaming a branch

Syntax: git branch -m [old-name] [new-name]

Mistakes happen! I once named a branch “feautre” instead of “feature”. This command came to the rescue.

Output:

$ git branch -m feautre/login feature/login

Typing errors, begone!

6. Displaying the last commit on each branch

Syntax: git branch -v

For those times when I wanted a quick peek at the last commit without switching branches, this was a game changer.

Output:

* master 7f8d3d9 Fix readme file
  feature/login 890e3d5 Add login validation
  fix/button-styling 9a2e4b0 Update button colors

7. Filtering branches by commit

Syntax: git branch --contains [commit]

Want to know which branches contain a specific commit? Here’s how. It’s not an everyday command, but on some occasions, especially during troubleshooting, it’s been invaluable.

Output:

$ git branch --contains 890e3d5
* master
  feature/login

8. Tracking a new remote branch

Syntax: git branch -u [remote/branch]

Imagine you’ve pushed a new branch to the remote repository and you want to set up a tracking relationship. This is your command.

Output:

$ git branch -u origin/feature/navbar
Branch 'feature/navbar' set up to track remote branch 'feature/navbar' from 'origin'.

It gives a sense of connection between local and remote branches, doesn’t it?

9. Viewing merged and unmerged branches

Syntax:

  • git branch --merged
  • git branch --no-merged

Sometimes, especially in bigger projects, I want to see which branches have been merged into the current branch and which haven’t. These two commands do just that.

Output:

$ git branch --merged
* master
  feature/login

$ git branch --no-merged
  fix/button-styling

10. Creating a branch and switching to it simultaneously

Syntax: git checkout -b [branch-name]

While technically a checkout command, it’s closely related to our branch discussion. This is my favorite when I want to create and switch to a new branch in one go.

Output:

$ git checkout -b feature/logout
Switched to a new branch 'feature/logout'

A neat shortcut for the multitaskers!

FAQ on Git branch command

Q1: Why is branching in Git important?
A: Branching in Git allows developers to work on different features, experiments, or bug fixes without affecting the main codebase. It promotes parallel development, ensuring that the main (often referred to as ‘master’ or ‘main’) branch remains stable and deployable at all times.

Q2: How can I see all the commits on a particular branch?
A: You can use git log [branch-name] to see a detailed log of commits on a particular branch. It’s a great way to track progress and review changes.

Q3: I mistakenly deleted a branch. Can I recover it?
A: Yes, if you’ve recently deleted a branch, you can typically recover it. One method is by finding the last commit of the branch using git reflog and then creating a new branch pointing to that commit.

Q4: What’s the difference between a local branch and a remote branch?
A: A local branch exists only on your local machine, whereas a remote branch exists on a remote repository (like GitHub or Bitbucket). Local branches can be pushed to a remote repository, turning them into remote branches that others can see and collaborate on.

Q5: Can I have two branches with the same name, one local and one remote?
A: Yes, it’s possible to have a local branch and a remote branch with the same name. However, they operate independently. It’s a common practice, but you should ensure that they stay synchronized to avoid confusion and conflicts.

Q6: How can I merge two branches?
A: You can use the git merge [branch-name] command. First, switch to the branch you want to merge into (usually the main branch) and then run the merge command with the name of the branch you want to merge.

Q7: What should I do if I encounter a merge conflict?
A: Merge conflicts arise when changes in one branch overlap with changes in another. Git will highlight these conflicts, and you’ll need to manually resolve them. Once resolved, you can then commit the merged changes. It’s always advisable to carefully review conflicts and, if possible, discuss with team members who made the conflicting changes.

Q8: How frequently should I create branches in my project?
A: It depends on your project’s requirements and your workflow. A common practice is to create a new branch for every new feature, experiment, or bug fix. This keeps the main branch clean and deployable.

Q9: Can I rename a remote branch?
A: Directly renaming a remote branch isn’t straightforward. Typically, you’d rename the local branch, push it to the remote as a new branch, and then delete the old remote branch.

Q10: How do I know which branch I am currently on?
A: The command git branch will list all local branches, and the current branch will be indicated with an asterisk (*) next to it.

Final thoughts

Branching in Git is undeniably one of the core strengths of modern software development, enabling parallel workflows, collaborative efforts, and isolated experimentation without risking the integrity of the main project. As we’ve explored various commands and addressed some common questions, it’s clear that mastering Git branching techniques is an invaluable skill for any developer.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.