How to remove a branch locally?

GitGithub

Git Problem Overview


I have a master and a dev branch in my repository. I want to remove the master branch from my computer so that I don't accidentally commit to it (it's happened..).

There are questions on here about how to delete branches locally and remotely, but I haven't been able to find out how to only delete a branch locally.

One answer said to use this:

git branch -d local_branch_name

But I tried that and the branch still shows up in the GitHub application.

Git Solutions


Solution 1 - Git

To Force Delete a Local Branch:

$ git branch -D <branch-name>

[NOTE]:

-D is a shortcut for --delete --force.


Solution 2 - Git

I think (based on your comments) that I understand what you want to do: you want your local copy of the repository to have neither the ordinary local branch master, nor the remote-tracking branch origin/master, even though the repository you cloned—the github one—has a local branch master that you do not want deleted from the github version.

You can do this by deleting the remote-tracking branch locally, but it will simply come back every time you ask your git to synchronize your local repository with the remote repository, because your git asks their git "what branches do you have" and it says "I have master" so your git (re)creates origin/master for you, so that your repository has what theirs has.

To delete your remote-tracking branch locally using the command line interface:

git branch -d -r origin/master

but again, it will just come back on re-synchronizations. It is possible to defeat this as well (using remote.origin.fetch manipulation), but you're probably better off just being disciplined enough to not create or modify master locally.

Solution 3 - Git

As far I can understand the original problem, you added commits to local master by mistake and did not push that changes yet. Now you want to cancel your changes and hope to delete your local changes and to create a new master branch from the remote one.

You can just reset your changes and reload master from remote server:

git reset --hard origin/master

Solution 4 - Git

The Github application for Windows shows all remote branches of a repository. If you have deleted the branch locally with $ git branch -d [branch_name], the remote branch still exists in your Github repository and will appear regardless in the Windows Github application.

If you want to delete the branch completely (remotely as well), use the above command in combination with $ git push origin :[name_of_your_new_branch]. Warning: this command erases all existing branches and may cause loss of code. Be careful, I do not think this is what you are trying to do.

However every time you delete the local branch changes, the remote branch will still appear in the application. If you do not want to keep making changes, just ignore it and do not click, otherwise you may clone the repository. If you had any more questions, please let me know.

Solution 5 - Git

After deleting branch using:

git branch -d BranchName

To remove branches that no longer exist in the remote repository use:

git fetch -p

-p indicates prune, to remove branches from local repository that doesn't exist in the remote.

Solution 6 - Git

You can delete multiple branches on windows using Git GUI:

  1. Go to your Project folder
  2. Open Git Gui: enter image description here
  3. Click on 'Branch': enter image description here
  4. Now choose 'Delete': enter image description here
  5. If you want to delete all branches besides the fact they are merged or not, then check 'Always (Do not perform merge checks)' enter image description here

Solution 7 - Git

By your tags, I'm assuming your using Github. Why not create some branch protection rules for your master branch? That way even if you do try to push to master, it will reject it.

> 1) Go to the 'Settings' tab of your repo on Github.

> 2) Click on 'Branches' on the left side-menu.

> 3) Click 'Add rule'

> 4) Enter 'master' for a branch pattern.

> 5) Check off 'Require pull request reviews before merging'

I would also recommend doing the same for your dev branch.

Solution 8 - Git

you need switch into another branch and try the same.

git branch -d

Solution 9 - Git

git branch | grep -v "develop" | grep -v "master" | xargs git branch -D

Simple and easy. It will delete all the except you are working on and "develop" and "master".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNateView Question on Stackoverflow
Solution 1 - GitBenyamin JafariView Answer on Stackoverflow
Solution 2 - GittorekView Answer on Stackoverflow
Solution 3 - GitEugene KaurovView Answer on Stackoverflow
Solution 4 - Gituser3377716View Answer on Stackoverflow
Solution 5 - GitHemanth KolliparaView Answer on Stackoverflow
Solution 6 - GitMehdi BouzidiView Answer on Stackoverflow
Solution 7 - GitQamar StationwalaView Answer on Stackoverflow
Solution 8 - GitKumar VaduganathanView Answer on Stackoverflow
Solution 9 - GitRavi RajView Answer on Stackoverflow