Skip to main content

Delete a Git branch both locally and remotely - Explained with Git commands and Example

Delete a Git branch both locally and remotely is a common task that can be accomplished in several ways. Here are some of the most common methods, explained in detail with examples.


Deleting a Git branch both locally and remotely - Explained with Git commands and Example


Deleting a local branch

To delete a local branch in Git, you can use the git branch -d command followed by the name of the branch you want to delete. For example, git branch -d feature-branch will delete the local branch called feature-branch.

Here's a step-by-step guide with images:

  • First, we create a local branch called feature-branch using the git branch feature-branch command:

Create local branch

  • Next, we delete the local branch using the git branch -d feature-branch command:

Delete local branch

Deleting a remote branch

To delete a remote branch in Git, you can use the git push command with the --delete option followed by the name of the remote branch you want to delete. For example, git push origin --delete feature-branch will delete the remote branch called feature-branch.

Here's a step-by-step guide with images:

  • First, we create a remote branch called feature-branch using the git push -u origin feature-branch command:

Create remote branch

  • Next, we delete the remote branch using the git push --delete origin feature-branch command:

Delete remote branch

Deleting a branch with merged changes

If the branch you want to delete has already been merged into another branch, you can use the git branch -d command to delete it. If you want to force the deletion, you can use the git branch -D command instead.

Here's a step-by-step guide with images:

  • First, we create a local branch called feature-branch and make some changes to it:

Create local branch

  • Next, we merge the changes from feature-branch into the main branch using the git merge feature-branch command:

Merge changes

  • Finally, we delete the feature-branch using the git branch -d feature-branch command:

Delete merged branch

Deleting a branch with unmerged changes

If the branch you want to delete has unmerged changes, you can use the git branch -D command to force the deletion.

Here's a step-by-step guide with images:

  • First, we create a local branch called feature-branch and make some changes to it:

Create local branch

  • Next, we try to delete the feature-branch using the git branch -d feature-branch command, but we get an error message because the changes have not been merged:

Delete unmerged branch

  • Finally, we force the deletion using the git branch -D feature-branch command:

Force delete unmerged branch

It's important to note that these commands can be dangerous if used incorrectly, so it's always a good idea to make a backup of your repository before making any changes. Additionally, if you're unsure about which command to use, it's always a good idea to consult the Git documentation or seek help from a more experienced Git user.

A few more reference to understand the concept "Deleting a Git branch both locally and remotely":

What's Hot

CVR Nummer : Register CVR Number for Denmark Generate and Test Online

CVR Nummer : Register CVR Number for Denmark Generate and Test Online | Image credit: Pexel What Is Danish CVR The Central Business Register (CVR) is the central register of the state with information on all Danish companies. Since 1999, the Central Business Register has been the authoritative register for current and historical basic data on all registered companies in Denmark. Data comes from the companies' own registrations on Virk Report. There is also information on associations and public authorities in the CVR. As of 2018, CVR also contains information on Greenlandic companies, associations and authorities. In CVR at Virk you can do single lookups, filtered searches, create extracts and subscriptions, and retrieve a wide range of company documents and transcripts. Generate Danish CVR For Test (Fake) Click the button below to generate the valid CVR number for Denmark. You can click multiple times to generate several numbers. These numbers can be used to Test your ...

Bing Homepage Quiz: Fun, Win Rewards, and Brain Teasers

Bing, Microsoft's search engine, has taken interactive engagement to the next level with its captivating feature - the Bing Homepage Quiz. This intriguing daily quiz not only challenges your knowledge but also offers a chance to earn rewards. In this comprehensive guide, we will explore the ins and outs of the Bing Homepage Quiz, including how to play, the different types of quizzes, and how you can earn and use rewards through this engaging feature. Bing homepage Quiz | Image credit: LanguageLassi How to Play the Bing Homepage Quiz Playing the Bing Homepage Quiz is simple and enjoyable. Here's how you can get started: Visit Bing : Open your preferred web browser and navigate to the Bing homepage (bing.com). Look for the Quiz : On the Bing homepage, keep an eye out for the interactive quiz card. This card is usually located near the bottom of the page and features a captivating image related to the quiz. Click to Start : Click on the quiz card to begin the quiz. It...

How To Iterate Dictionary Object

Dictionary is a object that can store values in Key-Value pair. its just like a list, the only difference is: List can be iterate using index(0-n) but not the Dictionary . Generally when we try to iterate the dictionary we get below error: " Collection was modified; enumeration operation may not execute. " So How to parse a dictionary and modify its values?? To iterate dictionary we must loop through it's keys or key - value pair. Using keys