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

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

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

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

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

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


这是暴力方法。它还删除了存储库的配置。

注意:如果存储库具有子模块,则此操作无效!如果您使用子模块,则应使用例如交互式rebase

步骤1:删除所有历史记录(确保您有备份,这无法恢复)

cat .git/config  # save your <github-uri> somewhere
rm -rf .git

步骤2:仅使用当前内容重建Git repo

在步骤2之前,如果您尚未设置init.defaultBranch配置,请通过git-config--globalinit.defaultBranch<branch-name>进行设置。在当前示例中,您可以选择main作为<branchname>

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

第三步:推送到GitHub。

git remote add origin <github-uri>
git push -u --force origin main

另一个选项是交互式rebase(假设您的git版本是>=1.7.12):gitrebase--root-i

当在编辑器中显示提交列表时:

将第一次提交的“pick”更改为“reword”每隔一次提交将“pick”更改为“fixup”

保存并关闭。Git将开始重新定基。

最后,您将有一个新的根提交,它是其后所有提交的组合。

这样做的好处是,您不必删除存储库,如果您有第二个想法,您总是有退路。

如果你真的想破坏你的历史记录,请将master重置为这个提交并删除所有其他分支。


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

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会给我带来一些冲突(因为我有很多历史,所以需要很长时间)。


下面的方法是完全可复制的,因此如果双方一致,则无需再次运行clone,只需在另一侧运行脚本即可。

git log -n1 --format=%H >.git/info/grafts
git filter-branch -f
rm .git/info/grafts

如果您想清理它,请尝试以下脚本:

http://sam.nipl.net/b/git-gc-all-ferocious

我为存储库中的每个分支编写了一个脚本“杀死历史”:

http://sam.nipl.net/b/git-kill-history

另请参见:http://sam.nipl.net/b/confirm


这是我喜欢的方法:

git branch new_branch_name $(echo "commit message" | git commit-tree HEAD^{tree})

这将创建一个带有一个提交的新分支,该提交将在HEAD中添加所有内容。它不会改变任何其他东西,所以它是完全安全的。


要从git中删除最后一次提交,只需运行

git reset --hard HEAD^ 

如果要从顶部删除多个提交,可以运行

git reset --hard HEAD~2 

删除最后两个提交。你可以增加数量以删除更多提交。

更多信息请点击此处。

Git tutoturial在这里提供了如何清除存储库的帮助:

您希望从历史记录中删除该文件,并将其添加到.gitignore以确保不会意外再次提交。例如,我们将从GitHub gem存储库中删除Rakefile。

git clone https://github.com/defunkt/github-gem.git

cd github-gem

git filter-branch --force --index-filter \
  'git rm --cached --ignore-unmatch Rakefile' \
  --prune-empty --tag-name-filter cat -- --all

既然我们已经从历史中删除了该文件,让我们确保不要不小心再犯了。

echo "Rakefile" >> .gitignore

git add .gitignore

git commit -m "Add Rakefile to .gitignore"

如果您对存储库的状态感到满意,您需要强制推送更改以覆盖远程存储库。

git push origin master --force

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

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

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


larsmans建议方法的变体:

保存未跟踪文件列表:

git ls-files --others --exclude-standard > /tmp/my_untracked_files

保存git配置:

mv .git/config /tmp/

然后执行larsmans的第一步:

rm -rf .git
git init
git add .

还原配置:

mv /tmp/config .git/

取消跟踪未跟踪的文件:

cat /tmp/my_untracked_files | xargs -0 git rm --cached

然后提交:

git commit -m "Initial commit"

最后推送到您的存储库:

git push -u --force origin master

您可以使用浅层克隆(git>1.9):

git clone --depth depth remote-url

进一步阅读:http://blogs.atlassian.com/2014/05/handle-big-repositories-git/


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

更具概念性的答案:

如果没有标记/分支/refs指向旧提交,git会自动垃圾收集旧提交。因此,您只需删除所有标记/分支,并创建一个新的孤立提交,与任何分支关联——按照惯例,您可以让分支主节点指向该提交。

除非使用低级git命令进行挖掘,否则任何人都不会再看到旧的、无法访问的提交。如果这对你来说足够了,我就停下来,让自动GC在它想做的时候做它的工作。如果你想马上摆脱它们,你可以使用gitgc(可能带有--aggressive--prune=all)。对于远程git存储库,除非您具有对其文件系统的shell访问权限,否则无法强制执行此操作。


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


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


只需删除Github repo并创建一个新的。迄今为止是最快、最简单和最安全的方法。毕竟,当您只需要一个提交的主分支时,在接受的解决方案中执行所有这些命令,您可以获得什么?


以下是根据@Zeelot的回答改编的脚本。它应该从所有分支中删除历史记录,而不仅仅是主分支:

for BR in $(git branch); do   
  git checkout $BR
  git checkout --orphan ${BR}_temp
  git commit -m "Initial commit"
  git branch -D $BR
  git branch -m $BR
done;
git gc --aggressive --prune=all

它符合我的目的(我没有使用子模块)。


干得好:

#!/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


这将删除主分支上的历史记录(您可能需要在运行命令之前进行备份):

git branch tmp_branch $(echo "commit message" | git commit-tree HEAD^{tree})
git checkout tmp_branch
git branch -D master
git branch -m master
git push -f --set-upstream origin master

这是基于@dan_waterworth的回答。


以下是清除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