As a developer, keeping your GitHub branches organized is crucial for an efficient workflow. But over time, managing branches can become unwieldy, with old branches cluttering up your repository. Pruning stale and outdated branches keeps things clean and streamlined.
In this comprehensive guide, we’ll explore the ins and outs of deleting GitHub branches like a pro. I’ll be sharing code examples, best practices, tips and tricks, and how to recover deleted branches. By the end, you’ll have the confidence to prune branches without worrying about losing work. Sound good? Let‘s dive in!
Local vs. Remote Branches: Understanding the Difference
Before we delete anything, you need to understand the fundamental difference between local and remote branches.
Local branches live on your machine and reflect the state of your local git repository. They allow you to work independently without impacting the main project.
Remote branches live on GitHub and represent the state of your project on GitHub.com or your GitHub Enterprise instance.
When working on a project, you’ll create local branches to develop features or fix bugs. These will then be pushed to GitHub to share with your team.
Here‘s a quick diagram to illustrate:
GitHub (remote)
|
push / pull
──────────────
──────────────
|
Your Machine (local)
Now here is the key point about deleting branches:
Deleting a branch locally does not delete it on GitHub. And vice versa.
To fully delete a branch, you need to prune it both locally and remotely. This is a common source of confusion!
Alright, now that we‘ve got that covered, let‘s look at how to delete branches locally using Git commands.
Deleting Local Branches from Your Machine
When you want to delete a local branch, the git branch command is your friend. The -d flag will delete a branch:
git branch -d branch_name
For example, to delete the local branch called new-feature:
git branch -d new-feature
Now this branch will be removed from your local repository. Easy!
Here are some key points on deleting local branches:
-
Git prevents you from deleting unmerged branches to avoid losing work. Use
-Dinstead of-dto forcibly delete unmerged branches. -
Local branches can be restored! Git keeps track of deleted branches for about 90 days in something called the reflog.
-
You can also use
git branch -D branch_nameas a shortcut syntax.
Let‘s look at an example. Say you have a local branch named feature-x that you want to remove:
> git branch
main
* feature-x
> git branch -d feature-x
error: The branch ‘feature-x‘ is not fully merged.
If you are sure you want to delete it, run ‘git branch -D feature-x‘.
> git branch -D feature-x
Deleted branch feature-x (was 4d22222).
> git branch
* main
Notice how it prevented deletion without -D since feature-x had not been merged.
Now that you know how to prune local branches, let‘s shift gears to deleting remote branches.
Pruning Branches on GitHub
Being able to delete remote branches on GitHub is just as important as local cleanup.
The git push command is used to update remote branches. We can also use it to delete branches with the --delete flag:
git push origin --delete branch_name
For example:
git push origin --delete new-feature
This will delete the new-feature branch from the remote GitHub repository.
Some key points:
-
This only deletes the branch on GitHub, not locally! Use
git branch -dto delete locally. -
You can also use
git push origin :branch_nameas a shorthand syntax. -
Deleted remote branches can also be recovered on GitHub if they were not merged into main.
-
List remote branches with
git branch -rto confirm deletion.
Alright, time to see this in action. Say we create a branch called experimental:
> git checkout -b experimental
> git push origin experimental
> git branch -r
origin/main
origin/experimental
We now have the experimental branch locally and on GitHub. To delete it remotely:
> git push origin --delete experimental
> git branch -r
origin/main
And…it‘s gone! The remote experimental branch has been deleted.
Now that you know how to delete local and remote branches from the command line, let’s look at using the GitHub interface.
Deleting GitHub Branches from the Web UI
In addition to CLI deletion, you can also prune branches right from GitHub:
-
Navigate to your repository on GitHub
-
Click on "Branches" in the top left
-
Locate the branch you want to delete
-
Click the trash can icon on the far right
-
Confirm branch deletion
And that branch is now removed from GitHub!
Some things to keep in mind:
-
This only deletes the remote branch, your local repository still has it
-
Unmerged remote branches can be restored quickly after accidental deletion
-
You can fetch restored remote branches back to your local clone
Okay, we‘ve covered the techniques – now let‘s move on to best practices for branch pruning.
Best Practices for Branch Deletion
To make branch deletion an effective habit, here are some tips:
-
Delete merged branches – Once a branch is merged into main via a pull request, delete it for a clean workflow.
-
Use pull requests – Using the PR merge strategy makes it easy to see which branches are merged and safe to delete.
-
Delete stale branches – Periodically delete branches that are outdated or haven‘t been worked on in a long time.
-
Check activity first – Review recent branch activity before deleting to avoid deleting in-progress work.
-
Warn teammates – If collaborating, let your team know before deleting shared branches.
-
Set up aliases – Simplify branch deletion with aliases like
git purge-branches. -
Delete ruthlessly – Don‘t be afraid to delete branches liberally to keep your repository organized.
-
Review periodically – Make branch pruning a habit by reviewing your branches weekly or monthly.
By regularly tidying up your branches, you‘ll keep your development environment focused and full of signal instead of noise.
Now that you‘re a deletion pro, let‘s talk about branch recovery.
Recovering Deleted Branches
Mistakes happen. If you delete a branch and realize you need it back, recovery is often possible. Here is how it works:
-
Local branches – Can restore deleted local branches from Git reflog data for ~90 days after deletion.
-
Remote branches – Can be restored on GitHub if unmerged for several days after deletion.
-
Long deleted branches – Cannot be recovered after ~90 days when pruned from local reflog and GitHub.
The key is to act quickly if you need to restore a deleted branch. Use git reflog to find the commit hash of a deleted local branch. Or use GitHub‘s branch restore option.
But after the reflog or GitHub prunes deleted branch refs, they are likely gone for good. So be cautious when deletion branches, just in case.
Final Thoughts on GitHub Branch Deletion
Branching is a vital part of the Git workflow. But balancing branching and branch cleaning is a skill that improves through experience.
Here are some final takeaways on deleting GitHub branches:
- Delete local branches with
git branch -d, force delete with-D - Delete remote branches with
git push origin --delete - Also delete branches in the GitHub UI for complete removal
- Review branches regularly and prune stale/merged ones
- Recover recently deleted branches quickly from reflog or GitHub
- Deleted branch data will eventually be pruned and permanent
And that wraps up this comprehensive guide on deleting branches! I hope you‘ve picked up some useful tips and techniques. Keeping your branches trimmed will take your GitHub skills to the next level.
Now get out there, delete some branches from your repositories, and enjoy the clean Git workflow!