Nate
Nate

Reputation: 28384

How can I remove a Git branch locally?

I have a master and a development 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.

Upvotes: 351

Views: 707738

Answers (10)

amir seraj
amir seraj

Reputation: 28

for deleting a branch using command line use these: 1.delete a branch locally

git branch -d branch_name

2.in order to force deletion (if branch hasn't merged)

git branch -D branch_name

3.in order to delete a remote branch

git push origin --delete branch_name

Upvotes: 1

Benyamin Jafari
Benyamin Jafari

Reputation: 34006

To force delete a local branch:

git branch -D <branch-name>

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

Upvotes: 344

Hemanth Kollipara
Hemanth Kollipara

Reputation: 1141

After deleting a 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.

Upvotes: 26

Ravi Raj
Ravi Raj

Reputation: 63

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

It is simple and easy. It will delete all the content, except what you are working on and "develop" and "master".

Upvotes: 4

Mehdi Bouzidi
Mehdi Bouzidi

Reputation: 1985

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

Upvotes: 3

Qamar Stationwala
Qamar Stationwala

Reputation: 186

By your tags on the questions, I'm assuming you’re using GitHub. 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 repository 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 development branch.

Upvotes: 3

Eugene Kaurov
Eugene Kaurov

Reputation: 2981

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 the remote server:

git reset --hard origin/master

Upvotes: 39

torek
torek

Reputation: 488103

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 resynchronizations. 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.

Upvotes: 139

user3377716
user3377716

Reputation:

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.

Upvotes: 16

Kumar Vaduganathan
Kumar Vaduganathan

Reputation: 47

You need switch into another branch and try the same:

git branch -d <branch name>

Upvotes: 0

Related Questions