简体   繁体   English

git - 如果没有其他分支,如何删除本地 master

[英]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.我更喜欢重置为远程,超过$ git reset --hard HEAD ,我发现它有时会给我带来问题。

I know I can delete a local git branch like so:我知道我可以像这样删除本地 git 分支:

$ 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.因为 git 不会让您删除当前所在的分支。

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:这可能不是您想要的,但如果问题是“我如何删除我唯一的 git 分支”,那么我认为这个答案可能满足这些要求:

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).在你的情况下,如果master是唯一剩下的分支,那是你当前的分支,你不能删除它(还)。 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 .如果您真的想要,您可以创建或签出另一个分支,甚至可以分离到提交而不是分支名称,然后删除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:master签出并跟踪origin/master将是相同的命令:

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另一种选择是删除您的.git文件夹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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