04 Git: Branches Are Breathtaking 🔥
Tags: github, git, webdev
Published: 2021-01-28 00:51:17

### What Are Branches?

Branches are a way of diverging from the main development line and keep working on the project without affecting the main development line ("main development line" refers to mainly `master` or `main` branch). That means every branch has its own commits which can be different from other branches and they do not meddle in each others stream unless we manually perform operations like merging, rebasing or cherry-picking.

### What Are Branches Used For?

As branches allow us to work without messing up main development line, branches are used for adding new features, solving bugs, hot fixing a critical bug etc. Basically everything that needs to be isolated at the time of development, uses branching.

We can have a new branch for every ongoing task, commit changes in that branch and finally when the work is done, we can merge that branch into main development line(in our case `master` branch).

In the [previous posts in this series](https://webdevjeet.me/tag/git-an-essential-tool-for-devs/), you might've noticed that there is `master` written in the terminal screenshots. That stands for which branch we're currently working on.

<figure>![master Is Name Of Current Branch](https://dev-to-uploads.s3.amazonaws.com/i/s96v69o4gln5hil8mu4v.jpg)

<figcaption>`master` Is Name Of Current Branch</figcaption>

</figure>

Now that we've discussed what Git Branches are and what they're used for, we can move on to how to use them. There are two commands that we generally use to create and navigate through the branches which are `git branch` and `git checkout`. The latter one can also be used with commits, but for now, we'll use it with branches.

### Command: `git branch`

Running `git branch` gives you a list of existing branches in the project and the current branch is shown with a `*` at the start of its name in the list. Here is [the](https://github.com/sereneinserenade/first-repo) repository that I am working on for [this series](https://webdevjeet.me/tag/git-an-essential-tool-for-devs/).

Running `git branch <name-of-branch>` will create a new branch.

<figure>![git branch "new-list"](https://dev-to-uploads.s3.amazonaws.com/i/f47vtq4a8jzxl9n4hykm.jpg)

<figcaption>`git branch "new-list"`</figcaption>

</figure>

### Command: `git checkout <name-of-branch>`

Running `git checkout <name-of-branch>` allows us to navigate to the branch name that we provided.

<figure>![git checkout new-list](https://dev-to-uploads.s3.amazonaws.com/i/4guc8lkk0qa9ruj1aay7.jpg)

<figcaption>`git checkout new-list`</figcaption>

</figure>

We see that running `git checkout new-list` switched the branch and when we ran `git branch | cat`, the `*` is now with `new-list` which means that `new-list` has become our current branch.

Now let's add a new list of friends in this branch. Since blog posts and books and are our biggest friends, we're gonna add them before everything.

<figure class="kg-card kg-image-card">![](https://dev-to-uploads.s3.amazonaws.com/i/v2cpbzvhw8icjwjzmo3n.jpg)</figure>

After creating the file and writing to it, we can finally commit that file and push it.

<figure>![git add friends.md && git commit -m "added friends.md" and git push](https://dev-to-uploads.s3.amazonaws.com/i/6ngea6d5lfyfiab34sb6.jpg)

<figcaption>`git add friends.md && git commit -m "added friends.md"` and `git push`</figcaption>

</figure>

We see that there was a fatal error when we ran `git push`. The error put in simple words means that the new branch named `new-list` doesn't exist in GitHub(remote). And it shows us a suggestion at the end to run to create this branch in GitHub, which is `git push --set-upstream origin new-list`.

<figure>![git push --set-upstream origin new-list](https://dev-to-uploads.s3.amazonaws.com/i/lozp0ngfc2naptmnzput.jpg)

<figcaption>`git push --set-upstream origin new-list`</figcaption>

</figure>

The command `git push --set-upstream origin new-list` is equivalent to creating a new branch in remote named `origin` and pushing our new local commit to that remote branch.

> The thing to keep in mind is that whenever we create a branch with `git branch`, it is only on our machine, we need to push it to remote manually after creating it.

## Shortcut: `git checkout -b <name-of-new-branch>`

Running `git checkout -b <name-of-new-branch>` will not only create a new branch but also navigate to that new branch so you don't have to run `git branch <name-of-new-branch>` and then `git checkout <name-of-new-branch>`.

<figure>![git checkout -b "checkout-b-test"](https://dev-to-uploads.s3.amazonaws.com/i/hc8edv4bu6g7uficawg1.jpg)

<figcaption>`git checkout -b "checkout-b-test"`</figcaption>

</figure>

**Yay 🧨🧨🧨, now you know how to create branches and switch between them, and a shortcut that does both by default!**

---

This post is originally written [on my blog](https://webdevjeet.me/2021-01-04-git-branches-are-breathtaking/).