【问题标题】:how tell 'git' to 'forget' ALL prior commits?如何告诉“git”“忘记”所有先前的提交?
【发布时间】:2011-07-13 02:05:42
【问题描述】:

我需要基于我的另一个项目的一小部分开始一个新项目,该项目的 repo 位于 github。

我做了一个 git clone 到一个新的项目文件夹中......很好。

我删除了我不需要的所有内容,摆脱了旧的迁移等......很好。

现在它在本地运行良好,但“git log”显示所有旧提交。

我想告诉 git “忘记所有之前的提交,这是一个新项目,所以忘记之前的一切,从现在开始作为第一次提交”

我读过关于 git rebase 的文章,但不清楚这是否是正确的命令,如果是,如何将它用于这个非常简单的目的。

【问题讨论】:

    标签: git


    【解决方案1】:

    删除.git文件夹,运行git initgit add

    【讨论】:

    • +1,但如果你有任何 repo 特定的东西,比如遥控器,你必须把它们加回来。
    • @vcsjones:或者你可以在删除其余部分之前复制出.git/config
    • 存在添加应该被忽略的文件的风险。忽略列表位于 4 个(或更多?)不同的位置,包括在 .git/ 下。所以我会在 rm -rf .git 动作之前做一个 git clean -dfx .
    【解决方案2】:

    这里接受的答案真的让我害怕 - 删除您的 .git 目录是一件很激烈的事情,在这里没有必要。

    此脚本使用一种更安全的方法 - 它创建一个名为 old-master 的分支来记录您在开始压缩提交之前的位置,然后使用 git reset --soft $ROOT_COMMITgit commit --amend 将整个分支压缩为一个提交。你也可以从这个GitHub gist下载脚本。

    #!/bin/bash
    
    # A script that squashes your entire current branch down to a single commit,
    # if this repository has a single root commit.  This will change the object
    # name of the root commit.
    
    if [ -n "$(git status --porcelain)" ]
    then
        echo "git status wasn't clean - refusing to run..."
        exit 1
    fi
    
    # From: http://stackoverflow.com/questions/1006775/
    root_commits () {
       git rev-list --parents HEAD | egrep "^[a-f0-9]{40}$"
    }
    
    NUMBER_OF_ROOT_COMMITS=$(root_commits|wc -l)
    
    if (( "$NUMBER_OF_ROOT_COMMITS" > 1 ))
    then
        echo "This script won't work when you have multiple root commits"
        exit 1
    fi
    
    ROOT_COMMIT=$(root_commits)
    
    if [ -z "$ROOT_COMMIT" ]
    then
        echo "No root commit was found!"
        exit 1
    fi
    
    set -e
    set -x
    
    # Create a branch based on the current HEAD for safety:
    git branch old-master
    
    # Reset the branch to the root commit, leaving the previous
    # state of the tree staged:
    git reset --soft $ROOT_COMMIT
    
    # Now amend the root commit with the state of the index:
    git commit --amend -m "The branch restarted"
    

    【讨论】:

      【解决方案3】:

      你试过reposurgeon吗?

      http://www.catb.org/~esr/reposurgeon/

      【讨论】:

      • 那么...这可以让您做git rebasegit filter-branch 已经可以做的事情? (此外,对于 OP 想要的东西来说,这太过分了。)不过,这个想法很有趣。
      【解决方案4】:

      为什么不直接将该目录复制到一个全新的 git 存储库并在那里提交?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-19
        • 2018-11-05
        • 2016-03-01
        • 1970-01-01
        • 2022-10-24
        • 1970-01-01
        • 2016-04-27
        • 1970-01-01
        相关资源
        最近更新 更多