git 如何完全清除git存储库而不删除它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28578581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to completely clear git repository, without deleting it
提问by nemo_87
How can I remove all files and folders that I'm storing on git repo, but keep the repository? Is there some command for restarting git repo? I've found many examples on this, but I've made even bigger mess. The repository is linked. So if someone is willing to explain it to me step by step I would be very thankful.
如何删除我存储在 git repo 上的所有文件和文件夹,但保留存储库?是否有一些命令可以重新启动 git repo?我在这方面找到了很多例子,但我弄得更糟了。存储库已链接。所以如果有人愿意一步一步地向我解释,我将非常感激。
回答by Chris Maes
if you only have a local git repository
如果您只有本地 git 存储库
if you want to erase your whole history and start over again:
如果您想删除整个历史并重新开始:
cd <repo>
rm -rf .git
git init
and start committing again.
并再次开始提交。
If you want to remove both files and history:
如果您想同时删除文件和历史记录:
cd <repo>
rm -rf *
git init
and start adding files and committing...
并开始添加文件并提交...
if you are linked with a remote repositoryif you want to start over again, but don't really mind some old history remaining; there is a quick way:
如果您与远程存储库链接,如果您想重新开始,但并不介意保留一些旧历史;有一个快速的方法:
git pull
git rm -r *
git commit
git push
now your repository is empty again, you'll only have some old history remaining. if you also want to clean up all your history; that takes a little more work (and note that this will cause trouble for anyone else linked to that same remote repository):
现在您的存储库又是空的,您将只剩下一些旧的历史记录。如果您还想清理所有历史记录;这需要更多的工作(请注意,这会给链接到同一远程存储库的其他人带来麻烦):
git checkout <first commit hash>
git rm -r *
touch README
git add README
git commit --amend
git push -f
note that I create a empty README file for the first commit, since a commit cannot be empty.
请注意,我为第一次提交创建了一个空的 README 文件,因为提交不能为空。
回答by VonC
If you are talking about an existing remote repo (and not just a local repo, which is trivial to do), you can:
如果您正在谈论现有的远程存储库(而不仅仅是本地存储库,这是微不足道的),您可以:
- clone it
- delete all remote branches:
git push origin --delete <branchName>
(see "Delete a Git branch both locally and remotely") make a new orphan master branch (see "How can I completely empty the master branch in Git?")
git branch -D master git checkout --orphan master
make at least one commit and push it.
- 克隆它
- 删除所有远程分支:(
git push origin --delete <branchName>
参见“在本地和远程删除 Git 分支”) 创建一个新的孤立主分支(请参阅“如何在 Git 中完全清空主分支?”)
git branch -D master git checkout --orphan master
至少进行一次提交并推送它。