How to Delete the Git Master Branch Safely
Safely remove a local or remote Git master branch. Check for unmerged work, understand force deletion, and clean up stale remote-tracking refs.
To safely delete a local branch named master, switch to another branch and use Git’s merge-checked delete command:
git switch main
git branch -d master
Replace main with the branch you intend to keep. The -d option refuses to remove master if it has commits not merged into its upstream branch, or into HEAD when it has no upstream. To delete the server-side branch on origin, use git push origin --delete master. These commands affect different refs: deleting one does not automatically delete the others.
1. Identify which master branch you mean
Git repositories can have a local branch, a branch on a remote server, and a local remote-tracking ref for that server branch. They may all be named master, but deleting one does not necessarily remove the others.
| What it is | Example | How it is removed |
|---|---|---|
| Local branch | master |
git branch -d master |
| Server-side branch | master on origin |
git push origin --delete master |
| Remote-tracking ref in your local repository | origin/master |
git fetch --prune origin |
Check your current branch and remotes before acting:
git status --short --branch
git branch -vv
git remote -v
git branch -vv shows local branches and their upstreams. git remote -v helps confirm which server is called origin. If your repository uses a different remote name, substitute it in remote commands.
2. Safely delete the local master branch
Step 1: Switch to another branch
You cannot delete the branch checked out in your current worktree. Switch to the branch you intend to keep; if it does not exist yet, create it from the commit you want to preserve.
git switch main
To see available local branches, run git branch --list. If the intended destination exists only on a remote, you can create a local branch that tracks it with git switch --track origin/main, adjusting the names to your repository.
Step 2: Check what would be lost
See whether master has commits absent from the branch you intend to keep:
git log --oneline main..master
Replace main with the comparison branch you chose. This lists commits reachable from master but not from that branch. You can inspect the branch tip and its upstream too:
git show --no-patch --oneline master
git branch -vv
A clean log does not prove that every change you care about is safe to discard: compare against the branch that should contain the work, and review the commits before deleting.
Step 3: Use the merge-checked delete
git branch -d master
Git’s branch documentation says the branch must be fully merged into its upstream, or into HEAD if no upstream was configured. This check makes -d the safe default. It removes the branch reference and its reflog; it does not merge the branch for you.
If Git says the branch is not fully merged
Do not immediately force-delete it. Inspect its unique commits:
git log --oneline main..master
If you need those changes, merge or cherry-pick them onto the intended branch, then retry the safe delete. If you have confirmed that the unmerged work can be discarded, force deletion is:
git branch -D master
-D is the force form of -d; it bypasses the merged-status safeguard. Before using it, record the tip if you may need to refer to it later:
git rev-parse master
3. Delete master from a remote
First verify the remote and branch identity. Deleting from a remote changes the server-side ref, not your local branch:
git remote -v
git ls-remote --heads origin master
If the listed remote and branch are the ones you intend to remove, delete the server-side branch:
git push origin --delete master
Git documents this as deleting the remote ref. A server configuration or hook can reject the request, and a hosting service may enforce its own default-branch or branch-protection rules. Check the repository’s host settings if deletion is rejected; the command cannot override those rules. See the Git push documentation.
Remove the stale remote-tracking ref
After the remote branch is gone, your local repository may still list origin/master. Preview stale refs first:
git remote prune --dry-run origin
If the preview is appropriate, prune them:
git fetch --prune origin
Pruning removes stale remote-tracking references associated with that remote. Review the preview if you use unusual refspecs or tags. Git explains pruning and its dry-run option in the remote documentation.
4. What deletion does—and how recovery works
A branch is a reference to a commit. Deleting a branch removes that reference; it does not instantly rewrite every copy of the repository or guarantee that the underlying commit disappears immediately. The Git user manual describes remote deletion as removing the pointer from the server. Some servers retain objects until garbage collection, but retention varies, so treat recovery as uncertain rather than as a safety plan. See the Git user manual and Pro Git’s remote-branch guide.
Before deleting, preserve the commit ID with git rev-parse master or, for a tracking ref, git rev-parse origin/master. If you recorded the ID and the commit remains available locally, you can create a new branch pointing to it:
git branch recovered-master <commit-id>
Replace <commit-id> with the saved hash. This only works while the commit object is still available in your repository. Do not rely on a universal recovery window.
5. Troubleshooting
| Message or symptom | Likely cause | What to do |
|---|---|---|
Cannot delete branch 'master' checked out |
Your current worktree is on master. |
Switch to another branch, then retry. Check linked worktrees with git worktree list if it still reports that the branch is checked out. |
not fully merged |
Git found commits not merged into the configured upstream or HEAD. |
Inspect with git log main..master, merge or preserve needed commits, and use -D only if discarding the remaining work is deliberate. |
remote ref does not exist |
The branch may already be deleted, the remote name may be wrong, or the branch name may differ. | Check git remote -v and git ls-remote --heads origin. |
| Remote rejects deletion | A server hook, repository policy, protection rule, or default-branch setting may prevent it. | Inspect the host’s repository settings and its error message. Change the applicable policy through the host if you are authorized to do so, then retry. |
origin/master still appears after deletion |
Your local remote-tracking refs have not been pruned. | Run git remote prune --dry-run origin to preview, then git fetch --prune origin. |
| Pruning preview includes unexpected refs | Remote refspecs or tag configuration may affect what is considered stale. | Review the remote configuration and preview before pruning; do not proceed until the listed refs are understood. |
6. Reliability and cost considerations
These Git commands run locally except for git push and git fetch, which contact the named remote. Confirm the remote before push, and inspect the prune preview before cleanup. Git provides no guarantee that a deleted local or remote branch can be recovered indefinitely; preserve important commits under another ref or merge them before deletion. The commands themselves do not require a paid service, though access to a hosted repository and its policies depends on that host.
7. ScreenshotNeo: capture a page without browser setup
If your work also involves capturing a website screenshot—for documentation, a bug report, or a page preview—ScreenshotNeo is a website screenshot API and MCP server for developers. It is separate from Git branch management. One GET request returns a PNG, JPEG, WebP, or PDF. The API accepts parameter names used by other screenshot APIs, which can make switching easier. See the ScreenshotNeo API documentation.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
import requests
r = requests.get(
"https://api.screenshotneo.com/v1/shot",
params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
timeout=90,
)
r.raise_for_status()
with open("shot.webp", "wb") as f:
f.write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`Screenshot request failed: ${res.status}`);
const image = Buffer.from(await res.arrayBuffer());
await import('node:fs/promises').then(fs => fs.writeFile('shot.webp', image));
Cookie and consent banners are accepted and removed before capture, along with supported newsletter popups and chat widgets; each cleanup step can be turned off. Bot checks and CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and responses report the page verdict and billing status in headers. AI agents can use its MCP server tools: take_screenshot, get_page_info, and capture_pdf. Plans include 1,000 screenshots per month free without a card; paid plans start at $5 for 3,000, and every feature is available on every plan.
Create a free ScreenshotNeo account for 1,000 screenshots a month with no card.
8. FAQ
Can I delete the branch named master while keeping main?
Yes. Switch to main, inspect any unique commits, and run git branch -d master. The local main branch remains.
Does deleting master rename it to main?
No. Deletion removes a ref; renaming is a separate operation. If you need to rename a branch, use Git’s branch rename command and update any affected remote or hosting settings.
Does deleting the remote branch delete my local master?
No. git push origin --delete master removes the server-side ref. Remove the local branch separately, and prune stale tracking refs if needed.
Should I use -D if -d refuses?
Only after reviewing the unmerged commits and deciding that none need to be retained. The refusal is a safeguard, not an instruction to force deletion.


