简体   繁体   English

如何从git上的filter-branch命令中删除重复的提交?

[英]How to remove duplicated commits from filter-branch command on git?

I have a django project that I've kept private for a long period of time. 我有一个django项目,我已经保密了很长一段时间。 Through the lifespan of the project I've had settings.py , base_settings.py , and secret_settings.py files with sensitive information. 在整个项目的生命周期中,我有secret_settings.py包含敏感信息的settings.pybase_settings.pysecret_settings.py文件。 Now I've decided to make the code open source as I am no longer actively working on the project. 现在我决定让代码开源,因为我不再积极地从事这个项目。

I ran the commands below to remove the history of these files to make sure all sensitive information was gone. 我运行下面的命令来删除这些文件的历史记录,以确保所有敏感信息都消失了。

git filter-branch -f --tree-filter 'rm -f exchange/secret_settings.py' HEAD
git filter-branch -f --tree-filter 'rm -f exchange/base_settings.py' HEAD
git filter-branch -f --tree-filter 'rm -f exchange/settings.py' HEAD

However, github warned me that there was still commits that had .py~ files with AWS information in them so I ran: 但是,github警告我,仍有提交的.py~文件包含AWS信息,所以我运行:

git filter-branch -f --tree-filter 'rm -f exchange/settings.py~' HEAD

I have secured my AWS account and removed all the secret keys from AWS so my account is secure. 我已经保护了我的AWS账户并从AWS中删除了所有密钥,因此我的账户是安全的。 However, now I have duplicated commits, sometimes 3 or 4, for my entire commit history. 但是,现在我的整个提交历史记录中都有重复的提交,有时是3或4。

I've found various answers saying I can revert back to some .git/refs backups but that doesn't seem to be working. 我找到了各种答案,说我可以恢复一些.git/refs备份,但这似乎不起作用。

Here is the project: https://github.com/ProgrammingJoe/Texchange 这是项目: https//github.com/ProgrammingJoe/Texchange

Does anyone know what I can do to fix this? 有谁知道我能做些什么来解决这个问题?

I ended up doing some more googling after jhill's comment and I ran the following commands on a new branch: 在jhill的评论之后我最终做了一些谷歌搜索,我在一个新的分支上运行了以下命令:

git filter-branch -f --tree-filter 'rm -f exchange/settings.py' HEAD       
git filter-branch -f --tree-filter 'rm -f exchange/settings.py~' HEAD       
git filter-branch -f --tree-filter 'rm -f exchange/base_settings.py' HEAD       
git filter-branch -f --tree-filter 'rm -f exchange/base_settings.py~' HEAD       
git filter-branch -f --tree-filter 'rm -f exchange/secret_settings.py' HEAD       
git filter-branch -f --tree-filter 'rm -f exchange/secret_settings.py~' HEAD       

After that, my new branch was back where I wanted it with around 300 commits. 在那之后,我的新分支回到了我希望它的约300个提交。 I then had to figure out how to completely overwrite master with this other branch. 然后,我必须弄清楚如何用另一个分支完全覆盖master。 In good practice, I cloned my repo in another location as a backup. 在良好的实践中,我将我的仓库克隆在另一个位置作为备份。 I then tried every combination of force pushing and merging I could to overwrite the master branch and nothing worked. 然后我尝试了推力和合并的每一个组合,我可以覆盖主分支,没有任何效果。 I then asked this question: Github, my branch is behind master but it is correct. 然后我问了这个问题: Github,我的分支是主人,但它是正确的。 How do I overwrite master with the branch? 如何用分支覆盖master?

I then realized that some of my older commits in master were where I wanted them so I ran: 然后我意识到我在master中的一些旧提交是我想要的地方所以我跑了:

git checkout master
git reset --hard correct_commit_id
git push -f origin master

This fixed all my issues but I accidentally deleted one of the files I used to have. 这解决了我的所有问题,但我不小心删除了我曾经拥有的一个文件。 Good thing I had a backup :). 好东西,我有一个备份:)。 I'm not sure why git reset --hard fixed_branch didn't work in the past. 我不确定为什么git reset --hard fixed_branch过去不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM