Ignorance is bliss: the .gitignore file

I don't focus on what I'm up against. I focus on my goals and I try to ignore the rest. Venus Williams

There is a feature of git that I am learning to appreciate more and more: the .gitignore file. It’s not very important if you are working on something all by your onesies, but as you add more people to a git project it becomes increasingly important. Mostly it’s a protection for people just getting started with git to prevent unintended consequences arising out of something unanticipated. This is especially important on CI/CD projects, where an ounce of prevention really does equate to a pound of cure (which is supposedly an idiom that can trace its roots to Benjamin Franklin).

I’m doing this on Windows 10 with VS Code.

Local .gitignore file

To set up a local .gitignore file enter this in a prompt:

mkdir test_gitignore
cd .\test_gitignore\
git init

and if a remote exits then push the branch:

git push

There’s a sample one here which is pretty comprehensive for my needs.

Global .gitignore

Git also supports a global .gitignore file. In PowerShell the recommended location is $Env: :USERPROFILE\.gitignore — so to create the file simply enter (in PowerShell)

cd $Env: :USERPROFILE\.gitignore
ni .gitignore

Note: that ni in PowerShell means New-Item.

After that, populate the .gitignore file with your files that you want to always ignore the local system. Good candidates are IDE files (e.g., VS Code, Eclipse, NetBeans, etc.), and on Windows Thumbs.db is a popular one.

More info on stackoverflow.

Remote Servers

Git allows both server side and local repository .gitignore files, which can get rather confusing! There is one for GitHub (https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files) and another for GitLab (https://docs.gitlab.com/ee/api/templates/gitignores.html) and I’m sure there are more that I’m not familiar with.

Credits

https://gist.github.com/subfuzion/db7f57fff2fb6998a16c

https://www.w3schools.com/git/git_ignore.asp?remote=github

https://zellwk.com/blog/gitignore/

https://www.pluralsight.com/guides/how-to-use-gitignore-file

https://github.com/github/gitignore

https://www.educative.io/answers/what-is-the-powershell-equivalent-of-touch

https://git-scm.com/docs/gitignore#_pattern_format

https://git-scm.com/docs/gitignore#_examples

Previous
Previous

Rewriting History: git interactive rebase

Next
Next

Introduction to Conda