Navigating Git Branches Like a Pro: The Git Branch Command

I’m a code mapache, subtly mastering the forest of Git branches. Let’s talk git branch, remote vs. local branches, and git prune.

Git Branch Command As a professional software developer, you might have noticed a few similarities between your codebase and a community of mapaches 🦝. They’re both intricately organized, require patience to understand, and have a lot of hidden treasures waiting to be discovered.

In this article, I’m going to take you through the woodland pathways of Git branches, using both the command line and GitHub Desktop. And yes, our guide through this adventure will be a code mapache, because (hissing)!

The Basics of Branching in Git

Branching is an integral part of Git version control. It allows developers to work on different features simultaneously without disrupting the main line of development, which is usually the main branch (or sometimes develop).

# Let's create a new mapache branch!
git branch "new_mapache_branch"

# Let's switch to (or "check out") our new branch.
git checkout "new_mapache_branch"

After doing the work on this new branch, it can be merged back into the main (or develop) branch, either from the command line or in a PR.

# Switch to the develop branch.
git checkout "develop"

# Merge the mapache branch into develop.
git merge "new_mapache_branch"

But you already know this, right? So let’s scurry forward like our mapache friends to discuss some other branch management tips and tricks.

Beyond the Basics: Branch Management with Git

Some features of the git branch command that are less obvious are the ability to list, rename, and delete branches with ease.

# List all branches:
git branch

# Rename a branch:
git branch -m "old_mapache_branch" "new_mapache_branch"

# Delete a local mapache branch:
git branch -d "mapache_branch_to_delete"

# Delete a remote mapache branch:
git branch -d -r "remote_mapache_branch_to_delete"

The -m option is used to rename a branch and -d to delete it. But proceed with caution! Once a branch is deleted, it can’t be easily recovered.

Additionally, you can also create a new branch and switch to it in one command, but you don’t use git branch. Instead, try git checkout -b:

# Create a new branch and switch to it at the same time:
git checkout -b "another_mapache_branch"

Just like a mapache swiftly climbing a tree, this command helps you start working on a new branch without breaking a sweat.

Viewing & Deleting Branches with GitHub Desktop

The command line is excellent, but sometimes, having a visual interface makes all the difference. Enter GitHub Desktop, my favorite Git GUI.

Open your repository in GitHub Desktop and click the Current Branch button, which is the middle button of the top row of actions in the GUI. Here, you’ll see a list of all your branches. Clicking a branch will automatically check it out, and creating a new branch is just as easy.

To create a new branch in GitHub Desktop, click the Branch menu at the top, then choose the New branch… menu option. If you’re not on the default branch (typically main) then you’ll be given a choice of whether you’d like to branch from your current branch or from the default (main).

Now type the name of the new branch in the field , and hit the “Create branch” button. It will automatically check out, like git checkout -b.

To delete a branch, go back to the list of all branches by clicking on the Current Branch button at top again. Right-click on a given branch, then choose Delete..., confirm, and poof! Your branch is deleted, just like a mapache vanishing in the underbrush, or the git branch -d command.

Using Remote Git Branches For Code Collaboration

In our Git adventure, the concept of managing remote branches remains essential. While we can use Git locally, without sharing code to a remote “origin” server, we’re generally collaborating with other professional devs.

Remote branches provide references (pointers) to the state of branches in your remote repositories, on the “origin server” (the “remote repository”). Remote branches act sort of like bookmarks to remind you where the branches on your remote repos were the last time you connected to them.

You can fetch a remote branch like this:

# Fetch a remote branch from the origin server (remote repository)
git fetch origin "remote_mapache_branch"

To start working on that remote branch, you need to check it out and create a local branch by using the git checkout -b command from before:

# Checkout a remote branch as a local branch using a "tracking branch"
git checkout -b "local_mapache_branch" origin/"remote_mapache_branch"

Git then sets up a “tracking branch” (also called an “upstream branch”) that will link your local branch and its remote counterpart.

To push your local branch to the remote repository, use:

# Push your local branch to origin (the remote repository)
git push origin "local_mapache_branch"

# As a "tracking branch" this will update origin/"remote_mapache_branch"

This is similar to a code mapache marking its territory: you’re letting others know that you’ve been there and have potentially made changes. 🦝

Looping back to the topic of deleting branches with GitHub Desktop, if you try deleting a local branch that tracks a remote branch, then you’ll be asked if you only want to delete the local branch or delete the remote as well.

Pruning Old Branches: Git Fetch Vs. Branch Delete

Just like a good mapache cleans up its habitat after spending all day eating trash, Git also allows us to tidy up old, unused branches. 🧹

When your code collaborators delete branches from the remote repository, your local repository does not remove its reference to these branches.

Over time, these outdated references (also called “dangling branches”) will start to clutter up your repository.

To remove these references, Git provides us with the --prune option.

# prune branches
git fetch --prune

The --prune option removes any remote-tracking references (meaning notes in your local repository) to remote branches that have been deleted.

I also want to share a “safe way to show the delete commands only for local branches merged into master with the last commit over a month ago”:

for k in $(git branch --format="%(refname:short)" --merged master); do 
if (($(git log -1 --since='1 month ago' -s $k|wc -l)==0)); then
echo git branch -d $k
fi
done

This is from a Stack Overflow answer by estani, and it’s something I’ve personally found to be useful in my work managing an engineering team.

Since the command only echos (prints to the screen) the git branch -d commands, it’s a safe way to preview the commands before running them.

A commenter named Asaf Pinhassi pointed out that you can add -r to this command to list the remote branches, which is a good tip to add here!

And there we have it, folks! I’m all branched out.

We’ve explored the thicket of Git branches with the agility of mapaches 🦝. We’ve seen how to create, rename, and delete branches; how to use GitHub Desktop for visual branch management; and how to prune old branches.

Remember, mastering Git branches is like understanding a mapache: it requires patience and time. But once you do, you’ll find branches to be a versatile tool that can help streamline your development and collaboration.

Happy coding! 🌲

Read More

More From Author

How to Set Up VS Code Like a Pro in Just 5 Minutes

Top 10 Advanced VS Code Settings for Senior Developers

Leave a Reply

Your email address will not be published. Required fields are marked *