是否有可能完全清空远程Git存储库? [英] Is it possible to completely empty a remote Git repository?

查看:103
本文介绍了是否有可能完全清空远程Git存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在第一次推送到远程Git存储库时犯了一个错误。我可以以某种方式恢复它,以便存储库恢复到初始状态吗?我无法访问远程机器,因此删除.git目录是不可能的。



这个问题,据我看到的是,你不能bset b

这就是我(或者实际上是一个询问这个问题的同事)做过的事情(我在这里做一个本地远程回购,因此任何人都可以测试这个):

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b cd ..
mkdir git-local
cd git-local
git clone ../git-remote。
touch a
git add a
git commit -minitial
git push origin master

在这一点上,他意识到他把错误的东西推送到远程仓库。



我唯一的想法是删除从他的回购&& git rm&& git push,它仍然会在那里留下错误的提交,但是这些文件不会再次打开。

可能有XY问题。您实际上并不需要将远程存储库恢复到原始状态以重新开始;你只需要在本地重新开始,然后强制推送到远程。

 #创建一个新的存储库,提交你想要
mkdir foo; cd foo; git init; ...; git commit

#设置远程
git remote add origin< url-of-remote>
git branch --set-upstream master origin / master

#推送您的新历史
git push -f

#删除过时的远程分支
git push origin:deprecated-branch

远程存储库永远不会返回no-commits状态,但它最终会在你想要的地方结束,这就是最重要的!



(底下,旧的提交实际上仍然在远程仓库中,在没有指向它们的情况下,它们将被自动删除,由某个点的推动触发。)



如果您确实想要将其清空,出于某种原因,您可以在远程存储库中设置 receive.denyDeleteCurrent ,然后按 - 删除所有分支,包括当前的分支。不过,我不明白为什么你真的需要!


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.

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.

解决方案

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.

# 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.)

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!

这篇关于是否有可能完全清空远程Git存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