Skip to content

Instantly share code, notes, and snippets.

@daichikuwa0618
Last active September 17, 2020 08:21
Show Gist options
  • Save daichikuwa0618/200298364995e6d519374c3c67b6a113 to your computer and use it in GitHub Desktop.
Save daichikuwa0618/200298364995e6d519374c3c67b6a113 to your computer and use it in GitHub Desktop.
git worktree を使って、ディレクトリの diff を出さないする tips

概要

git worktree を使って、例えば develop というブランチを操作する時、

$ git worktree add develop

とすると思う。 こうすると、 git 管理下に develop というディレクトリが作られて、diff が出てしまう。

その対象法を紹介する。

対象法

個人開発であれば .gitignore に書くのも手なのだが、

  1. 複数人で操作するリポジトリには使用できない
  2. リポジトリ毎に .gitignore に追加する必要がある

というので面倒 or 不可能である。

そこで $HOME/.config/git/ignore に書いて、ローカルのグローバル ignore として扱えば良い。 ($HOME/.gitignore_global でも動作としては問題ない)

例えば、 projectDir/git-worktrees/branchA というように、 git-worktrees/ 配下に配置している場合、次ファイルのように $HOME/.config/git/ignore ファイルを作成する。、

git-worktrees/*

これにより、 git-worktrees/ 配下のディレクトリ・ファイルは無視されるので、この中に git worktree add していけば良い。

さらに、以下のような関数を $HOME/.zshrc 等に作成することで、gwt コマンドにより自動的に git-worktrees/ 配下に作られる。

function gwt() {
GIT_CDUP_DIR=`git rev-parse --show-cdup`
git worktree add ${GIT_CDUP_DIR}git-worktrees/$1 -b $1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment