In this lesson, you will learn how to finalize your collaborative efforts by integrating approved changes into the main branch and tidying up your repository by removing obsolete feature branches.
After a code review is completed and your Pull Request (PR) is approved, the final step is to merge those changes into your project’s primary branch. On GitHub, this is typically done using the "Merge pull request" button. This action performs a merge commit, bringing your isolated feature work into the codebase shared by your entire team.
Why Delete Stale Branches?
Once a PR is merged, the feature branch that contained your work is considered "stale." Keeping these branches around long after their purpose is served leads to "branch clutter." If you don't delete them, your repository's list of branches will grow indefinitely, making it harder to track active development and increasing the likelihood of accidentally pushing changes to the wrong, outdated branch later.
The Cleanup Workflow
When you merge via the GitHub interface, it provides a convenient "Delete branch" button immediately following the merge. You should almost always click this to keep your remote repository clean. However, don’t forget that your local environment still holds a copy of that branch!
To clean up your local repository, perform these steps in your terminal:
- Switch back to your primary branch:
git checkout main - Ensure you have the latest state:
git pull origin main - Delete your local feature branch:
git branch -d feature-branch-name
By deleting the branch locally and on GitHub, you ensure that your development environment remains focused only on active tasks. This systematic approach—merging, verifying, and then pruning—maintains the health of the project and makes it easier for your team to navigate the repository history.
Key Takeaway: Always delete your feature branches after a successful merge to prevent repository clutter and ensure that your workflow remains clear and organized for future development.