在Heroku上清理git仓库

35
情况如下:
我们有一个在Github上持续的项目,并将其部署到Heroku,后者具有自己的mini-Git。一些更改直接应用于Heroku(从Heroku克隆存储库,进行更改并推送)。与此同时,Github上的主要分支也增加了一些更改。
现在,当我想将主存储库推送到Heroku时,由于Heroku上的提交与本地提交不同步,推送失败。
我不想与Heroku上的更改合并 - 我只想让它们消失。
是否有某种方法可以清理Heroku上的git存储库,然后从头开始推送我的本地存储库?
我不想销毁应用程序并重新创建它,因为它拥有一些付费服务,并且我只是协作者。

5
"heroku run bash"会将您登录到应用程序文件夹,然后只需删除所有内容。 - Muhammad Umer
5个回答

65
安装Heroku Repo插件,然后使用它重置远程git仓库。

首先在服务器上删除git仓库:

> cd /my-project/
> heroku plugins:install heroku-repo
> heroku repo:reset

然后通过删除并重新创建本地 git 存储库来重新初始化它:

> cd /my-project/
> rm -rf .git
> git init
> heroku git:remote -a <appname>

其中 <appname> 是你在 Heroku 中的应用程序名称。

现在,这两个仓库(本地和远程)应该是空的并且同步。


57
您可以在 git 中使用 -f 标志(它是标准 git 标志)来强制推送。请参见 Dev Center Git 文档中的侧边栏注释。这将显然覆盖您在 Heroku 上所做的更改。

6

4
如果您的Heroku推送失败,因为它不是快进提交,并且您没有尝试在历史记录中进行任何奇怪的操作,那么解决方法非常简单:
  1. Ensure that your non-Heroku repository is defined as a remote.
  2. Fetch all remotes.
  3. Merge your other repository into your local Heroku clone using the recursive strategy with the "theirs" strategy option.

    git merge -X theirs remotes/origin/master
    
  4. Push with the --force option to make a non-fast-forward commit to your upstream Heroku repository.

另一方面,如果您真的想要清除历史记录,还有其他选择。
  • Remove all but the first commit from your history (easiest).

    git reset --hard $(git log --pretty=oneline | tail -n1 | cut -d ' ' -f1)
    git push --force
    
  • Create an empty master branch.
  • Use git-filter-branch.

就个人而言,我建议保留您的历史记录并修复快进问题作为最佳实践,但如果您想积极修改历史记录,Git在这方面非常灵活。


2
您也可以简单地使用Heroku的应用程序版本控制系统。 进入“活动”,您可以查看项目的完整历史记录:回滚到第一个活动,存储库应完全清除,并返回到在Heroku上创建应用程序时的状态。

那不起作用:![rejected] master -> master(非快进式) - mgPePe
我不知道,这是Heroku的问题,他们必须解决。但是...你是在使用Web界面还是命令行?尝试使用Web界面... - Aerendir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接