使用当前的.gitignore使Git重建历史记录 [英] Make Git rebuild history using the current .gitignore

查看:114
本文介绍了使用当前的.gitignore使Git重建历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有使用.gitignore忽略项目中的文件,例如编译后的二进制文件,因此这些编译后的二进制文件已提交到Git存储库中.现在,我将.gitignore添加到我的项目中.我想重建整个Git存储库,而忽略.gitignore中列出的文件,而不会丢失任何历史记录.我该怎么办?

I did not use .gitignore to ignore files such as compiled binaries in my project, so these compiled binaries have been committed into the Git repository. And now I add .gitignore to my project. I want to rebuild the whole Git repo ignoring the files listed in .gitignore without losing any history. How can I do that?

推荐答案

如果刚刚添加了二进制文件,则从当前缓存中将其删除.

Remove those binary from the current cache if they were just added.

git rm --cached abinary

然后您的.gitignore将自动忽略那些二进制文件.

Then your .gitignore will automatically ignores those binaries.

但是,如果它们是在过去的提交中提交的,则不会丢失历史记录,但是您仍然需要从所述提交中删除那些二进制文件.
您将需要 git filter-branch

But if they were committed in past commits, You won't loose history, but you still need to remove those binaries from said commit.
You will need either git filter-branch or bfg repo cleaner.

这将重写您的历史记录,并且您将需要一个git push --force,因此,如果您有多个(您自己)的协作者,请提前警告他们.

That will rewrite your history, and you will need a git push --force, so if you have more than one (yourself) collaborator, warn them in advance.

这篇关于使用当前的.gitignore使Git重建历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