Sub topic 15
Clean up after your pull request is merged
This chapter assumes you have a merged pull request, an origin remote pointing to your fork, and an upstream remote pointing to the original repository. You should also know the basic branch commands from earlier chapters. Cleanup keeps your fork's main matching upstream and removes branches you no longer need.
Why cleanup matters
After a merge, your feature branch has done its job. If you leave it, your branch list fills with stale names. Worse, if you start your next change from an old main, the new pull request will carry commits that upstream already has, and it will probably conflict.
Cleanup has three parts:
- Update your local
mainfrom upstream. - Delete the local feature branch.
- Delete the branch on your fork.
The cleanup sequence
Run these commands from inside your clone:
git switch main
git fetch upstream
git merge --ff-only upstream/main
git branch -d fix-readme-typo
git push origin --delete fix-readme-typoReplace main with the project's default branch name if it differs.
git switch mainleaves the feature branch. Git will not delete the branch you are standing on.git fetch upstreamdownloads the latest upstream commits, including your merged change.git merge --ff-only upstream/mainmoves yourmainforward. If yourmainhas commits that upstream lacks, the command fails instead of creating a surprise merge commit.git branch -ddeletes the local branch only if Git sees it as merged.git push origin --deleteremoves the branch from your fork on GitHub.
Your fork's main on GitHub is still behind. Push it with git push origin main, as in the sync chapter.
When `git branch -d` refuses
Many projects use squash merge or rebase merge. Both create new commits on main instead of keeping your original ones. Git then sees your branch commits as not merged, and git branch -d prints an error such as error: the branch 'fix-readme-typo' is not fully merged.
If GitHub shows your pull request as merged, the work is safe in upstream. In that case, force the deletion:
git branch -D fix-readme-typoCheck first that the pull request really merged. -D discards any commits that exist only on that branch.
Check the result
Confirm that the cleanup worked:
git branch
git branch -r
git log --oneline -3git branch should list only branches you still use. git branch -r shows remote-tracking branches, and a deleted branch can linger there. Run git fetch --prune origin to remove stale entries. The log should show your merged change near the top of main.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `Not possible to fast-forward, aborting` | Your `main` has commits upstream lacks | Inspect with `git log upstream/main..main`. Move those commits to a branch before resetting. |
| `Cannot delete branch ... checked out` | You are still on the feature branch | Run `git switch main` first |
| `remote ref does not exist` on delete | The branch was already removed, often by GitHub's delete-branch button | Run `git fetch --prune origin` |
Start the next change
Begin each new contribution from the updated main:
git switch -c next-changeYour new branch then starts from a base that matches upstream.
Exercise
Your pull request was squash-merged. After you run git branch -d fix-readme-typo, Git reports that the branch is not fully merged. Why does this happen, and what do you check before running git branch -D?
Show answer
A squash merge puts your changes on upstream's main as one new commit. Your original commits are not in main's history, so Git thinks the branch is unmerged. Before using -D, confirm on GitHub that the pull request shows as merged, and that your local main now contains the squashed change.