LoginSignup
0
0

More than 5 years have passed since last update.

gitリポジトリのrootディレクトリから相対パスでファイルにアクセスできるようにする

Posted at

何ができるようになるか

 % git init
Initialized empty Git repository
 % mkdir -p dir_1/dir_2
 % echo 'abc' > file_1
 % tree
.
├── dir_1
│   └── dir_2
└── file_1

2 directories, 1 file
 % cat file_1
abc
 % cd dir_1
 % cat $GIT_ROOT/file_1
abc
 % cd dir_2
 % cat $GIT_ROOT/file_1
abc
 % cd $GIT_ROOT

このようにgitのサブディレクトリにいても環境変数GIT_ROOTを通してgitのrootディレクトリからの相対パスでファイルを指定・アクセスできるようになる。

やり方

.zshrcに以下を書く

function precmd_set_git_root_variable() {
  GIT_ROOT=$(git rev-parse --show-toplevel 2> /dev/null)
}
autoload -Uz add-zsh-hook
add-zsh-hook precmd precmd_set_git_root_variable

参考記事

0
0
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
0
0