LoginSignup
3
1

More than 1 year has passed since last update.

Git管理下のプロジェクトで自分用のスクリプトを使いたい

Last updated at Posted at 2018-04-25

やりたいこと

  • プロジェクトディレクトリ下でのみ有効なコマンドを作りたい
  • 自分が使うためのものなので、Git管理下には置きたくない

手順

方針

  • プロジェクトディレクトリに自分専用のフォルダ tsuemura を作る
  • tsuemura/binにスクリプトを保存する
  • Gitがtsuemuraを常に無視するように設定する
  • プロジェクトフォルダに入った時だけ tsuemura/bin にパスを通すようにする

Gitの設定

MacなどUnix系OSの場合は$HOME/.config/git/ignoreに下記設定を追加する。

$HOME/.config/git/ignore
tsuemura
.envrc

Windowsの場合は事前にignoreファイルのパスをgitに設定しておかないといけない。PowerShellで以下を実行する。

git config --global core.excludesfile "$Env:USERPROFILE\.config\git\ignore"

tsuemura は自分用のスクリプトを入れておく場所。
ついでに.DS_Storeとか.vscodeとかも書いておくと吉。

scriptbinでなく自分の名前にしてあるのは、プロジェクト内にもともと存在するファイルとの干渉を防ぐ為。

direnvの設定

Homebrewを利用している場合は↓でOK。

$ brew install direnv

シェルの設定(bashの場合)

$ echo 'eval "$(direnv hook bash)"' >> .bashrc
$ source ~/.bashrc

プロジェクトディレクトリに移動して、.envrcというファイルにPATHの設定を書く。

export PATH=./tsuemura/bin:$PATH

direnvの設定を反映する。

$ direnv allow

確認

tsuemura/binに保存したコマンドが、プロジェクトディレクトリ内では動く。

hello-world
#!/usr/bin/bash
echo "Hello World!
$ cd /path/to/your/project
$ hello-world

Hello World!
$ cd
direnv: unloading

$ hello-world

bash: command not found: hello-world
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1