【问题标题】:Cloning a Git repo into a new repo - without commit history将 Git 存储库克隆到新存储库中 - 没有提交历史记录
【发布时间】:2015-01-12 05:33:07
【问题描述】:

所以我试图了解 Git,并且需要(为什么我有这种需要,我不会深入探讨)能够通过命令行从一个 repo 中获取文件并将它们放入进入一个全新的存储库,而无需使用它之前的所有提交历史。

我现在要做的是git init 创建一个新的存储库,然后使用克隆或子树从我现有的存储库中获取文件(我还没有完全理解这一点所以可能会叫错树),然后添加、提交,然后将它们推送到我的新存储库。

git init
--- get the files from stock repo ---
git add .
git commit -m 'Initial commit'
git remote add origin <url of my new repo>
git push -u origin master

我的存储库都在 Bitbucket 上,如果这有所作为,

【问题讨论】:

  • 你能不能把文件rsync到别的地方然后git init那里?
  • 是的,如果回购中有太多脏话等看起来不专业:P

标签: git command-line bitbucket


【解决方案1】:

如果您在某处克隆了股票回购中的这些文件,您可以执行以下操作:

cd yourNewRepo
git --work-tree=/path/to/stock/repo add .
git commit -m 'Initial commit'

这意味着:您正在考虑将来自 /path/to/stock/repo 的文件作为您新 repo 的工作树(仅适用于 git add 步骤)

一旦您的新存储库的索引记录了这些文件,您就可以忘记库存存储库,并在新存储库中提交这些新文件(没有任何先前的历史记录)。

【讨论】:

    【解决方案2】:

    您可以像这样克隆旧存储库:

    git clone git@bitbucket.org:username/repository.git
    

    然后你删除 git 目录:

    rm -rf .git/
    

    现在您创建新的存储库:

    git init
    

    【讨论】:

      【解决方案3】:

      如果您已经克隆了 TMI 存储库,您可以重置您的克隆的 master 以仅使用该人的提示提交历史记录

      git branch -f master `git commit-tree -m "My new initial commit" origin/master^{tree}`
      

      然后像往常一样,修好遥控器,随心所欲地推送历史记录。

      (edit:如果你有 master 签出 git branch 不会想重写 ref,但你根本不接触内容,所以你可以绕过所有瓷器的把手,就去做吧

      git update-ref -m "Truncating history" refs/heads/master \
              `git commit-tree -m "My new initial commit" origin/master^{tree}`
      

      )

      【讨论】:

        【解决方案4】:
        1. 您像这样克隆旧存储库:

          git clone git@bitbucket.org:username/repository.git

        2. 然后你删除git目录:

          rm -rf .git

        3. 创建新的存储库:

          git init

        4. 提交新的存储库并将其推送到主源

          git push -u origin master

        如果可能,请检查 git 日志的状态以验证您记录的信息是否正确。

        【讨论】:

        • rm -rf 在接受的答案中比rm -R 效果更好
        • 为什么还要复活这个老问题?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-06
        • 2023-03-31
        • 2022-08-07
        • 2019-08-27
        • 1970-01-01
        • 1970-01-01
        • 2011-08-25
        相关资源
        最近更新 更多