简体   繁体   中英

git - how to delete local master if you have no other branches

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

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)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM