是否可以完全清空远程 Git 存储库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4922104/
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
Is it possible to completely empty a remote Git repository?
提问by Makis
Let's say I make a mistake in my first push to a remote Git repository. Can I somehow revert this so that the repository is back to it's initial state? I don't have access to the remote machine, so deleting .git directory is not possible.
假设我在第一次推送到远程 Git 存储库时犯了一个错误。我可以以某种方式恢复它,以便存储库恢复到它的初始状态吗?我无权访问远程机器,因此无法删除 .git 目录。
The problem, as far as I can see is that you can't "unset" the head.
问题,据我所知,你不能“取消”头部。
This is what I (or actually a colleague who asked about this) did (I do a local "remote" repo here so anyone can test this):
这就是我(或者实际上是一位询问此问题的同事)所做的(我在这里做了一个本地“远程”存储库,因此任何人都可以对此进行测试):
mkdir git-remote
cd git-remote
git init --bare
cd ..
mkdir git-local
cd git-local
git clone ../git-remote .
touch a
git add a
git commit -m "initial"
git push origin master
And at this point he realised he pushed the wrong stuff to the remote repository.
此时他意识到他将错误的内容推送到远程存储库。
The only idea I had is the delete everything from his repo && git rm && git push which would still leave the faulty commit there, but the files wouldn't bother a second push.
我唯一的想法是从他的 repo && git rm && git push 中删除所有内容,这仍然会在那里留下错误的提交,但文件不会打扰第二次推送。
回答by Cascabel
I think you may have an XY problem. You don't actually need to get the remote repository back into its original state in order to start over; you simply need to start over locally, then force-push that to the remote.
我认为您可能有 XY 问题。为了重新开始,您实际上并不需要将远程存储库恢复到其原始状态;您只需要在本地重新开始,然后将其强制推送到远程。
# create a new repository that has the initial commit that you want
mkdir foo; cd foo; git init; ...; git commit
# set up a remote
git remote add origin <url-of-remote>
git branch --set-upstream master origin/master
# push your new history
git push -f
# delete obsolete remote branches
git push origin :deprecated-branch
The remote repository never returns to a no-commits state, but it ends up where you want it, and that's all that matters!
远程存储库永远不会返回到无提交状态,但它最终会到达您想要的位置,这才是最重要的!
(Under the hood, the old commits are actually still in the remote repository, but they're left dangling, with no refs pointing to them. They'll be automatically removed when git gc --auto
is triggered by a push at some point.)
(在幕后,旧的提交实际上仍在远程存储库中,但它们悬而未决,没有指向它们的引用。当git gc --auto
在某个时候被推送触发时,它们将被自动删除。)
If you really, really want to empty it for some reason, you can set receive.denyDeleteCurrent
in the remote repository, and push-delete all branches, including the current one. I don't see why you actually need to, though!
如果你真的,真的因为某种原因想清空它,你可以receive.denyDeleteCurrent
在远程存储库中设置,并推送删除所有分支,包括当前分支。不过,我不明白为什么你真的需要这样做!
回答by John Bachir
When you say "first push" do you mean the very first push to the branch ever? As in there was no code in it at all before you pushed to it. If that's the case…
当你说“第一次推送”时,你是指有史以来第一次推送到分支吗?因为在你推送到它之前根本没有代码。如果是这样的话……
If you wanted to push a local branch footo a remote branch for the first time, you would do this:
如果你想第一次将本地分支foo推送到远程分支,你可以这样做:
git push origin foo:foo
In order to deleteremote branch foo, you tell git to push "nothing" to it:
为了删除远程分支foo,你告诉 git 向它推送“nothing”:
git push origin :foo
Now you can start over with it however you like.
现在,您可以随心所欲地重新开始。
To be honest I'm not sure what actually remains physically on the server -- but in terms of completely resetting branch history, this will work.
老实说,我不确定服务器上实际实际保留了什么——但是就完全重置分支历史记录而言,这会起作用。
(fwiw, I think this interface is pretty ridiculous)
(fwiw,我觉得这个界面很可笑)
回答by Mayank Dudakiya
Best way to empty the GitHub repository.
清空 GitHub 存储库的最佳方法。
Step 1:Create new branch.
git checkout -b master1
第一步:新建分支。
git checkout -b master1
Step 2:Add your data into its.
第 2 步:将您的数据添加到其中。
git add .
git add .
git commit -m "Initial commit"
git commit -m "Initial commit"
git push origin master1
git push origin master1
Step 3:Now remove old master branch.
第 3 步:现在删除旧的 master 分支。
git push origin --delete <your_branch>
git push origin --delete <your_branch>
回答by sandeep chougule
Below steps will do it easily, Be careful to take master backup in case needed.
下面的步骤很容易做到,如果需要,请注意进行主备份。
mkdir reset
cd reset
git init
touch README.md
git add .
git commit -m 'Initial commit'
git remote add origin [email protected]:XXX/xxx.git
git push --force --set-upstream origin master
回答by deleted77
You can git revert
commits, e.g. git revert HEAD
.
你可以git revert
提交,例如git revert HEAD
。
Keep in mind that the content you committed will reside in the .git
directory. (You can't do anything about that)
请记住,您提交的内容将驻留在.git
目录中。(你对此无能为力)
It's impossible to actually deletea commit from the history without having access to the .git
directory.
在没有访问目录的情况下,实际上不可能从历史记录中删除提交.git
。