avatarTeri Radichel

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4519

Abstract

eem to be all programming languages. There’s nothing for AWS or operating systems.</p><p id="9357">You can find a list of common .gitignore templates here:</p><div id="b89f" class="link-block"> <a href="https://github.com/github/gitignore"> <div> <div> <h2>GitHub - github/gitignore: A collection of useful .gitignore templates</h2> <div><h3>This is GitHub's collection of file templates. We use this list to populate the .gitignore template choosers available…</h3></div> <div><p>github.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*Hl3B--aLdvYZM8dy)"></div> </div> </div> </a> </div><p id="e961">You can find lots other examples of .gitignore files by searching around for them.</p><p id="d281">On your local machine you can create a global .gitignore file for all repositories like this:</p><div id="3313"><pre><span class="hljs-meta prompt_"> </span><span class="language-bash"><span class="hljs-built_in">touch</span> ~/.gitignore</span> <span class="hljs-meta prompt_"> </span><span class="language-bash">git config --global core.excludesFile ~/.gitignore</span></pre></div><p id="adfc">or ignore a previously committed file (but it will still be in git’s history):</p><div id="d389"><pre><span class="hljs-symbol">$</span> echo debug.<span class="hljs-built_in">log</span> >> .gitignore

<span class="hljs-symbol">$</span> git rm --cached debug.<span class="hljs-built_in">log</span> rm <span class="hljs-string">'debug.log'</span>

<span class="hljs-symbol">$</span> git commit -m <span class="hljs-string">"Start ignoring debug.log"</span></pre></div><p id="4f22">As explained here:</p><div id="c8db" class="link-block"> <a href="https://www.atlassian.com/git/tutorials/saving-changes/gitignore"> <div> <div> <h2>.gitignore file - ignoring files in Git | Atlassian Git Tutorial</h2> <div><h3>Git sees every file in your working copy as one of three things: tracked - a file which has been previously staged or…</h3></div> <div><p>www.atlassian.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/)"></div> </div> </div> </a> </div><p id="ea72">For now I’m going to automatically add a .gitignore file to any new repo I create by adding it the script I wrote to create a new repository in git.</p><p id="fc16">I added the following after the repository is deleted and before deleting the token.</p><figure id="c309"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*9FiOMbpJS59Rqdt8KrPEvA.png"><figcaption></figcaption></figure><p id="09fa">Now, I want to see if I have the same problem I had in the last post.</p><div id="9cd2" class="link-block"> <a href="https://readmedium.com/create-a-new-github-repository-using-the-command-line-ed197a788052"> <div> <div> <h2>Create a New GitHub Repository Using the Command Line</h2> <div><h3>ACM.228 git command line versus the git CLI and related credential issues</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*b7uktPAgKq1sfWdYbMDE3g.png)"></div> </div> </div> </a> </div><h2 id="4538">A create_repository function that includes adding the gitignore</h2><p id="92f1">But before I test it I’m going to do one other thing. I’m going to turn this script into a function. That way I can source it and call it from another directory.</p><figure id="2d02"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Wsb_RJDZOEDdWKZX0JmwtA.png"><figcaption></figcaption></figure><h2 id="bbbf">Aligning the new repository with the main branch</h2><p id="8886">That didn’t work. I wrote about a few errors I got trying to create a repo and push files:</p><div id="61be" class="link-block"> <a href="https://readmedium.com/pathspec-repository-did-not-match-any-file-s-known-to-git-b4c02842aa70"> <div> <div> <h2>pathspec ‘repository’ did not match any file(s) known to g

Options

