Git branches

alcb1310

Andres Court

Posted on April 3, 2023

Git branches

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

git branch my_new_branch
git checkout my_new_branch
Enter fullscreen mode Exit fullscreen mode

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

git checkout -b my_new_branch
Enter fullscreen mode Exit fullscreen mode

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

git branch
Enter fullscreen mode Exit fullscreen mode

To delete a branch

git branch -d my_new_branch
Enter fullscreen mode Exit fullscreen mode

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

git branch -D my_new_branch
Enter fullscreen mode Exit fullscreen mode

To rename your current branch

git branch -m branch_new_name
Enter fullscreen mode Exit fullscreen mode

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

git push origin branch_new_name
Enter fullscreen mode Exit fullscreen mode

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.

git checkout development
Enter fullscreen mode Exit fullscreen mode

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

git fetch
git pull origin development
Enter fullscreen mode Exit fullscreen mode

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

git merge branch_new_name
Enter fullscreen mode Exit fullscreen mode

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

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

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

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

Image description

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

Image description

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

Image description

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.

💖 💪 🙅 🚩
alcb1310
Andres Court

Posted on April 3, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Manual Git e Github para iniciantes
webdev Manual Git e Github para iniciantes

December 6, 2023

Git branches
webdev Git branches

April 3, 2023

Git & Github the essentials
webdev Git & Github the essentials

March 27, 2023

Git Pull vs Git Fetch
webdev Git Pull vs Git Fetch

July 16, 2022