Organizing Your Git Branches: Moving Existing Branches to a Folder Structure

Feb 12, 2025

Organizing Your Git Branches: Moving Existing Branches to a Folder Structure

As development progresses, managing numerous Git branches can quickly become messy. Without proper organization, finding and working with branches can become cumbersome, especially in projects with long-running feature developments or multiple contributors. One effective strategy is to structure branches in a folder-like hierarchy, making navigation and management much more convenient.

In this post, I’ll show you how to use a simple script to restructure your existing branches, grouping them under a specific category—such as "2024"—while ensuring that critical branches remain untouched.

Why Structure Branches?

By default, Git lists branches in a flat format, which can become overwhelming as your project scales. Using a hierarchical structure has several advantages:

  • Improved Organization: Categorizing branches helps you quickly find what you're working on.
  • Better Maintenance: Easier to clean up old branches when they're grouped logically.
  • Reduced Confusion: Avoids clutter by keeping related branches together.

Automating the Branch Renaming Process

Instead of manually renaming each branch, we can use a script to automate the process. The following script moves existing branches under the "2024/" prefix while skipping critical branches like master and dependabot/*.

The Script

for branch in $(git branch --format="%(refname:short)"); do  if [[ "$branch" == "master" || "$branch" == 2023/* || "$branch" == dependabot/* ]]; then    echo "Skipping branch: $branch"    continue  fi  new_branch="2024/$branch"    echo "Renaming branch $branch to $new_branch"  git branch -m "$branch" "$new_branch"done

Explanation

  1. List Branches: git branch --format="%(refname:short)" retrieves all local branches.
  2. Skip Important Branches: The script ensures that master, dependabot/*, and previously categorized branches (2023/*) are not modified.
  3. Rename Branches: The git branch -m command renames each selected branch by prefixing it with "2024/".

Running the Script

Save the script as rename-branches.sh and run it inside your Git repository:

bash rename-branches.sh

After execution, your branches will be neatly grouped under 2024/.

Verifying the Changes

To confirm the restructuring, run:

git branch

You should see something like this:

2024/feature-login2024/bugfix-ui2024/refactor-apimasterdependabot/npm-package-update

Handling Remote Branches

If your repository is shared with others, remember to push the updated branches and clean up old ones:

git push origin --delete old-branch-namegit push origin new-branch-name

Or, if you want to rename branches remotely:

git push origin :old-branch-name new-branch-name

Conclusion

By restructuring your Git branches into a hierarchical format, you can greatly improve project organization and simplify branch management. This approach is especially useful for long-term projects and large teams where branch clutter can become an issue.

Would you structure your branches this way? Let me know your thoughts in the comments! 🚀