it</h2> <div><h3>Nonsense git error message based on the actual problem</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*4oxP4LXk8l8c3mpRvO7ejg.png)"></div> </div> </div> </a> </div><div id="ec70" class="link-block"> <a href="https://readmedium.com/error-src-refspec-master-does-not-match-any-71b0e0750e38"> <div> <div> <h2>error: src refspec master does not match any.</h2> <div><h3>Similar to a prior git error when trying to push files but not exactly</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*4oxP4LXk8l8c3mpRvO7ejg.png)"></div> </div> </div> </a> </div><p id="ebea">Here’s what finally worked:</p><figure id="bc95"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*2CiqC_u1pT6rYqbGjDbzGg.png"><figcaption></figcaption></figure><p id="6f35">I did a pull to get the readme file before adding the new file and I got aligned with the correct origin. Then I create add, and push the file in the normal way. That way I can avoid rebase, merge, etc.</p><p id="a9ef">Now every repo I create will have a .gitignore file with whatever standard excludes I want to add here.</p><p id="822f">I could also have created a separate file and then included the contents. I the .gitignore file ends up being longer then I might do that later.</p><h2 id="af5b">Calling the create_repository function</h2><p id="945a">I added the code to call my function to the file below. I can execute this script to create a static website, including creating the repository for the site and all the AWS resources it requires.</p><figure id="8608"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*bbTaDzMmZn5RnVt48D3cNQ.png"><figcaption></figcaption></figure><h2 id="175a">Global gitignore options</h2><p id="f34f">One note on .gitingore files — make sure they are not allowing sensitive files in subfolders within youre repository. One way to ensure you disallow sensitive files from all repositories is to use a global git ignore file.</p><p id="0587">Insert something you don’t want to check in into the global file like this:</p><div id="f4a7"><pre><span class="hljs-keyword">echo</span> *<span class="hljs-string">.pem</span> >> ~<span class="hljs-string">/.gitignore_global</span></pre></div><p id="3d21">Then apply the rule globally on your local machine.</p><div id="5ff9"><pre>git config <span class="hljs-params">--global</span> core.excludesfile ~<span class="hljs-string">/.gitignore_global</span></pre></div><p id="3fe4">Follow for updates.</p><p id="4a3a">Teri Radichel | <i>© <a href="https://2ndsightlab.com/?source=post_page---------------------------">2nd Sight Lab</a> 2023</i></p><div id="8b5f"><pre><span class="hljs-section">About Teri Radichel:

⭐️ Author: Cybersecurity Books
⭐️ Presentations: Presentations by Teri Radichel
⭐️ Recognition: SANS Award, AWS Security Hero, IANS Faculty
⭐️ Certifications: SANS ~ GSE 240
⭐️ Education: BA Business, Master of Software Engineering, Master of Infosec
⭐️ Company: Penetration Tests, Assessments, Phone Consulting ~ 2nd Sight Lab</pre></div><div id="caae"><pre><span class="hljs-section">Need Help With Cybersecurity, Cloud, or Application Security?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</span>
🔒 Request a penetration test or security assessment
🔒 Schedule a consulting call
🔒 Cybersecurity Speaker for Presentation</pre></div><div id="5a42"><pre>Follow <span class="hljs-keyword">for</span> more stories like <span class="hljs-keyword">this</span>:

❤️ Sign Up my Medium Email List ❤️ Twitter: <span class="hljs-meta">@teriradichel</span> ❤️ LinkedIn: https:<span class="hljs-comment">//www.linkedin.com/in/teriradichel</span> ❤️ Mastodon: <span class="hljs-meta">@teriradichel</span><span class="hljs-meta">@infosec</span>.exchange ❤️ Facebook: 2nd Sight Lab ❤️ YouTube: @2ndsightlab</pre></div><figure id="faf5"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*H9Ew1KCl-29nZiPR.jpeg"><figcaption></figcaption></figure></article></body>

Preventing Sensitive Files in GitHub with a .gitignore file

ACM.233 Keeping sensitive data out of GitHub by default

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

⚙️ Part of my series on Automating Cybersecurity Metrics. The Code.

🔒 Related Stories: Application Security | GitHub Security

💻 Free Content on Jobs in Cybersecurity | ✉️ Sign up for the Email List

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I’m writing some code to automate the deployment of an S3 bucket on AWS. In the last post I considered how to migrate files from S3 to my new bucket. I had some issues checking in the files.

Now I’m going to use some code I created previously to automatically create an GitHub repository and add a .gitignore file to every new repository. I resolved the error I got in the last post.

