怎么告诉'git''忘记'所有先前的提交?

jpw*_*ynn 6 git

我需要根据我的另一个项目的一小部分开始一个新项目,我的回购是在github.

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

我删除了我不需要的一切,摆脱了旧的迁移等......好吧.

现在它在本地运行正常,但"git log"显示所有旧提交.

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

我读到了关于git rebase但是不清楚这是否是正确的命令,如果是这样,如何将它用于这个非常简单的目的.

ori*_*rip 12

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

  • +1,但如果您有任何特定的回购特定的东西,你将不得不添加回来. (2认同)

Mar*_*air 9

这里接受的答案真的让我感到害怕 - 删除你的.git目录是一件非常艰巨的事情,而且这里没有必要.

此脚本使用更安全的方法 - 它创建一个分支old-master,在开始压缩提交之前记录您所在的位置,然后使用git reset --soft $ROOT_COMMIT并将git commit --amend整个分支压缩到一个提交.您也可以从这个GitHub要点下载脚本.

#!/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"
Run Code Online (Sandbox Code Playgroud)