📝

簡単に .gitignore を作成する方法のメモ

2024/05/04に公開

こちらの記事がすべて

この記事は自分の覚書のために作成していますが、内容については、こちらの記事がすべてです。ありがとうございます。
https://blog1.mammb.com/entry/2020/01/25/090000

作成方法

例えば、以下の3つのテンプレート

をまとめた .gitignore ファイルを作る場合、以下のコマンドになる。

curl -sS https://raw.githubusercontent.com/github/gitignore/main/{\
Global/macOS.gitignore,\
Python.gitignore,\
Global/MicrosoftOffice.gitignore,\
} >> .gitignore

これで出来上がった .gitignore ファイルは長いので記事の末尾に配置します。
Mac な .DS_Store から、Python については自分がまだ知りもしない色々なもの、Office の一時ファイルがカバーされています。

勉強

上のコマンドについてこの機会に勉強。

curl

他の機能などを無視して極めて雑にいうと、URL で指定したファイルを受信するコマンド。
デフォルトでは、取得した情報を標準出力(画面)に出すので、一番下の >> .gitignore でファイルに書き出している。

-sS オプション

-s オプションで進捗を表示しないようにしつつ、ただしその場合はエラーを表示するように -S オプション。

コマンドラインの中でのバックスラッシュ

コマンドラインの中でのバックスラッシュ \ は、改行です。
なので、別になくても良いのですが、次のブレース展開で指定している3つの URL が視覚的に分かりやすくなっています。

ブレース展開

{波括弧} はコマンドライン上で複数の文字列を展開するためのブレース展開という書き方。
...master/ までの部分が共通の3つのURLの異なる部分だけを差し替えて3回 URL 全体を指定しているのと同じことになる書き方。
ホゲホゲ戦隊という3人組の名乗りを描くとしたら、こうなる

% echo hogehoge-{red,blue,green}!
hogehoge-red! hogehoge-blue! hogehoge-green!

裏話

最近 Python を勉強し始めて、はじめて「__pycache__とういファイルができる」とか pytest を使うと「.pytest_cache が作られる」ということを知って、あとから .gitignore に追加することがあった。
加えて、もともと Mac をつかっているから .DS_Store とかもあるし、今回は Microsoft Office (正確には Excel) のファイルもあって、これも一時ファイルを作ったりする。
ということで、.gitignore のテンプレートから「Mac のと、Python のと、Office のをコピーして・・・」とやれば良いのだけれども、何か楽なり確実な方法はあるだろうか? と思った。

そこで見つけたのが冒頭で書いた記事と、
https://blog1.mammb.com/entry/2020/01/25/090000
こちらの gibo というツールだった。
https://github.com/simonwhitaker/gibo

今回は勉強も兼ねて curl を使う前者の方法を試してみたが、gibo も便利そうです。

記事内の方法で生成せされた .gitignore ファイル

# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
#  Usually these files are written by a python script from a template
#  before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
#   For a library or package, you might want to ignore these files since the code is
#   intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
#   According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
#   However, in case of collaboration, if having platform-specific dependencies or dependencies
#   having no cross-platform support, pipenv may install dependencies that don't work, or not
#   install all needed dependencies.
#Pipfile.lock

# poetry
#   Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
#   This is especially recommended for binary packages to ensure reproducibility, and is more
#   commonly ignored for libraries.
#   https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
#   Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
#   pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
#   in version control.
#   https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
#  JetBrains specific template is maintained in a separate JetBrains.gitignore that can
#  be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
#  and can be added to the global gitignore or merged into this file.  For a more nuclear
#  option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
*.tmp

# Word temporary
~$*.doc*

# Word Auto Backup File
Backup of *.doc*

# Excel temporary
~$*.xls*

# Excel Backup File
*.xlk

# PowerPoint temporary
~$*.ppt*

# Visio autosave temporary files
*.~vsd*
400: Invalid request

Discussion