Course contentsShow
← Git & GitHub
Lesson 73 of 964. Collaborative WorkflowsFree lesson

Managing Contribution Merging and Branch Cleanup

Delete your local and remote feature branches after the maintainer merges your contribution to maintain a clean and organized repository.

In this lesson, you will learn how to maintain a professional and organized repository by systematically deleting feature branches after your code has been successfully merged.

The Importance of Branch Hygiene

Once a maintainer merges your pull request, your feature branch has served its purpose. Leaving stale branches in your local and remote repositories creates "digital clutter," making it harder to track active development and increasing the risk of accidentally pushing changes to the wrong branch. Keeping your workspace clean is a hallmark of an advanced collaborator.

Cleaning Up the Remote and Local Environment

After your pull request is merged on GitHub, the remote branch is typically deleted by the maintainer. However, your local machine still holds that branch, and your local Git configuration might still track a remote branch that no longer exists.

Step 1: Sync and Prune

First, switch back to your main (or master) branch, pull the latest changes, and prune your local tracking references to remove any remote branches that were deleted on GitHub:

git checkout main
git pull origin main
git remote prune origin

Step 2: Delete the Local Feature Branch

Now that you are synced, you can safely remove the local feature branch. Since the work is safely merged, you can use the -d flag to delete it securely. Git will prevent you from deleting the branch if the changes haven't been fully merged, acting as a final safety check:

git branch -d feature/my-new-contribution

If you ever need to forcefully remove a branch that wasn't fully merged (though you should avoid this for finished work), you would use the capital -D flag.

By performing these steps, you ensure your repository remains a lean, efficient environment. This practice not only keeps your command history readable but also demonstrates respect for the project structure by preventing the accumulation of obsolete references.

Key Takeaway: After a successful merge, always pull the latest changes to your primary branch and delete your local feature branch to maintain a clean, navigable repository.