github删除敏感信息

最近写了个爬虫程序,用于爬取某些网站的二手房售卖信息https://github.com/farrellcn/ScrapyHouseSales,为了维护方便,把代码传到了GitHub上,传完了才想起来一些配置文件里包含了很多个人信息,包括邮箱用户名密码,赶紧修改了再commit上去,但这时在GitHub上已经留下了修改记录了。

于是开始找方法将历史记录从github上删除,搜了一下,找到的第一篇是《animation更新1.1-5,及彻底删除Git文件历史记录的办法》,但是文章里对要执行的command写的不详细,且没有注解,但是作者给出了GitHub官方的Removeing sensitive data帮助页链接,于是打开官方的看看写的是否详细点。

前两步就不说了,关键的第三步:

Run the following command, replacing PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA with thepath to the file you want to remove, not just its filename. These arguments will:

  • Force Git to process, but not check out, the entire history of every branch and tag
  • Remove the specified file, as well as any empty commits generated as a result
  • Overwrite your existing tags
    $ git filter-branch --force --index-filter \
    'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA' \
    --prune-empty --tag-name-filter cat -- --all
    > Rewrite 48dc599c80e20527ed902928085e7861e6b3cbe6 (266/266)
    > Ref 'refs/heads/master' was rewritten

Note: If the file with sensitive data used to exist at any other paths (because it was moved or renamed), you must run this command on those paths, as well.

这步的作用是把你目标文件从本地删除,并清空与这个文件有关的历史和tag。

注意:在Windows下上面的单引号要改为双引号,PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA是你要彻底删除的文件路径和文件名。

第四步:

Add your file with sensitive data to .gitignore to ensure that you don't accidentally commit it again.

$ echo "YOUR-FILE-WITH-SENSITIVE-DATA" >> .gitignore
$ git add .gitignore
$ git commit -m "Add YOUR-FILE-WITH-SENSITIVE-DATA to .gitignore"
> [master 051452f] Add YOUR-FILE-WITH-SENSITIVE-DATA to .gitignore
>  1 files changed, 1 insertions(+), 0 deletions(-)

其实这步可以省略,这步的目的是防止你以后不小心将删除的文件再传上去。

具体步骤是增加一个.gitignore文件,并将你要删除的文件路径和文件名添加进去,然后将.gitignore commit到远程仓库。

这样你以后再向项目里add这个文件的时候,就会提示不能添加,但是可以通过--force参数强制添加。

第六步:

Once you're happy with the state of your repository, force-push your local changes to overwrite your GitHub repository, as well as all the branches you've pushed up:

git push origin --force --all
Counting objects: 1074, done.
Delta compression using 2 threads.
Compressing objects: 100% (677/677), done.
Writing objects: 100% (1058/1058), 148.85 KiB, done.
Total 1058 (delta 590), reused 602 (delta 378)
To https://github.com/YOUR-USERNAME/YOUR-REPOSITORY.git
 + 48dc599...051452f master -> master (forced update)

这步是将之前步骤在本地仓库所做的修改提交到远程仓库上去。

第七步:

Once you're happy with the state of your repository, force-push your local changes to overwrite your GitHub repository, as well as all the branches you've pushed up:

git push origin --force --tags
Counting objects: 321, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (166/166), done.
Writing objects: 100% (321/321), 331.74 KiB | 0 bytes/s, done.
Total 321 (delta 124), reused 269 (delta 108)
To https://github.com/YOUR-USERNAME/YOUR-REPOSITORY.git
 + 48dc599...051452f master -> master (forced update)
这步的作用是将第三步中对tags的操作push到远程仓库,如果你之前没有对仓库做过添加tag操作,那么这步可以跳过。

添加tag相当于在当前时间点给仓库打了个基线,即使你删除了所有分支下的目标文件,但当你选择某个tag后,还是会看到已经被删除了的目标文件。

我在删除config.ini之前给仓库添加了一个v1.0的tag,经过第六步以后,可以看到master下的config.ini已经没有了。


但当我选择tags-v1.0以后,可以看到config.ini这个文件还是存在的,所以我们要执行上面的git push origin --force --all,将目标文件从所有tags中删除。



删除之后的效果:


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值