1

EDIT: The intent is to do a completely clean reset of my local copy of master, to remote master. I prefer to reset to remote, over $ git reset --hard HEAD, which I've found on occasion gives me issues.

I know I can delete a local git branch like so:

$ git branch -D master

but is there a way to do that if you have no other branches? as git won't let you delete the branch you're currently on.

Normally I'd just switch into another branch, but what if you have no other branches? Create a temporary branch?

NOTE: not quite a duplicate of this other question, I believe

7
  • Why would you do something like this?
    – triplem
    Mar 19, 2021 at 12:50
  • Wouldn't deleting master basically delete your whole repo? Couldn't you just delete the .git folder for the same effect
    – mousetail
    Mar 19, 2021 at 12:51
  • 1
    You could create an empty-branch (eg. like described here stackoverflow.com/questions/34100048/…) and then delete master. Still the question, why this would be useful is open.
    – triplem
    Mar 19, 2021 at 12:53
  • 1
    "The intent is to do a completely clean reset of my local copy of master, to remote master" I don't get why you wouldn't just throw the whole folder away and reclone the remote.
    – matt
    Mar 19, 2021 at 19:32
  • I think that either resetting or @matt's suggestion is the most adequate solution. Everything else would be a hackfix.
    – mnestorov
    Mar 19, 2021 at 19:35

3 Answers 3

2

It might not be exactly what you want, but if the question is "how do I delete my only git branch" then I think this answer might meet those requirements:

mkdir deleteme
cd deleteme
git init
Initialized empty Git repository in deleteme/.git/
git:(master) echo foo > bar.txt
git:(master) ✗ git add -A
git:(master) ✗ git commit -m 'initial commit'
[master (root-commit) 2dc7ccd] initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 bar.txt
git:(master) git checkout 2dc7ccd
HEAD is now at 2dc7ccd initial commit
git:(2dc7ccd) git branch -D master
Deleted branch master (was 2dc7ccd).
git:(2dc7ccd) git branch -l
* (HEAD detached at 11c60ce)
2
  • 1
    I don't think this is what OP wants either, but I'm upvoting because it highlights the fact that you can delete even the last branch. I suspect after deleting master, OP would go checkout master again from the remote. And btw, I don't think you even have to tag it. You could simplify this by checking out the top commit by ID. (And I just linked my answer to yours because I like your point.)
    – TTT
    Mar 19, 2021 at 19:19
  • I updated the answer to not tag it - you're right, thanks!
    – mickadoo
    Mar 27, 2021 at 15:24
1

You always have to have something checked out (unless you make a bare repo for sharing purposes only which you won't be working out of). In your case, if master is the only branch left, that's your current branch and you can't delete it (yet). You could create or checkout another branch if you really want to, or even detach to a commit instead of a branch name, and then delete master. Or,

I prefer to reset to remote...

That is exactly what I would do:

git fetch
git reset --hard @{u}

With master checked out and tracking origin/master that would be the same command as:

git reset --hard origin/master

From there, you can wipe out your non-tracked files too if you'd like.

Alternatively, since you have only one branch and you don't like what you have on it, this is a pretty good candidate for deleting your repo and re-cloning. For a sufficiently small repo that is what I'd do, otherwise, I'd just reset my branch to the remote.

-1

If you want to just delete everything from your branch, then you can do something like this at your local repository

$ git checkout master
$ git rm -r folder-name or rm <file1> <file2> <file3>…
$ git commit -m "Remove files"
$ git push origin master

Another option is to just delete your .git folder

1
  • 1
    I have added some clarification to the question, since you answered - please see the EDIT: comment in the original question. Thanks!
    – Brad Parks
    Mar 19, 2021 at 12:57

Your Answer

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

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