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.
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.
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 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With