Git basics

Clean up branches deleted on GitHub

Remove the leftover copies of branches that were already deleted on GitHub, so your branch list only shows branches that still exist.

Fill these in once
  1. Forget remote branches that no longer exist
    Terminal
    git fetch --prune

    Run this inside your project. It removes entries like origin/fix-login when that branch was deleted on GitHub. Your own local branches stay untouched.

  2. Find local branches whose remote is gone
    Terminal
    git branch -vv

    Look for lines that end with ": gone]". Those branches were deleted on GitHub, usually after a pull request was merged.

  3. Move off the branch you want to delete
    Terminal
    git switch main

    Git won't delete the branch you are on. Use your main branch name if it is different.

  4. Delete the old local branch
    Terminal
    git branch -d fix-login

    Git refuses if the branch has work that isn't merged. If the pull request was squash merged, Git may still refuse. Use git branch -D {{branch}} only when you are sure the work is safe on GitHub.

  5. Prune on every fetch from now on (optional)
    Terminal
    git config --global fetch.prune true

    After this, every git fetch and git pull cleans up deleted remote branches for you.

Check it worked

  1. Check no branch shows as gone
    Terminal
    git branch -vv
  2. List the remote branches Git still knows about
    Terminal
    git branch -r
Undo it
  1. Find the last commit of a deleted branch
    Terminal
    git reflog

    Look for the commit you were on in that branch and copy its short ID.

  2. Bring the branch back
    Terminal
    git branch fix-login <commit-id>

    Replace <commit-id> with the ID from git reflog.

  3. Stop pruning on every fetch
    Terminal
    git config --global --unset fetch.prune
  1. Forget remote branches that no longer exist
    Terminal · Ubuntu / Debian
    git fetch --prune

    Run this inside your project. It removes entries like origin/fix-login when that branch was deleted on GitHub. Your own local branches stay untouched.

  2. Find local branches whose remote is gone
    Terminal · Ubuntu / Debian
    git branch -vv

    Look for lines that end with ": gone]". Those branches were deleted on GitHub, usually after a pull request was merged.

  3. Move off the branch you want to delete
    Terminal · Ubuntu / Debian
    git switch main

    Git won't delete the branch you are on. Use your main branch name if it is different.

  4. Delete the old local branch
    Terminal · Ubuntu / Debian
    git branch -d fix-login

    Git refuses if the branch has work that isn't merged. If the pull request was squash merged, Git may still refuse. Use git branch -D {{branch}} only when you are sure the work is safe on GitHub.

  5. Prune on every fetch from now on (optional)
    Terminal · Ubuntu / Debian
    git config --global fetch.prune true

    After this, every git fetch and git pull cleans up deleted remote branches for you.

Check it worked

  1. Check no branch shows as gone
    Terminal · Ubuntu / Debian
    git branch -vv
  2. List the remote branches Git still knows about
    Terminal · Ubuntu / Debian
    git branch -r
Undo it
  1. Find the last commit of a deleted branch
    Terminal · Ubuntu / Debian
    git reflog

    Look for the commit you were on in that branch and copy its short ID.

  2. Bring the branch back
    Terminal · Ubuntu / Debian
    git branch fix-login <commit-id>

    Replace <commit-id> with the ID from git reflog.

  3. Stop pruning on every fetch
    Terminal · Ubuntu / Debian
    git config --global --unset fetch.prune
  1. Forget remote branches that no longer exist
    PowerShell
    git fetch --prune

    Run this inside your project in PowerShell or Git Bash. It removes entries like origin/fix-login when that branch was deleted on GitHub. Your own local branches stay untouched.

  2. Find local branches whose remote is gone
    PowerShell
    git branch -vv

    Look for lines that end with ": gone]". Those branches were deleted on GitHub, usually after a pull request was merged.

  3. Move off the branch you want to delete
    PowerShell
    git switch main

    Git won't delete the branch you are on. Use your main branch name if it is different.

  4. Delete the old local branch
    PowerShell
    git branch -d fix-login

    Git refuses if the branch has work that isn't merged. If the pull request was squash merged, Git may still refuse. Use git branch -D {{branch}} only when you are sure the work is safe on GitHub.

  5. Prune on every fetch from now on (optional)
    PowerShell
    git config --global fetch.prune true

    After this, every git fetch and git pull cleans up deleted remote branches for you.

Check it worked

  1. Check no branch shows as gone
    PowerShell
    git branch -vv
  2. List the remote branches Git still knows about
    PowerShell
    git branch -r
Undo it
  1. Find the last commit of a deleted branch
    PowerShell
    git reflog

    Look for the commit you were on in that branch and copy its short ID.

  2. Bring the branch back
    PowerShell
    git branch fix-login <commit-id>

    Replace <commit-id> with the ID from git reflog.

  3. Stop pruning on every fetch
    PowerShell
    git config --global --unset fetch.prune