16

I made a local branch, but then realized that I do not need it.

If I delete it from the local repo, will I have to delete from the remote, too? I just made that branch and did not add, commit or pushed anything into it - in short I did not do anything in it.

sleske
  • 81,358
  • 34
  • 189
  • 227
Garden
  • 243
  • 1
  • 4
  • 11
  • If you believe that deleting it in both your local and remote repository is necessary, then yes you can do that and I suggest that you do just that. Here is a related stackoverflow post that you can follow to delete the branch locally and remotely. http://stackoverflow.com/questions/2003505/how-to-delete-a-git-branch-both-locally-and-remotely – ultrajohn Aug 12 '16 at 07:12
  • 1
    The question was a bit of a mess, but I think it's a decent (though basic) question. I edited it in hope of saving it. – sleske Aug 12 '16 at 07:46

5 Answers5

29

If you didn't push the branch to the remote, you can simple delete it locally:

git branch -d my_branch

Note: git will refuse to delete the branch if you have not merged it into its upstream or the current branch, to save you from losing the commits in the branch. If you are certain you do not need the commits, you can force the deletion with git branch -D my_branch.


You can get an overview of all branches by typing:

git branch -a

This may give something like the following:

*  master
   my_branch
   remotes/origin/master

(The branch with * is your current branch)

Note that the remote 'origin' doesn't have the local branch my_branch, since you haven't pushed it yet. Even if you had added and committed on the branch locally, it wouldn't be on the remote (until you push it).

If you did push it, you can remove it from the remote as follows:

git push origin :my_branch
sleske
  • 81,358
  • 34
  • 189
  • 227
Aerus
  • 4,332
  • 5
  • 43
  • 62
2

It seems like you are from SVN background (though I am not sure completely). So here are the points. 1. Creating branch locally has nothing to do with remote until you pushed it.

  1. All you commit will reside in your local machine and will go to the remote only you push it.

So you can go ahead and delete the local branch if you are sure you no longer need it.

But you want double check if this local branch is have reference on remote then after delete you can run git fetch and then check if that branch still exist by executing command git branch --all

MaNKuR
  • 2,578
  • 1
  • 19
  • 31
2

If the branch is only present in your local environment then just delete it by the following steps;

To get all the local branches;

git branch

the output will be like;

* your_local_branch (which you want to delete)
  master

Do git checkout master

And then to delete it locally,

git branch -d your_local_branch

If you want to delete the remote branch ( if the branch has been pushed to git repo )

git push origin :your_local_branch (if it is pushed)
Sam
  • 1,623
  • 1
  • 19
  • 31
  • No one in other solutions mentioned this(though people did in comments), if you are in the branch you want to delete, you can not do that. Git will not allow to use the command git branch -d your_local_branch, if that branch is currently checked out. – Mohammad Anas Nov 17 '20 at 21:14
1

You need to type in your console

git branch -D myBranch

benjdan
  • 169
  • 2
  • 6
0

if u are getting error while trying the git branch -d branchName, try unstaging all changes in your local first and then checkout to master. and try -D option to force delete the branch.

git reset

git checkout master

git branch -D branchName
Obsidian
  • 3,719
  • 8
  • 17
  • 30
geeta
  • 1