I have both a master and development branch in my Git repo, but I want to delete the master branch locally so I don’t accidentally commit to it again, it’s happened before. I tried using git branch -d master, but the branch still appears in the GitHub Desktop app, which is confusing.
What’s the correct way to use git delete local branch so it fully removes the branch from my local environment without affecting the remote repo? And is it normal for GitHub Desktop to still show it even after it’s been deleted locally?
I’ve dealt with this issue a lot, especially when I’m cleaning up my local setup to avoid accidentally committing to branches like master. First, let me say that git delete local branch
, which you’re doing with git branch -d master
, on’t be able to remove it, so first, make sure you’ve switched to another branch (like development
). Then you can run:
git branch -d master
If Git tells you the branch isn’t fully merged, but you’re sure you want to delete it, just force it:
git branch -D master
This will only remove it locally, it doesn’t touch the remote repo at all unless you specifically push a delete command. So don’t worry about affecting anything on GitHub.
I’ve encountered the same thing when cleaning up branches, and yes, GitHub Desktop can be a bit confusing. The reason you’re still seeing the branch in GitHub Desktop is because it often caches branch data or syncs with the remote repository, showing branches that are still tracked remotely. Deleting it locally via the command line, like you did with git delete local branch
, won’t automatically remove it from GitHub Desktop if it’s still a remote-tracking branch (like origin/master
).
To verify if the branch was removed locally, you can run:
git branch
This will show you only your local branches. And to see what remote branches are still around, use:
git branch -r
This way, you’ll know if it’s still hanging around in the remote.
Exactly! Deleting a local branch with git delete local branch
like you did removes it from your local environment, but GitHub Desktop might still show it if it’s a remote-tracking branch, because GitHub Desktop likes to show all branches, even ones you’re not actively using. After deleting locally, you can clean up any stale remote-tracking references by running:
git fetch --prune
This will clear any local references to remote branches that no longer exist. However, keep in mind, GitHub Desktop will still display remote branches unless you remove them from GitHub itself. So, if you still see origin/master
, it’s just a reference to the remote branch, not your local one.