Branch management is one of the most important skills when working with Git and GitHub—especially for Django projects that require stable, versioned development. This guide explains how to change the main branch, rename branches, list and delete branches, update branch content, push changes from one branch to another, and switch between branches.
All commands include both Git Bash/WSL and PowerShell friendly versions when needed.
The main branch (formerly "master") is the primary branch that represents production-ready code.
You may need to change which branch is the main branch, especially when introducing a new version of your project.
Push the new branch if not already pushed:
git push --set-upstream origin new-main-branch
On GitHub:
Settings → Branches → Default Branch → Choose new-main-branch
This immediately makes the new branch the “main” branch for PRs, forks, and deployments.
git branch -m old-main new-main
git push origin new-main git push origin --delete old-main git branch --set-upstream-to=origin/new-main new-main
If GitHub blocks deletion due to branch protection, disable protection temporarily, delete, then re-enable.
git branch
git branch -r
git branch -a
Safe delete:
git branch -d branch-name
Force delete:
git branch -D branch-name
git push origin --delete branch-name git fetch origin --prune # refresh remote list
This removes the branch from GitHub and removes its “Compare & Pull Request” notification.
Sometimes you want to move code from one branch to another — for example, merging new work into main, or replacing main with content from a feature branch.
git checkout main git pull origin main git merge origin/v2-branch # resolve conflicts git push origin main
git checkout main git cherry-pick git push origin main
Use git log --oneline to find commit IDs.
Useful when creating a new main branch version.
git checkout -b replace-main origin/main git checkout origin/v2 -- . git add -A git commit -m "Replace main with v2 content" git push --set-upstream origin replace-main
Then create a PR for review.
git checkout v2 git push --force origin v2:main
This overwrites the main branch history completely.
Branch renaming helps maintain clean, descriptive version control.
Locally:
git branch -m old-name new-name
Push new branch and remove the old branch:
git push origin new-name git push origin --delete old-name git branch --set-upstream-to=origin/new-name new-name
git checkout branch-name git checkout -b new-branch # create + switch
git switch branch-name git switch -c new-branch
If Git blocks the switch:
git stash push -m "WIP" git switch branch-name
Restore later:
git stash pop
© 2024 Webapptiv. All rights reserved.