Gitのskip-worktreeで、手元だけの変更を無視する方法
git update-index --skip-worktree ファイル名 を実行すると、Git で管理しているファイルを手元で書き換えても、git status や git diff に出なくなり、git add . や git commit -a でもコミットに入らなくなります。リポジトリに入っている設定ファイルを、自分の開発環境に合わせて書き換えたまま、ほかの変更だけをコミットしたいときに使います。ただし、共有側でそのファイルが変わると git pull が止まるので、その場合の手順も合わせて確かめました。
検証環境は Ubuntu 24.04、Git 2.43.0 です。作者とコミッターには例示用の値(Neko <neko@example.com>)を設定しました。共有のリポジトリは同じフォルダーに置いた bare リポジトリで、リモートの URL は相対パスの ../origin.git にしています。Windows・macOS と、GitHub などのホスティングサービスを相手にした操作は試していません。
書き換えた設定ファイルが、毎回 git status に出る
次の config.json をコミットして、共有のリポジトリに push しておきました。
$ cat config.json
{
"apiUrl": "https://api.example.com",
"timeout": 3000,
"retry": 2,
"debug": false
}
手元だけ debug を true に書き換えると、当然ながら変更として出ます。
$ sed -i "s/\"debug\": false/\"debug\": true/" config.json && git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: config.json
no changes added to commit (use "git add" and/or "git commit -a")
.gitignore に config.json を書いても、すでに Git で管理しているファイルには効きません。git status --short で見ると、config.json の変更( M)は消えず、.gitignore が新しいファイル(??)として増えただけでした。
$ printf "config.json\n" > .gitignore && git status --short
M config.json
?? .gitignore
確かめたあと、.gitignore は消しました。
$ rm .gitignore && git status --short
M config.json
skip-worktree を付けると、変更が出なくなる
git update-index --skip-worktree でファイルを指定します。何も表示されずに終わり、そのあとの git status では何も変わっていないことになりました。git diff も何も表示しません。
$ git update-index --skip-worktree config.json
$ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
ファイルの中身は書き換えたままです。
$ cat config.json
{
"apiUrl": "https://api.example.com",
"timeout": 3000,
"retry": 2,
"debug": true
}
このコマンドは、ファイルの中身ではなく、インデックス(ステージの情報を持つファイル)の中の、そのファイルの項目に「skip-worktree」という設定を付けます(git update-index の公式リファレンスの --[no-]skip-worktree の項)。
どのファイルに付いているかを確かめる
git ls-files -v を実行すると、skip-worktree が付いたファイルの頭に S が付きます。それ以外の、ふつうに管理しているファイルは H です(git ls-files の公式リファレンスの -t と -v の項)。付けたファイルの一覧だけがほしいときは、grep で S の行だけを取り出します。
$ git ls-files -v
H app.js
S config.json
$ git ls-files -v | grep "^S"
S config.json
付けたことを忘れると、ファイルを直したのにコミットできない理由が分からなくなります。git status には何も出ないので、この一覧で確かめます。
add や commit -a でもコミットに入らない
git commit -a は、変更がないとして何もせずに終わりました(終了コードは 1)。
$ git commit -a -m "全部コミット"; echo "exit=$?"
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
exit=1
ファイル名を指定して git add すると、次のメッセージが出て、終了コード 1 で止まりました。メッセージは「sparse-checkout の範囲の外にあるので、インデックスを更新しない」という意味です。skip-worktree は、もともとスパースチェックアウト(リポジトリの一部のファイルだけを作業フォルダーに出す使い方)のための仕組みで、いまは git sparse-checkout コマンドで設定します。そのため、git sparse-checkout を使っていなくても、このメッセージが出ます。
$ git add config.json; echo "exit=$?"
The following paths and/or pathspecs matched paths that exist
outside of your sparse-checkout definition, so will not be
updated in the index:
config.json
hint: If you intend to update such entries, try one of the following:
hint: * Use the --sparse option.
hint: * Disable or modify the sparsity rules.
hint: Disable this message with "git config advice.updateSparsePath false"
exit=1
git add . は何も言わずに終わり、config.json はステージされませんでした。この状態で app.js を直して git commit -am すると、コミットに入ったのは app.js だけでした。
$ git add .; echo "exit=$?"; git status --short
exit=0
$ printf "console.log(\"nyan nyan\");\n" > app.js && git commit -q -am "app.js を直す" && git show --stat --format="%h %s" HEAD
999d945 app.js を直す
app.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
stash や reset –hard でも、手元の変更は残る
git stash は、しまう変更がないと判断しました。git reset --hard のあとも、config.json の debug は true のままでした。
$ git stash; echo "exit=$?"
No local changes to save
exit=0
$ git reset --hard && git diff HEAD --stat && grep debug config.json
HEAD is now at 999d945 app.js を直す
"debug": true
git stash や git reset --hard で作業フォルダーを最新のコミットの状態に戻したつもりでも、skip-worktree を付けたファイルは書き換えたままです。書き換えたままの config.json で動いていないか、先ほどの git ls-files -v で確かめてください。
設定はクローンごとで、ほかの人には伝わらない
skip-worktree はインデックスに付ける設定なので、コミットや push では共有されません。別の場所にクローンし直すと、config.json は H で、中身もコミットされたままの false でした。
$ git clone -q origin.git other 2>/dev/null; cd other && git ls-files -v && grep debug config.json
H app.js
H config.json
"debug": false
チームの全員に同じファイルを無視させたい場合には、この方法は使えません。新しくクローンしたときも、付け直しが必要です。
共有側で同じファイルが変わると、pull が止まる
ほかの人の役のクローン(other)で README.md だけを足して push し、手元で git pull すると、問題なく取り込めました。config.json は書き換えたままで、S も付いたままです。
$ git pull -q; echo "exit=$?"; git log --oneline -1 && grep debug config.json && git ls-files -v
exit=0
2e8b575 README を足す
"debug": true
H README.md
H app.js
S config.json
次に、other で config.json の apiUrl を書き換えて push しました。手元で git pull すると、「手元の変更がマージで上書きされる」というエラーで止まります。
$ git pull; echo "exit=$?"
From ../origin
2e8b575..e70ef1e main -> origin/main
error: Your local changes to the following files would be overwritten by merge:
config.json
Please commit your changes or stash them before you merge.
Aborting
Updating 2e8b575..e70ef1e
exit=1
止まったので、手元のコミットは前のまま(2e8b575)で、config.json も書き換えたままでした。エラーメッセージは stash を勧めていますが、skip-worktree を付けたままでは、先ほどのように git stash がこの変更をしまいません。
いったん外してから stash し、pull のあとで付け直す
--no-skip-worktree で外すと、変更が git status に戻ってきます。その状態で stash し、pull してから stash pop で変更を戻します。
$ git update-index --no-skip-worktree config.json && git status --short
M config.json
$ git stash
Saved working directory and index state WIP on main: 2e8b575 README を足す
$ git pull -q; echo "exit=$?"; git log --oneline -1
exit=0
e70ef1e API の URL を変える
$ git stash pop -q; echo "exit=$?"; cat config.json
Auto-merging config.json
exit=0
{
"apiUrl": "https://api2.example.com",
"timeout": 3000,
"retry": 2,
"debug": true
}
共有側の apiUrl の変更と、手元の debug の変更が両方入りました。この検証では、stash pop のときに自動でマージされました。最後に、もう一度 skip-worktree を付けます。
$ git update-index --skip-worktree config.json && git status --short && git ls-files -v | grep "^S"
S config.json
ブランチの切り替えも、ファイルの中身が違うと止まる
config.json の中身が変わらないブランチとの間なら、git switch で行き来しても、手元の変更はそのまま残りました。
$ git switch -c feature/menu && printf "menu\n" > menu.txt && git add menu.txt && git commit -q -m "menu を足す" && git switch main && grep debug config.json
Switched to a new branch 'feature/menu'
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
"debug": true
other で timeout を書き換えたブランチ feature/timeout を push し、手元でそのブランチに切り替えようとすると、pull のときと同じ種類のエラーで止まり、main から動きませんでした。
$ git fetch -q && git switch feature/timeout; echo "exit=$?"; git branch --show-current
error: Your local changes to the following files would be overwritten by checkout:
config.json
Please commit your changes or stash them before you switch branches.
Aborting
exit=1
main
外して、ふつうの状態に戻す
付けたときと同じファイル名で --no-skip-worktree を指定します。S が H に戻り、手元の変更がまた git status と git diff に出るようになりました。
$ git update-index --no-skip-worktree config.json && git status --short && git ls-files -v
M config.json
H README.md
H app.js
H config.json
手元の変更をやめて、コミットされた中身に戻したいときは、外したあとで git restore を実行します。debug が false に戻り、git status --short にも何も出なくなりました。
$ git restore config.json && git status --short; echo "exit=$?"; grep debug config.json
exit=0
"debug": false
assume-unchanged との違い
似たオプションに --assume-unchanged があります。sed で手元の debug を true にしてから付けると、git status に出なくなり、git ls-files -v では小文字の h で表示されました。git stash も、しまうものがないと判断しました。
$ git update-index --assume-unchanged config.json && git status --short && git ls-files -v
H README.md
H app.js
h config.json
$ git stash; echo "exit=$?"
No local changes to save
exit=0
違いが出たのは git reset --hard です。skip-worktree のときは手元の変更が残りましたが、assume-unchanged では debug が false に戻り、書き換えた内容が消えました。
$ git reset --hard -q && git ls-files -v && grep debug config.json
H README.md
H app.js
h config.json
"debug": false
git update-index の公式リファレンスによると、assume-unchanged は「このファイルは変えない」と Git に約束して、ファイルの変更を確かめる処理を省かせるものです。ファイルの状態を調べる処理(lstat(2))が遅いファイルシステムで、動作を速くするために使います。この検証では、git reset --hard のあとも手元の変更が残ったのは skip-worktree のほうでした。
公式ドキュメントは、別のファイルに分ける方法を勧めている
git update-index の公式リファレンスの NOTES の節と、Git の FAQ の「How do I ignore changes to a tracked file?」には、assume-unchanged と skip-worktree は、管理しているファイルの変更を無視する目的には正しく動かない、と書かれています。Git は操作によっては作業フォルダーのファイルとインデックスを比べるためです。FAQ には、Git にはこのための機能がないことと、その理由も書かれています。ファイルを上書きする必要があるとき、Git には手元の変更を残すべきか捨ててよいか分からないので、安全側に倒して必ず残す、というものです。
代わりに勧められているのは、ひな形のファイル(例:config.example.json)をリポジトリに入れておき、各自がそれをコピーした config.json を書き換える方法です。コピーしたほうは .gitignore に書いて、間違ってコミットしないようにします。FAQ のすぐ後の項目にあるとおり、すでに管理しているファイルを管理から外すには git rm --cached を使います。
skip-worktree は、ひな形に分ける変更をリポジトリに入れられないときに、自分の手元だけで使う方法です。使うなら、いったん外して stash してから pull し直す手順と、git ls-files -v での確かめ方を覚えておけば、pull や switch で止まっても慌てずに済みます。





