Git branches
Tags: webdev, git, github, tutorial
Published: 2023-04-03 00:35:20


In this blog post we will be making an in-depth review of the git branches models. Git branches are a part of your everyday development process. Each branch is effectively a snapshot
of your changes. Every time you want to update your code, either a new feature, fix a bug, o any other modification to your code independent of the size of it, you can encapsulate the
changes. This approach will make it harder to get unstable code to get merged into your production branch.

## How to create a branch

There are two main ways to create a new branch

```bash
git branch my_new_branch
git checkout my_new_branch
```

This will create a copy of the branch you are in into a new branch called `my_new_branch` and then make that as the active branch.

The other way to create a branch is

```bash
git checkout -b my_new_branch
```

This will have the same effect as the two commands we showed before, I personally like this second way because I can do the same thing but in a single command.

Making a new branch for your changes is important because it will not modify your stable branch and you can make your changes with confidence that you will not break by accident your main branch.

## Branches structure

When you initialize a git repository you will get a first branch that is usually named `main`. This branch is usually the one that is sent to production, so making changes to it is not recommended
because of the potential risks of untested and unstable code be introduced.

Even in small projects, I personally like to create a separate branch called `development` where all of my changes will go into and be able to make the required tests to ensure my code quality before "copying" those into the
main branch.

When working on larger projects, specially when working on a team, I like to create a different branch for each issue I have to implement. The issues can be a feature, fix, refactor, etc.

## Branch commands

To list all of the branches of your repository you can run

```bash
git branch
```

To delete a branch

```bash
git branch -d my_new_branch
```

This is a safe way to delete the branch, because it will not take effect if your branch is unmerged. However if you like to delete a branch even if it is not merged, you should run

```bash
git branch -D my_new_branch
```

To rename your current branch

```bash
git branch -m branch_new_name
```

## Uploading your branch to GitHub

Now that we've created our branch we will like to have it on GitHub, so every member of your team can see it, to do so you need to run

```bash
git push origin branch_new_name
```

## Merging branches locally

Merging is git's way to copy your changes into a single branch. It will combine multiple commits into one unified story. So we can merge our feature branches into development and the development into main.

To merge, we will need to first go to the receiving branch with the `git checkout` command.

```bash
git checkout development
```

Now that we are in the development branch, we need to make sure we have all of the latest remote commits, to do so we need to execute

```bash
git fetch
git pull origin development
```

Finally to actually merge the data, you need to run the following command

```bash
git merge branch_new_name
```

After the branch is merged I highly recommend to delete the feature branch, this will make your code base easier to understand and you will know what features are being worked on.

## Merging in GitHub

Once you have made all of the changes you have to do and committed them, you should push your branch to GitHub as we reviewed earlier in this post.  In GitHub to make a merge is called ***Pull Request***.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lmh00j3qwovy9ayk08xl.png)

To make a Pull Request (PR) in your project GitHub page, you have to go to the Pull Request section, and press the New Pull Request button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/85qt4633eom5n62ywduo.png)

The base branch should always be the branch you want to merge into and the compare branch should be the branch that contains the changes and press the Create Pull Request button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n43ci5cuen0vq661vu9z.png)

Then after you made your tests you can just press the Merge pull request button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6w5y4wgdwo3patg0v4jl.png)

To confirm your merge, you need to press the confirm merge button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mo9bgeom4i76lqubb9no.png)

Finally, as I stated earlier, is important to clean up your merged branches, so you can just press the delete branch button

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jjnjnw95yr7kvvwb94ua.png)

## Conclusions

In this post we reviewed how to work with branches both locally and with GitHub.  One important thing, when working on a solo project, both merging your branches locally or in GitHub are similar, but, when working on a team, is important to use the GitHub method so any time a PR is created it has to be reviewed by a different team member and make sure the changes produces the expected results.