【问题标题】:Pushing to github after a shallow clone浅克隆后推送到 github
【发布时间】:2012-07-07 16:07:45
【问题描述】:

由于提交的数量巨大,我有一个庞大的 git 存储库,因此按照here 的建议,我创建了一个浅克隆。我已经对这个新的本地 repo 进行了更改,现在我想推送到我在 Github 的起源(然后再推送到我在 Heroku 上的暂存和生产遥控器)。也许有一天我会学会阅读文档:

git clone --depth 命令选项说

--depth 创建一个浅克隆,其历史被截断为指定的修订数。浅存储库有一个数字 限制的(你不能克隆或从中获取,也不能从或推送 进去)

那么...我如何才能摆脱这种情况并将我的代码推送到 Github?

【问题讨论】:

  • 有什么问题?你尝试了什么?
  • 供将来参考:git 现在支持从浅克隆推送(从 1.9 版开始)。当推送到比您的历史更早的过时分叉时,它仍然会失败。

标签: git heroku github shallow-clone


【解决方案1】:

Git(从 1.8.3 开始)现在有一种官方方法来获取浅克隆的完整历史记录:

git fetch --unshallow

来自git fetch documentation:

--unshallow

如果源存储库是完整的,则将浅层存储库转换为完整的存储库,消除浅层存储库施加的所有限制。

如果源存储库很浅,请尽可能多地获取,以便当前存储库与源存储库具有相同的历史记录。

【讨论】:

    【解决方案2】:

    我不同意接受的答案有两个原因:

    1. 失败和忘记文件的原因有很多
    2. 您丢失了提交消息和历史记录

    以下是我的建议:

    嫁接点

    你应该有一个带有移植点的 $GIT_DIR/.git/shallow 文件。如果历史足够简单,即使文档另有说明,这个移植点也应该允许您推动。

    补丁

    这允许您保留提交历史等:

    git format-patch origin..master
    

    然后克隆原点并重新申请:

    git clone origin_path
    cp shallow_clone/*.patch deep_clone
    cd deep_clone
    git am *.patch
    

    这次你可以推了!

    git push
    

    【讨论】:

    • 虽然复制粘贴我的答案到你的答案是很蹩脚的
    • 就我而言,它给出了错误“ does not exist in the index”
    • 我也将此解决方案应用于托管在 github 中的 angular-seed 项目,该项目是使用 --depth=1 克隆的。当尝试推送到不同的远程目标存储库时,我收到错误“![远程拒绝] master -> master (shallow update not allowed)”。使用目标 repo 作为来源效果很好。
    • `"你应该有一个带有移植点的 $GIT_DIR/.git/shallow 文件。" -- 如何创建?
    【解决方案3】:

    如果您在浅层克隆中工作并且缺少历史记录导致问题,您可以使用 --depth 选项获取更多历史记录。

    git fetch --depth=20
    

    其中 20 是要获取的提交数量。如果这还不够,请增加它。

    您还可以将--depth 选项与git pull 一起使用。

    【讨论】:

      【解决方案4】:

      选项 1) 如果您仍然拥有原始仓库,请在推送之前从中获取:

      git fetch --unshallow
      

      选项 2) 注意!这仅推荐用于新的回购,因为这会导致历史丢失,而且很容易发生冲突!

      如果您已经删除了从中提取的存储库, 您需要丢弃所有历史记录。

       git filter-branch -- --all
       git push
      

      git filter-branch:让你重写 Git 修订历史

      -- :将过滤器分支选项与修订选项分开

      --all:重写所有分支和标签

      【讨论】:

      • 请编辑您的答案并解释为什么这是问题的答案。纯代码答案不是很有帮助。
      • 啊,谢谢兄弟!这是最好的答案,不需要搞乱原始获取我已经删除的内容!
      【解决方案5】:

      我在将浅层克隆存储库推送到 Bitbucket 服务器时遇到了类似的问题,并且我无法访问旧历史记录。最后,我找到了解决方案。请参阅下面的带有 cmets 的示例脚本:

      #!/bin/bash
      
      # Fix shallowness
      mv .git/shallow .git/info/grafts
      
      git checkout --orphan temp # create temp empty commit
      git reset --hard
      git commit -m "Init" --allow-empty
      
      # Replace all shallow commits ids with new commit id. I copy-paste all refs from shallow file 
      git replace 196cdbdb30e608aae2fd7cbe97cc8c0e6fa66c06 <commit_id_of_empty_init_above>
      git replace 4c645849b296aaafc1809a9e1537c0fb305167ad <commit_id_of_empty_init_above>
      git replace 50eab8bd8c416c47354331211b1efd8688ad8e97 <commit_id_of_empty_init_above>
      git replace 649dc7577b87d1b05dff05bf9adc5e46f6612dfa <commit_id_of_empty_init_above>
      git replace 6902148fde7b98ff0d6b6c6ebe929590322c95ff <commit_id_of_empty_init_above>
      git remote set-url origin http://<username>:<password>@<example.com:port/repo.git> # reference to a remote repo to push
      git push origin 'refs/replace/*' # push replace refs to remote repo first
      git push -u origin master # push to master, finally
      
      # Clear some garbage just in case
      git filter-branch --tag-name-filter cat -- --all # rewrite history
      git push --force origin
      git fsck # check that everything is ok
      

      【讨论】:

        猜你喜欢
        • 2021-05-31
        • 2015-02-21
        • 2020-07-04
        • 2013-08-04
        • 2018-12-02
        • 1970-01-01
        • 2011-10-17
        • 1970-01-01
        相关资源
        最近更新 更多