Git Branch Deletion Explained — Local and Remote

In the previous blog, we explored how branches map between local and remote repositories.

Now, let’s take it further:

  • How to create a new branch
  • How to delete a branch locally
  • How to delete a branch from GitHub
  • Different ways of deleting remote branches

Step 1: Creating a New Branch

Switch to main branch and create a new branch called test:

git switch main
git branch test

👉 Output:

* main
  feature1
  test

✅ A new local branch test is created. But at this point, it only exists locally.


Step 2: Pushing the Branch to GitHub

Now push it to the remote repository:

git push origin test

👉 On GitHub, you’ll see the test branch created.

Visual representation:



Step 3: Deleting the Local Branch

If your work on this branch is complete, you can delete it locally:

git branch -d test

👉 This deletes only the local branch. The remote branch still exists on GitHub.


Step 4: Deleting the Remote Branch

There are two ways to delete a branch from GitHub (remote repo).

Method 1: Using --delete

git push origin --delete test

👉 The branch test will be removed from the remote repository.

Method 2: Using Push with Empty Source

git push origin :test

👉 Here, you don’t pass any source branch. Git interprets this as: “delete the branch test on remote.”

✅ This also removes the branch from GitHub.


Step 5: Confirming the Result

Check remote branches:

git branch -a

Output before deletion:

* main
  feature1
  test
  remotes/origin/main
  remotes/origin/test

Output after remote deletion:

* main
  feature1
  test
  remotes/origin/main

👉 The remote test branch is gone, but the local copy still exists until you delete it manually.



Summary

  • Local branch deletion does not affect remote.
  • Remote deletion does not affect local.
  • You must delete both separately if you want them gone everywhere.
  • git branch -d <branch> → Deletes branch locally.
  • git push origin --delete <branch> → Deletes branch from GitHub.
  • git push origin :<branch> → Alternate way to delete remote branch.
  • Local and remote branches are independent — deleting one does not delete the other.


Keep Learning 🚀

👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.

👉 My YouTube Channel – More videos, more fun, and lots of learning!

👉 📺 Watch this topic in video form

Stay curious, keep coding, and let’s make learning fun together! 🎉