Deleting both local and remote branches in Git might sound like a single-command task, but it actually requires a two-step process. Whether you’re cleaning up after merging features or just tidying up your repository, knowing how to efficiently manage your branches is crucial for maintaining a well-organized codebase. In this tutorial, we’ll walk you through the steps needed to delete a Git branch both locally and remotely, ensuring you can keep your project’s branch structure clean and manageable. This guide will also help you avoid common pitfalls and ensure that your branches are properly deleted without affecting your workflow. Let’s dive in and streamline your Git operations together!
Setting Up the Scenario:
To illustrate the process, I’ve created a GitHub repository named “Branch onixdigitalmarketing” with two branches: “Master” and “dev.” We’ll clone this repository, bring down both branches to the local machine, delete the remote branch, and then remove the local one. If you’re interested in learning more about Git operations, you may also find our article on “How to Undo Recent Local Commits in Git” helpful. This guide provides insights into reverting local commits in Git, ensuring a smooth version control experience.
Step By Step Method How To Delete a Git Branch Locally And Remotely?
1
Cloning the Repository
◉ Clone the repository using the following command:
git clone https://github.com/XEESHANAKRAM/onixdigitalmarketing.git
Navigate to the cloned folder using cd onixdigitalmarketing
2
Checking Branches
◉ Check the available branches using:
git branch -a
Initially, the local branch is “Master,” and “dev” is a remote tracking branch.
3
Switching and Verifying
◉ Switch to the “dev” branch and verify the local branches using:
git switch dev
git branch -a
Switch back to the “Main” branch for convenience:
git switch Master
4
Deleting Remote Branch
◉ To delete the remote branch “dev,” use:
git push origin --delete dev
Verify the deletion by refreshing the GitHub repository.
5
Deleting Local Branch
◉ Now, delete the local branch “dev” using:
git branch -d dev
Verify the deletion using:
git branch -a
Congratulations! You’ve successfully deleted both local and remote branches in Git.