Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Git Bonsai, or Keeping Your Branches Well Pruned

March 26, 2014
Steve Milner Sam Van Oort
Related topics:
DevOps
Related products:
Red Hat OpenShift

Share:

    Today, we’ll share a small victory in our DevOps journey at Red Hat IT. This cross-team collaboration has saved our IT organization some headaches and wasted time. We open-sourced the code, hoping it can help you, too.

    The Dev problem, from Sam Van Oort:

    Old, pruned git branches are sometimes re-created by accident, making a mess for our developers.

     

    Code repositories are the final resting place for code, acting as equal parts bank vault, museum, and graveyard. Unlike a vault, content is almost always added faster than it is removed, but much like a graveyard, there is a definite miasma when a repository becomes too full. That smell is developer frustration, as they have to search through dozens, or eventually, hundreds of branches to find the one they want.

    We've had sporadic cases where branches did not get merged into masters (and sometimes fixes were overwritten in later releases) and have wasted collectively hundreds of developer hours on "which branch is this in?" exchanges.

    This situation becomes worse when dealing with a configuration management system like Chef or Puppet. In Red Hat IT, Puppet environments are also defined by branch names, and there's a hierarchy in which branch they look to for code. We had a case where a branch used in testing new code was re-pushed by accident, breaking the ability to create new nodes in that dev environment, and greatly puzzling two developers.

    The solution is to periodically prune branches and throw out content which is no longer needed. But, there’s a big problem: git provides one very dangerous command that can quickly undo all your careful pruning work: "git push." One developer that accidentally runs this (rather than “git push origin branchname”) can immediately recreate all the branches that were so carefully removed.

    The Ops collaboration, from Steve Milner:

    Hey, we have pre-receive hooks in git. We can solve this, and prevent yet another “did I do that?!” moment.

    To address this challenge, the Inception team put together a free, open source git branch blacklisting tool that adds shell commands to “blacklist” branches, and has git hooks to prevent accidental re-pushes.

    Two approaches originally came to mind while thinking about implementation of a blacklisting tool: an in-repo blacklist file and a server-side configuration blacklist. After mulling over the two approaches we decided that having an in-repo blacklist file may lead to developers accidentally merging or overwriting each others’ blacklisted branches and that would negate the tool’s purpose! Instead, the idea of using the built in git config system to house a master list of blacklisted branches on the git origin was chosen. Since all of our git repositories utilize ssh as the transport it became trivial to use ssh to execute git config on the server side allowing for adding, listing, and removing of blacklisted branches. The server side would also house a pre-receive hook which checks the blacklist before allowing the branch to be pushed.

    It was important to make the tool as easy as possible so developers could use it without remembering special conventions. Forcing developers to remember the right remote commands to modify the blacklist was out of the question. The tool also needed to work in a similar fashion as other git tools. To that end, the client side of the tool, wrapping remote execution, was named git-branch-blacklist. By using this name the developer can call it directly or as git branch-blacklist. The tool also follows a simple convention of git branch-blacklist $COMMAND where $COMMAND is add, remove, or list.

    Finally, adding the pre-receive hook needed to be as easy as possible. So, we wrote a hook installer named git-branch-blacklist-install. It simply verifies the repository is set up in such a way the blacklist system will work, and then copies the hook into the right location for said repository.

    The implementation was not without its downsides. It assumes ssh is your transport, so if you use a different transport and disable ssh access, it doesn’t work. Also, the tool is written in shell script and requires common unix tools. If someone wanted to run this tool on Windows they would need to install Cygwin first. That said, the downsides were acceptable as they don’t apply to our use cases.

    The DevOps success:

    Today, Sam’s team has implemented the git-branch-blacklist tool. They’re at 180 and 65 branches (after initial pruning) in their two main repositories, and expect blacklisting to help reduce this over time, saving much developer time and frustration. Nobody has fessed up to git-branch-blacklist saving their hide just yet, but we suspect it’s just a matter of time.

    Last updated: January 10, 2023

    Recent Posts

    • Kubernetes MCP server: AI-powered cluster management

    • Unlocking the power of OpenShift Service Mesh 3

    • Run DialoGPT-small on OpenShift AI for internal model testing

    • Skopeo: The unsung hero of Linux container-tools

    • Automate certificate management in OpenShift

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue