ikuma-t.

検索

「$HOME/git/ignore」と 「$GIT_DIR/info/exclude」と「.gitignore」の使い分け

に公開

IMAGE: https://cdn-ak.f.st-hatena.com/images/fotolife/i/ikmbear/20210102/20210102104736.png🔗

f:id:ikmbear:20210102104736p:plain

現在のプロジェクトで「自分だけ」が使用しているファイルをGitの対象外にしたい

最近Railsを6.1にあげたのですが、これが原因でrailwaysというRubyMineでルーティングを表示するRailsが使えなくなりました😅

Rails6.1にあげた事でrake routesが使用できなくなり、RubyMineのrailways$1(rake routesが必要)が動かなくなった。https://t.co/OzkrSsW3dw🔗https://t.co/M5WJnv3FfX🔗

— Ikuma_t (@ikumatdkr) 2021年1月2日🔗

railwaysが対応してくれるまでは、一旦Rakeタスクを自分で追加しています(他力本願…自分で直せるようになりたい😓)。

暫定対応として、routes.rakeファイルを作ってrails routesをラップしてる(毎回やるのは面倒😅)

— Ikuma_t (@ikumatdkr) 2021年1月2日🔗

このファイルの使用状況は以下の通りです。

  • 自分しか使っていない(RubyMineでrailwaysを使う人しか必要ない)
  • railwaysが治れば、このファイル自体が不要。なのでグローバルにはGitの対象外としたくない(あとあとメンテナンスが面倒なので)

この場合どうGitの対象外にすればいいのか迷ったので、調べてみました。

結論

現在のプロジェクトで「自分だけ」が使用しているファイルをGitの対象外にするには、$GIT_DIR/info/excludeに追記する

Gitの対象外とする際の使い分け

Gitの対象外とするには次の3つの方法があります。

  1. .gitignoreに追記:特定の$1を利用するすべての人が対象外とすべきものをここに追記する。
  2. $GIT_DIR/info/excludeに追記:特定の$1で対象外とすべきだけれど、他の人に共有すべきではないものをここに追記する。
  3. $HOME/git/ignoreに追記:自分が利用するすべての$1で対象外とすべきものをここに追記する。

例えば今FJORD BOOT CAMPでRailsアプリケーションの課題を進めていますが、その場合の設定例は次の通りです。

.gitignore

Railsのデフォルトに加え、画像のアップロード先、Railsの設定先を対象外として追記しています。

# See https://help.github.com/articles/ignoring-files for more about ignoring files.## If you find yourself ignoring temporary files generated by your text editor# or operating system, you probably want to add a global ignore instead:# git config --global core.excludesfile '~/.gitignore_global'# Ignore bundler config./.bundle# Ignore the default SQLite database./db/*.sqlite3/db/*.sqlite3-journal/db/*.sqlite3-*# Ignore all logfiles and tempfiles./log/*/tmp/*!/log/.keep!/tmp/.keep# Ignore pidfiles, but keep the directory./tmp/pids/*!/tmp/pids/!/tmp/pids/.keep# Ignore uploaded files in development./storage/*!/storage/.keep/public/assets.byebug_history# Ignore master key for decrypting credentials and more./config/master.key/public/packs/public/packs-test/node_modules/yarn-error.logyarn-debug.log*.yarn-integrity/public/uploads/*.env

$GIT_DIR/info/exclude

railways用に今回の$1に追加したRakeタスクを対象外とします。

# git ls-files --others --exclude-from=.git/info/exclude# Lines that start with '#' are comments.# For a project mostly in C, the following would be a good set of# exclude patterns (uncomment them if you want to use them):# *.[oa]# *~/lib/tasks/routes.rake

$HOME/git/ignore

Mac上でRubyMineを使用して開発をしているので、全Macで対象外とします。

# General.DS_Store.AppleDouble.LSOverride# Icon must end with two \rIcon# 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.AppleDesktopNetwork Trash FolderTemporary Items.apdisk# RubyMine.idea.ediotorconfig

参考:Git - gitignore Documentation🔗

Which file to place a pattern in depends on how the pattern is meant to be used.

Patterns which should be version-controlled and distributed to other repositories via clone (i.e., files that all developers will want to ignore) should go into a .gitignore file.

Patterns which are specific to a particular repository but which do not need to be shared with other related repositories (e.g., auxiliary files that live inside the repository but are specific to one user’s workflow) should go into the $GIT_DIR/info/exclude file.

Patterns which a user wants Git to ignore in all situations (e.g., backup or temporary files generated by the user’s editor of choice) generally go into a file specified by core.excludesFile in the user’s ~/.gitconfig. Its default value is $XDG_CONFIG_HOME/git/ignore. If $XDG_CONFIG_HOME is either not set or empty, $HOME/.config/git/ignore is used instead.

ikuma-t

ikuma-t

ABOUT

9割笑顔、1割 (´・ω・) なエンジニア