A .gitignore file is a file that you can add to your git repository to disallow certain files to be added to it. You can enter a list of file names or various wildcard characters to the file.

What you put in your .gitignore file depends on your environment and the application development tools you are using. Here are some types of eils you might want to exclude in differnet scenarios:

  • when using AWS, SSH, or encryption technologies you might want to disallow anyfile that ends with .pem.
  • When using .NET there’s a local, personal configuration file that you should not check in. Otherwise the next person that checks out the code will have their personal settings overwritten.
  • On Linux, if you have a file open and your system shuts down, a hidden cache still exists in the directory.
  • On Macs, there’s a type of hidden file that hangs around in directories that you don’t need or want in your git repos.
  • In Python temp files exist in some circumstances.

So you’ll want to set up your .gitignore file according to the types of files you want to prevent getting into the repository. Those seem to be all programming languages. There’s nothing for AWS or operating systems.

You can find a list of common .gitignore templates here:

You can find lots other examples of .gitignore files by searching around for them.

On your local machine you can create a global .gitignore file for all repositories like this:

$ touch ~/.gitignore
$ git config --global core.excludesFile ~/.gitignore

or ignore a previously committed file (but it will still be in git’s history):

$ echo debug.log >> .gitignore
  
$ git rm --cached debug.log
rm 'debug.log'
  
$ git commit -m "Start ignoring debug.log"

As explained here:

For now I’m going to automatically add a .gitignore file to any new repo I create by adding it the script I wrote to create a new repository in git.

I added the following after the repository is deleted and before deleting the token.

Now, I want to see if I have the same problem I had in the last post.

A create_repository function that includes adding the gitignore

But before I test it I’m going to do one other thing. I’m going to turn this script into a function. That way I can source it and call it from another directory.

Aligning the new repository with the main branch

That didn’t work. I wrote about a few errors I got trying to create a repo and push files:

Here’s what finally worked:

I did a pull to get the readme file before adding the new file and I got aligned with the correct origin. Then I create add, and push the file in the normal way. That way I can avoid rebase, merge, etc.

Now every repo I create will have a .gitignore file with whatever standard excludes I want to add here.

I could also have created a separate file and then included the contents. I the .gitignore file ends up being longer then I might do that later.

Calling the create_repository function

I added the code to call my function to the file below. I can execute this script to create a static website, including creating the repository for the site and all the AWS resources it requires.

Global gitignore options

One note on .gitingore files — make sure they are not allowing sensitive files in subfolders within youre repository. One way to ensure you disallow sensitive files from all repositories is to use a global git ignore file.

Insert something you don’t want to check in into the global file like this:

echo *.pem >> ~/.gitignore_global

Then apply the rule globally on your local machine.

git config --global core.excludesfile ~/.gitignore_global

Follow for updates.

Teri Radichel | © 2nd Sight Lab 2023

About Teri Radichel:
~~~~~~~~~~~~~~~~~~~~
⭐️ Author: Cybersecurity Books
⭐️ Presentations: Presentations by Teri Radichel
⭐️ Recognition: SANS Award, AWS Security Hero, IANS Faculty
⭐️ Certifications: SANS ~ GSE 240
⭐️ Education: BA Business, Master of Software Engineering, Master of Infosec
⭐️ Company: Penetration Tests, Assessments, Phone Consulting ~ 2nd Sight Lab
Need Help With Cybersecurity, Cloud, or Application Security?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
🔒 Request a penetration test or security assessment
🔒 Schedule a consulting call
🔒 Cybersecurity Speaker for Presentation
Follow for more stories like this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
❤️ Sign Up my Medium Email List
❤️ Twitter: @teriradichel
❤️ LinkedIn: https://www.linkedin.com/in/teriradichel
❤️ Mastodon: @teriradichel@infosec.exchange
❤️ Facebook: 2nd Sight Lab
❤️ YouTube: @2ndsightlab
Git
Gitignore
Sensitive Data
Code
Security
Recommended from ReadMedium