コンテンツへスキップ
LatchkeyLatchkey home

`git stash` とは?

git stash はコミットしていない変更を一時的に脇へ置き、クリーンな working directory でコンテキストを切り替えられるようにし、後で復元できるようにします。

git stash は現在の作業を棚上げする手軽な方法です。緊急の用事が発生してまだコミットの準備ができていないとき、stash はコミットしていない変更をしまい込み、クリーンな状態を用意し、戻ってきたときに作業を復元できるようにします。

stash がすること

stash は staged と unstaged の両方のコミットしていない変更を記録し、working directory を直近のコミットに戻します。変更は後で再適用できる stash スタックに保存されます。これはコミットではなく軽量な一時停止ボタンであり、完全にあなたのマシン上に留まります。

退避と復元

stash してから緊急の作業を行い、その後 stash を pop して変更を元に戻します。

Shelving and restoring work
git stash push -m "WIP form layout"
# handle the interruption
git stash pop

stash が役立つとき

stash は、緊急の修正のためにブランチを切り替える必要があるとき、クリーンなツリーに更新を取り込むとき、クリーンなビルドをテストするために変更を一時的にどけるときに便利です。複数の stash を保持して選択的に適用できますが、深い stash スタックは混乱しがちです。

stash と CI/CD

stash は純粋にローカルであり、決して push されないため、CI がそれらを見ることはありません。これは意図的な設計です。パイプラインはコミットされた履歴をビルドするのであって、誰かが棚上げした作業途中のものはビルドしません。CI で何かをテストしたい場合は、コミットして push する必要があります。stash された変更はリモートには存在しません。

stash を賢く使う

  • 中途半端な作業をコミットせずにコンテキストを切り替えるために stash を使いましょう。
  • 各 stash が何を保持しているか覚えられるようにメッセージを付けましょう。
  • stash が溜まって古くなるのを避けましょう。
  • stash はローカルであることを忘れないでください。変更を CI に届けるにはコミットしましょう。

Applying this to your pipeline

  • Measure before changing. Most CI optimisation targets the wrong step because the slow one is assumed rather than timed.
  • Cache what is expensive to produce and cheap to validate, and key the cache to the exact tool version.
  • Fail fast: run the cheapest checks that can reject a change first, so an expensive job never starts on code that cannot pass.
  • Prefer determinism over speed when they conflict. A fast pipeline nobody trusts gets re-run, which is slower than a slow one that is believed.

重要なポイント

  • git stash はコミットしていない変更を退避し、クリーンな working directory を用意します。
  • 後で stash スタックから pop または apply で復元します。
  • stash はローカル限定です。CI はコミットされた履歴をビルドし、stash はビルドしません。

よくある質問

What is What is git stash??
git stash is the quick way to put your current work on a shelf. When something urgent comes up and you are not ready to commit, stashing tucks your uncommitted changes away, gives you a clean slate, and lets you bring the work back when you return.
What stashing does?
Stashing records your uncommitted modifications, both staged and unstaged, and reverts your working directory to the last commit. The changes are saved on a stash stack you can reapply later. It is a lightweight pause button, not a commit, and stays entirely on your machine.
Stashing and restoring?
You stash, do the urgent work, then pop the stash to bring your changes back.
When stashing helps?
Stashing is handy when you need to switch branches to fix something urgent, pull in updates onto a clean tree, or temporarily clear changes to test a clean build. You can keep multiple stashes and apply them selectively, though deep stash stacks get confusing.

関連ガイド