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.

2
  • 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. stackoverflow.com/questions/2003505/…
    – ultrajohn
    Aug 12, 2016 at 7: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, 2016 at 7:46

5 Answers 5

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
3
  • 1
    You mean git branch -D my_branch
    – user6110389
    Jan 25, 2018 at 12:36
  • 2
    For some reason, because I was on said branch, git wouldn't let me delete it. So I checked out another and only then could I delete the first branch.
    – Jonny
    Jul 9, 2018 at 13:32
  • 2
    Don't forget to switch your HEAD to another branch before deleting it :) Jan 31, 2019 at 10:35
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

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)
1
  • 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. Nov 17, 2020 at 21:14
1

You need to type in your console

git branch -D myBranch

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.