Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a local Git Branch that was never pushed - delete it on the server, too?

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.

like image 711
Garden Avatar asked Aug 12 '16 07:08

Garden


People also ask

How do you delete a branch that has not been pushed?

Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet. The branch is now deleted locally.

How do I delete local branches that have been deleted remotely?

Issue the git push origin –delete branch-name command, or use the vendor's online UI to perform a branch deletion. After the remote branch is deleted, then delete the remote tracking branch with the git fetch origin –prune command. Optionally delete the local branch with the git branch -d branch-name command.

What happens if I delete a local branch in git?

What Happens If I Delete a Git Branch? When you delete a branch in Git, you don't delete the commits themselves. That's right: The commits are still there, and you might be able to recover them.


1 Answers

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
like image 87
Aerus Avatar answered Sep 21 '22 09:09

Aerus