我目前有一个本地Git存储库,我将其推送到Github存储库。

本地存储库有约10个提交,Github存储库是这一点的同步副本。

我想做的是从本地Git存储库中删除所有版本历史记录,这样存储库的当前内容将显示为唯一的提交(因此不会存储存储库中的旧版本文件)。

然后我想将这些更改推送到Github。

我已经研究过Git的rebase,但这似乎更适合删除特定版本。另一个可能的解决方案是删除本地回购,并创建一个新的回购-尽管这可能会产生大量工作!

ETA:有些特定的目录/文件未被跟踪-如果可能的话,我想保持这些文件的未跟踪。


当前回答

我通过从项目中删除.git文件夹并通过IntelliJ重新集成版本控制解决了类似的问题。注意:.git文件夹是隐藏的。您可以使用ls-a在终端中查看它,然后使用rm-rf.git删除它。

其他回答

gitfilter分支是主要的手术工具。

git filter-branch --parent-filter true -- @^!

--父过滤器获取stdin上的父对象,并应在stdout上打印重写的父对象;unix true成功退出,但不打印任何内容,因此:没有父级。@^!Git是“头部提交,而不是其任何父级”的简写。然后删除所有其他参考并在空闲时推送。

干得好:

#!/bin/bash
#
# By Zibri (2019)
#
# Usage: gitclean username password giturl
#
gitclean () 
{ 
    odir=$PWD;
    if [ "$#" -ne 3 ]; then
        echo "Usage: gitclean username password giturl";
        return 1;
    fi;
    temp=$(mktemp -d 2>/dev/null /dev/shm/git.XXX || mktemp -d 2>/dev/null /tmp/git.XXX);
    cd "$temp";
    url=$(echo "$3" |sed -e "s/[^/]*\/\/\([^@]*@\)\?\.*/\1/");
    git clone "https://$1:$2@$url" && { 
        cd *;
        for BR in "$(git branch|tr " " "\n"|grep -v '*')";
        do
            echo working on branch $BR;
            git checkout $BR;
            git checkout --orphan $(basename "$temp"|tr -d .);
            git add -A;
            git commit -m "Initial Commit" && { 
                git branch -D $BR;
                git branch -m $BR;
                git push -f origin $BR;
                git gc --aggressive --prune=all
            };
        done
    };
    cd $odir;
    rm -rf "$temp"
}

也在此处托管:https://gist.github.com/Zibri/76614988478a076bbe105545a16ee743

以下是清除Github存储库历史的步骤

首先,从.git中删除历史记录

rm -rf .git

现在,仅从当前内容重新创建git repo

git init
git add .
git commit -m "Initial commit"

推送到Github远程repo,确保您覆盖历史记录


git remote add origin git@github.com:<YOUR ACCOUNT>/<YOUR REPOS>.git
git push -u --force origin master

为此,请使用浅层克隆命令gitclone--深度1 URL-它将仅克隆存储库的当前HEAD

唯一适用于我(并保持子模块工作)的解决方案是

git checkout --orphan newBranch
git add -A  # Add all files and commit them
git commit
git branch -D master  # Deletes the master branch
git branch -m master  # Rename the current branch to master
git push -f origin master  # Force push master branch to github
git gc --aggressive --prune=all     # remove the old files

当我有子模块时,删除.git/总是会引起巨大的问题。使用gitrebase--root会给我带来一些冲突(因为我有很多历史,所以需要很长时间)。