Git Git and GitHub

Managing Branches in Git: Main Branch Control, Listing, Deleting, Updating, Switching & Renaming

Posted: January 09, 2026 | Updated: January 09, 2026
Share: Twitter LinkedIn

Managing Branches in Git: Main Branch Control, Listing, Deleting, Updating, Switching & Renaming

Posted: December 03, 2025 | Updated: December 03, 2025

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.


⭐ 1. Changing the Main Branch & Renaming It

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.


Set a Different Branch as the Default Main Branch

  1. Push the new branch if not already pushed:

git push --set-upstream origin new-main-branch
  1. On GitHub:
    Settings → Branches → Default Branch → Choose new-main-branch

This immediately makes the new branch the “main” branch for PRs, forks, and deployments.


Rename the Main Branch Locally

git branch -m old-main new-main

Push the renamed branch and delete the old one on GitHub

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.


⭐ 2. Listing Branches & Deleting Them

List Local Branches

git branch

List Remote Branches

git branch -r

List All Branches (local + remote)

git branch -a

Delete a Local Branch

Safe delete:

git branch -d branch-name

Force delete:

git branch -D branch-name

Delete a Branch on GitHub

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.


⭐ 3. Update Branch Content & Push to Another Branch

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.


A. Merge: Bring all changes from another branch

git checkout main
git pull origin main
git merge origin/v2-branch
# resolve conflicts
git push origin main

B. Cherry-pick: Bring specific commits

git checkout main
git cherry-pick 
git push origin main

Use git log --oneline to find commit IDs.


C. Replace current branch with another branch’s working tree (without preserving history)

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.


D. Force push (dangerous — use only if you understand the impact)

git checkout v2
git push --force origin v2:main

This overwrites the main branch history completely.


⭐ 4. Rename Branches & Switch Between Them

Branch renaming helps maintain clean, descriptive version control.


Rename a Branch

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

Switch Between Branches

Traditional:

git checkout branch-name
git checkout -b new-branch   # create + switch

Modern (recommended):

git switch branch-name
git switch -c new-branch

Handling uncommitted changes when switching branches

If Git blocks the switch:

Stash changes:

git stash push -m "WIP"
git switch branch-name

Restore later:

git stash pop