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

Що таке .gitignore? Для чого потрібен та як використовувати

Post cover: Що таке .gitignore? Для чого потрібен та як використовувати
This content has not been translated yet.We're showing the original Ukrainian content below.
.gitignore - це файл, який використовується в системі контролю версій Git для вказівки файлів та директорій, які повинні бути ігноровані Git. Це означає, що файли та директорії, зазначені у .gitignore, не будуть додані до індексу Git, не будуть відслідковуватися і не будуть потрапляти у коміти.

Навіщо потрібен .gitignore?

Файл .gitignore допомагає уникнути включення до репозиторія файлів, які не мають відношення до коду або не повинні бути загальнодоступними. Це можуть бути:
  • Файли конфігурації середовища розробки.
  • Лог-файли.
  • Згенеровані файли (наприклад, результати компіляції).
  • Файли тимчасового характеру (кеш, тимчасові файли).
  • Чутливі дані (наприклад, паролі або ключі доступу).

Приклад використання .gitignore

Створення файлу .gitignore:
У кореневій директорії вашого репозиторія створіть файл з назвою .gitignore.
Додавання файлів і директорій до .gitignore:
У файл .gitignore додавайте шаблони для файлів та директорій, які повинні бути ігноровані. Наприклад:
# Ігнорувати всі файли .DS_Store
.DS_Store

# Ігнорувати файли журналів
*.log

# Ігнорувати всі файли в директорії temp/
temp/

# Ігнорувати файли конфігурації IDE
.idea/
*.iml
Застосування .gitignore:
Після додавання записів до .gitignore, переконайтеся, що ці файли ще не були додані до індексу. Якщо вони вже були додані, видаліть їх з індексу за допомогою команди git rm --cached <filename> і зробіть коміт.

Як працює .gitignore?

  • Кожен рядок у .gitignore представляє шаблон для файлів, які повинні бути ігноровані.
  • Шаблони можуть бути простими назвами файлів або містити символи підстановки (wildcards) для більшої гнучкості.
  • Коментарі можуть бути додані за допомогою символу #.
  • .gitignore може бути розташований у будь-якій директорії репозиторія, і його дія буде обмежена цією директорією та піддиректоріями. Це доречі, іноді прям дуже зручно.
Доречі, файл .gitignore ви не побачите у Finder (тільки в редакторі коду або в терміналі виконавши ls -a). Прочитайте у попередньому дописі детальніше про приховані файли (з крапкою на початку).
Приклад файлу .gitignore для Ruby on Rails проєкту
# 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
Зазвичай файл .gitignore наповнюється додатковими записами під час імплементації нового функціоналу проєкту. Наприклад, нова бібліотека створює тимчасові файли кешу на локальній машині - тож ми додаємо їх (або теку) до списку ігнорування. Перед комітом, ви побачите зайві файли і з легкістю ідентифікуєте, що саме треба додати до .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
Пишемо демо-гру Drones vs Zombies (Gosu / Ruby)
12 Jul 12:17

Пишемо демо-гру 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
Що означає .map(&:name) в Ruby?
28 Jul 11:18

Що означає .map(&:name) в Ruby?

meme code
meme code@memecode
Як працює метод map в Ruby? Огляд роботи методу з прикладами
30 Jul 07:33

Як працює метод map в Ruby? Огляд роботи методу з прикладами

meme code
meme code@memecode
Що означає крапка на початку файлу(.gitignore, .DS_Store, .bashrc тощо)?
02 Aug 13:15

Що означає крапка на початку файлу(.gitignore, .DS_Store, .bashrc тощо)?

meme code
meme code@memecode
Як видалити файл .DS_Store з Git репозиторію?
02 Aug 19:34

Як видалити файл .DS_Store з Git репозиторію?

meme code
meme code@memecode
Що таке ідемпотентний метод?
21 Aug 20:57

Що таке ідемпотентний метод?

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