• Python Snacks
  • Posts
  • The Basics of Git Branching for your Python Code

The Basics of Git Branching for your Python Code

Keep your code under source control by leveraging the most popular version control tool out there: Git

If you’ve ever wondered how projects are able to develop multiple features across multiple developers, the secret lies within Git branching.

Branching allows you to work on bugs and features simultaneously with your coworkers (or yourself) while not interfering with the main code base. Let me show you how this is done.

» ICYMI: I’ve been writing a 3-part series on using Git with your Python code. I previously discussed initializing a git repository, adding and committing, pushing and pulling.

Branching explained

The simplest way to think of branching is just like a tree branch: for the tree to grow leaves, it needs to grow branches.

If the tree tried to grow leaves directly on its trunk, it would quickly run out of space, and the structure would become crowded and disorganized.

When the tree branches, each branch provides its own space to grow leaves, ensuring the tree can expand in multiple directions without interfering with its core structure.

Similarly in Git, branches allow your project to grow by creating isolated spaces for new features, experiments, or bug fixes, while keeping the main code base clean and stable.

Let’s take a project with a bunch of releases. The commits may look like this:

However, multiple users identified a bug within the most recent release.

It’s now your job to go in and fix this release. However, you don’t want to be tinkering with this code and messing this line of development up. So, you’ll want to “branch” out from the current code:

Then, you’ll make all the commits that are needed to be able to fix this bug. This can be as simple as one commit:

Once everything is tested and finalized, you can take this commit with the bug fix and put it back into the original code base and release a new version:

Commands to branch

To create a new branch and switch to it:

git branch bug-fix-v2.1.1
git checkout bug-fix-v2.1.1

For the latest versions of git, you can use switch instead:

git switch bug-fix-v2.1.1

Once you make all of your commits, you’re going to want to switch to the branch you want to put the changes into (in most cases, it would be the main branch):

git checkout main

And then run the merge command, which simply takes the code you had in the bug-fix-v2.1.1 branch and puts it into the branch you’re currently on (in this case, main):

git merge bug-fix-v2.1.1

Happy coding!

📧 Join the Python Snacks Newsletter! 🐍

Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:

  1. Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.

  2. Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.

  3. Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.

You can unsubscribe at any time.

Interested in starting a newsletter or a blog?

Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.

If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.

This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.

Reply

or to participate.