0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【初心者向け】git stashの使い方がスッキリわかる!作業中の変更を一時的に保存する方法

Last updated at Posted at 2025-06-06

はじめに

「作業中だけど、ちょっと他のブランチを見たい…でも今の変更をコミットするのはまだ早い」

そんなときに便利なのが git stash です!

この記事では、git初心者の方でもわかるように、git stash の使い方をゆっくり丁寧に紹介します。


git stashって何?

簡単に言うと、「作業内容を一時的に隠しておける機能」です。

今やってる変更を保存して、ワーキングディレクトリをキレイな状態に戻せます。そして、また元に戻すこともできます!


基本の使い方

1. 作業中の変更を一時保存する:

git stash

これで、作業内容はGitの"隠し場所(stash)"に保存され、作業ディレクトリはキレイな状態になります。


2. スタッシュ一覧を確認する:

git stash list
stash@{0}: WIP on main: 修正中のコード
stash@{1}: WIP on feature/foo: バグ対応中

3. 保存した内容を戻す:

直前のスタッシュを戻す:

git stash apply

特定のスタッシュ(例:stash@{1})を戻す:

git stash apply stash@{1}

ポイント:stash@{番号}git stash list の出力で確認できます。

popとの違い

  • apply:戻すだけ(stashはそのまま残る)
  • pop:戻したあとstashを削除(使い切り)

よく使うオプション

未追跡ファイルも一緒に保存する:

git stashは ステージされていない(git addしていない)ファイルの変更 も保存しますが、
まったく新しく作った未追跡ファイル(Untracked)は保存されません。そこで-uを使います。

git stash -u
  • -u(または --include-untracked)をつけると、まだ git add していないファイルも保存できます。

スタッシュにメモをつける:

git stash push -m "API修正中"

後で見返すときに、わかりやすくて便利です!


応用テクニック

特定のスタッシュだけ消したい:

git stash drop stash@{1}

全部のstashを消す:

git stash clear

よくあるトラブルと対処法

エラー例:

error: Your local changes to the following files would be overwritten by merge.

これは、戻そうとしたときに作業内容とぶつかってしまうエラーです。

➡ 対処法:

  • 今の作業をコミットする
  • もしくは、さらにstashしてから戻すのが安全!

まとめ:覚えておきたいコマンド一覧

コマンド やること
git stash 作業内容を一時保存する
git stash list 保存されたstashの一覧を見る
git stash apply 内容を元に戻す(stashは残る)
git stash apply stash@{1} 特定のスタッシュを元に戻す
git stash pop 元に戻してstashを削除
git stash -u 未追跡ファイルも一緒に保存する
git stash clear すべてのstashを削除する

この記事が良かったら、ストックやいいねをしてもらえると嬉しいです!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?