All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is .gitignore? What is it for and how to use it

Post cover: What is .gitignore? What is it for and how to use it
Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
.gitignore is a file used in the Git version control system to specify files and directories that should be ignored by Git. This means that files and directories listed in .gitignore will not be added to the Git index, will not be tracked, and will not be included in commits.

Why is .gitignore needed?

The .gitignore file helps avoid including files in the repository that are not related to the code or should not be publicly accessible. These can include:
  • Development environment configuration files.
  • Log files.
  • Generated files (e.g., compilation results).
  • Temporary files (cache, temporary files).
  • Sensitive data (e.g., passwords or access keys).

Example of using .gitignore

Creating a .gitignore file:
In the root directory of your repository, create a file named .gitignore.
Adding files and directories to .gitignore:
Add patterns for files and directories that should be ignored in the .gitignore file. For example:
# Ignore all .DS_Store files
.DS_Store

# Ignore log files
*.log

# Ignore all files in the temp/ directory
temp/

# Ignore IDE configuration files
.idea/
*.iml
Applying .gitignore:
After adding entries to .gitignore, make sure that these files have not already been added to the index. If they have already been added, remove them from the index using the command git rm --cached <filename> and make a commit.

How does .gitignore work?

  • Each line in .gitignore represents a pattern for files that should be ignored.
  • Patterns can be simple file names or contain wildcard characters for greater flexibility.
  • Comments can be added using the # symbol.
  • .gitignore can be located in any directory of the repository, and its effect will be limited to that directory and its subdirectories. This is convenient, sometimes very convenient.
By the way, you won't see the .gitignore file in Finder (only in the code editor or in the terminal by executing ls -a). Read in the previous post for more details about hidden files (with a dot at the beginning).
An example of a .gitignore file for a Ruby on Rails project
# Ignore bundler config
/.bundle

# Ignore the default SQLite database
/db/*.sqlite3
/db/*.sqlite3-journal
/db/*.sqlite3-shm
/db/*.sqlite3-wal

# Ignore the default PostgreSQL and MySQL database files
/db/*.pg
/db/*.pg-journal
/db/*.mysql
/db/*.mysql-journal

# Ignore all logfiles and tempfiles
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep

# Ignore uploaded files in development
/storage/*
!/storage/.keep

# Ignore application secret key
/config/master.key

# Ignore pidfiles, tempfiles, and byebug command history
/tmp
/tmp/pids
/tmp/cache
/tmp/sockets
/.byebug_history

# Ignore dotenv environment variable files
.env
.env.*

# Ignore node_modules
/node_modules
/yarn-error.log

# Ignore precompiled assets
/public/assets
/public/packs
/public/packs-test
/public/packs-dev

# Ignore compiled files
/public/assets
/public/packs
/public/packs-test
/public/packs-dev
/public/packs/css
/public/packs/js
/public/packs/images

# Ignore coverage results generated by SimpleCov
/coverage/

# Ignore minitest and rspec result files
/test/tmp
/test/version_cache
/spec/tmp
/spec/fixtures/files
/spec/examples.txt

# Ignore rubocop cache
/.rubocop-https*
/.rubocop_cache

# Ignore Capybara screenshots
/tmp/capybara/

# Ignore Redis stores
/tmp/redis/

# Ignore .DS_Store
.DS_Store

# Ignore backups
/backup/

# Ignore ActiveStorage uploaded files in development
/storage/*
!/storage/.keep
Typically, the .gitignore file is filled with additional entries during the implementation of new project features. For example, a new library creates temporary cache files on the local machine - so we add them (or the folder) to the ignore list. Before committing, you will see the extra files and easily identify what needs to be added to .gitignore.

This post doesn't have any additions from the author yet.

Gosu Ruby Tutorial - пройдемось по офіційній документації
03 Jul 11:50

Gosu Ruby Tutorial - пройдемось по офіційній документації

meme code
meme code@memecode
We are writing a demo game Drones vs Zombies (Gosu / Ruby)
12 Jul 12:17

We are writing a demo game Drones vs Zombies (Gosu / Ruby)

meme code
meme code@memecode
How to fix a Windows crash caused by CrowdStrike?
19 Jul 13:53

How to fix a Windows crash caused by CrowdStrike?

meme code
meme code@memecode
What does .map(&:name) mean in Ruby?
28 Jul 11:18

What does .map(&:name) mean in Ruby?

meme code
meme code@memecode
How does the map method work in Ruby? Overview of the method's operation with examples
30 Jul 07:33

How does the map method work in Ruby? Overview of the method's operation with examples

meme code
meme code@memecode
What does a dot at the beginning of a file (.gitignore, .DS_Store, .bashrc, etc.) mean?
02 Aug 13:15

What does a dot at the beginning of a file (.gitignore, .DS_Store, .bashrc, etc.) mean?

meme code
meme code@memecode
How to remove the .DS_Store file from a Git repository?
02 Aug 19:34

How to remove the .DS_Store file from a Git repository?

meme code
meme code@memecode
What is an idempotent method?
21 Aug 20:57

What is an idempotent method?

meme code
meme code@memecode
What is a repository?
21 Aug 21:25

What is a repository?

meme code
meme code@memecode
What is a commit in the context of programming and SCM / Git?
21 Aug 21:37

What is a commit in the context of programming and SCM / Git?

meme code
meme code@memecode
What is SCM (Source Control Management)?
21 Aug 21:46

What is SCM (Source Control Management)?

meme code
meme code@memecode
What hierarchy does the DOM (Document Object Model) have?
23 Aug 09:22

What hierarchy does the DOM (Document Object Model) have?

meme code
meme code@memecode