{"id":11063,"date":"2021-11-29T10:00:00","date_gmt":"2021-11-29T01:00:00","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=11063"},"modified":"2021-11-26T19:30:52","modified_gmt":"2021-11-26T10:30:52","slug":"deleting-env-file-back-from-git","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/11063","title":{"rendered":"Deleting .env file back from Git"},"content":{"rendered":"\n<p>Sometimes , we accidentally pushed a file with some username , password or secret tokens etc. It has a variety security concerns for your project and this should be removed back from Git.<\/p>\n\n\n\n<h3>Git ignore<\/h3>\n\n\n\n<p>First thing you need to do is to add the file you don&#8217;t want to push to git at .gitignore file. Add it in .gitignore , commit and push it. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Secret file\n.env<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>But this will not change any effects to .env as the file is already pushed before and .gitignore doesn&#8217;t untracked the already committed changes<\/p>\n\n\n\n<h3>Deleting the file<\/h3>\n\n\n\n<p>So we have to remove the specific file , .env in our case from the git remote repo entirely. We can use the following command. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git rm -r --cached .env<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>If we push the changes , the .env file will be remove from the remote repository<\/p>\n\n\n\n<h3>Git History<\/h3>\n\n\n\n<p>However, one more thing is left which is git histories. We can check the .env file contents also in git histories. So we need to remove the specific git history as well to hide the .env contents.<\/p>\n\n\n\n<p>To remove the file altogether, we can use like following.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git filter-branch --index-filter \"git rm -rf --cached --ignore-unmatch .env\" HEAD<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>When you push this time, you have to use &#8211;force option.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git push --force<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>If we look at our history, we can still see the commits that include .env file  but the content is empty.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>That&#8217;s all for now.<\/p>\n\n\n\n<p>Yuuma<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11063\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/11063\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Deleting .env file back from Git\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11063\" data-text=\"Deleting .env file back from Git\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11063\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11063\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes , we accidentally pushed a file with some username , password or secret tokens etc. It has a variety [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11063"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=11063"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11063\/revisions"}],"predecessor-version":[{"id":11066,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11063\/revisions\/11066"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=11063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=11063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=11063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}