Git Branches in Five Minutes
Tags: beginners, git
Published: 2020-09-08 18:06:31

Let's talk about branches. We've touched on these _very briefly_ in previous posts in this series, but let's look at them on their own. (And do it in five minutes!)

_Hi: notes for absolute beginners may show up like this._

#### Jump To:

- [Overview](#overview)
- [Making a Branch](#branch)
- [Pull Requests](#pull)
- [Cleanup](#cleanup)

<a name="overview"></a>

#### Overview

In Git, branches are used to _isolate changes_ from the rest of the code base.

This way, you can keep the spaghetti code you're using to build a new feature or fix a bug to yourself until you're ready to merge it with the main code.

The _master_ branch is typically considered the _working_ branch, which means that any changes you make there may effect anyone - or anything - using the code. Not where you want to play around with a new feature!

Here we go:

<a name="branch"></a>

#### Making a Branch

For the rest of this I'm going to assume you're using <a href="https://github.com/">GitHub</a>, but there are lots of other Git hosting services out there. Remember, Git and GitHub are two different things.

First, you need to <a href="https://dev.to/afteralec/creating-a-new-git-repository-for-the-absolute-beginner-25bn">create your repository</a> and navigate there in your terminal.

You can see which branch you're currently in by doing:

`git status`

You should see something like:

```shell
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
```

_If you don't, go back and make sure that your repository is pointed at the correct remote_

Now, make a new branch by doing:

`git checkout -b new-feature`

_Remember flags? `-b` is saying "make a **new** branch!_

You should now see:

```shell
Switched to a new branch 'new-feature'
```

You've now created a new branch!

Now, make some changes. I'm just going to create a file called `README.md`.

_You can create a new file with `touch README.md` in your terminal._

Now, do:
`git add .` and `git commit -m "Adds README"`
_This adds everything in the current directory `.` and commits the changes. <a href="https://dev.to/afteralec/creating-a-new-git-repository-for-the-absolute-beginner-25bn">More here.</a>_

You should see:

```shell
[new-feature (root-commit) e18e06c] Adds README
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md
```

Notice that the commit message went under the branch `new-feature` in the first line of that message.

Now, push to the _remote_ branch:

`git push origin new-feature`

You'll see something like:

```shell
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 216 bytes | 216.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:afteralec/git-blog.git
 * [new branch]      new-feature -> new-feature
```

Done! Pretty easy, right?


#### Pull requests

In a real project, you'd continue committing and pushing to your branch until your code was ready to see the light of day. _(Or, at least, be merged with the main code of the project in the_ master _branch!)_

Typically, this is done using a pull request.

<a href="https://guides.github.com/activities/hello-world/#pr">GitHub has a great walk-through on it here</a>, but again, use whichever platform you've been using until now.

With GitHub, it's pretty easy:

1. Open the pull request from the repository's page. Click the _pull requests_ tab at the top and then **new pull request**.

2. Follow the prompts and complete the form

3. Open the pull request!

_In this example, as the repository owner, you'll be able to merge your own pull request - this isn't always the case!_

For our purposes, go ahead and merge your changes.

#### Cleanup

From here, you have two options:

1. Delete your branch.
2. Merge the new master code into your branch.

For this, go ahead and delete the branch. Our new feature (the README file) has been added, and there's nothing else to do with it for now!

Back in your terminal (in the _local repository_) do:

`git checkout master`

_Without the -b flag, this moves you around from branch to branch_

and then:

`git pull`

_This part's important - remember, you made changes in the_ new-feature _branch, not the_ master _branch, so this brings you back up-to-speed with the current master code._

You can also delete your branch locally!

To see your branches, do:

`git branch`

_Flags again! You can add `-r` to see only the_ remote _branches, and add `-a-` to see_ all _branches._

Do:

`git branch -D new-feature`

You should see something like:

```shell
Deleted branch new-feature (was e18e06c).
```

This has been Git Branches in (Less Than?) Five Minutes.

If this helped you, you have something to add, or you or want to tell me why you think birds aren't real, leave a comment below.