Git Branches and how to Use Them

Git is amazing software. For those of you who stumbled across this post without knowing what git is — how did you manage that? But to catch you up to speed it is an open-source version control software that almost all developers use. Its often used in combination with other software like GitHub to keep backups of local code on the web, making it easier to collaborate with other developers or just safe backups of personal code on the internet.

Many new developers learn the basics of git and have even used it repeatedly, maybe typing “git add”, “git commit”, and “git push” hundreds of times into their console. However, another basic piece of git functionality that I think many brand new developers don’t understand quite as much is branches. Branches are important though and the way that git handles branches is actually a big reason as to why it is so popular over other version control software.

Branches are really what makes version control, version control. and that's because you can keep different iterations (or versions) of your code on your machine (or local repository) at a time. Let's look at some good ways to start using branches in helpful ways if you’re someone that hasn’t used them before.

Let's assume for this example you have recently finished and published an app, maybe for the entire development of that app so far you have don't all your work on the main branch of your repository. That’s ok but now is a perfect time to learn branches and implement them into the development of your app. Now that your app is published and people are using it you think of a new feature that would be helpful to users and want to implement it. Before you start working on this new feature in your console run:

git checkout -b feature-name

This command just did a few things at once so let's quickly look at that. The first thing it did was to create a new branch called feature-name ( you could also do this by typing ‘git branch feature-name’). Now you have this new branch on your local repository to save your changes to. The second thing this command did was to switch your current or head branch to this newly created feature-name branch(this could also be done with ‘git switch feature-name’ if you had already created the feature-name’ branch). For now the main branch of your repository and this new feature-name branch are identical, however, as we make changes now the feature-name branch will be updated while the main branch will continue to work as you had designed it without fear of introducing new bugs or other complications.

Now you can use git the same way you have been by adding your commits to the staging area, committing them, and then pushing them to your remote repository — with one exception. The first time you push to your remote repository you will want to use the command ‘git push -u origin feature-name’. This will push your newly created branch to your remote repository and set your local repository up to track that branch so you can use the commands git push and git pull the same as you were before.

The last step of the process here is when you're finished with the feature, or whenever you feel ready to add this feature to the main code. Now its time to merge the 2 branches. To merge your feature-name branch into your main branch simply switch back to your main branch with ‘git switch main’, or ‘git checkout main’, and then ‘git merge feature-name’. After this is done your feature-name branch with be merged into your main code and you can push that to your remote repository or wherever you are hosting the code for your app!

If the basics of branching we talked about here were interesting to you and you want to learn more I would recommend looking into some branching strategies to see how other developers are using git to keep track of their work.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store