0
1

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初心者向け:「git stash」コマンドガイド

Posted at

Gitを使って開発をしていると、「作業中だけど、他のブランチに移動したい!」という場面に出くわすことがあります。
そんなときに便利なのが、git stash(ギット スタッシュ)コマンドです。

本記事では、Git初心者の方向けにgit stashの使い方と注意点を丁寧に解説します。


◆ git stashとは?

git stashとは、作業中の変更内容を一時的に保存(退避)し、作業ディレクトリをきれいな状態に戻すためのコマンドです。

ブランチの切り替えや他の作業を行う前に、今の変更を一旦しまっておくことができます。


◆ 基本的な使い方

1. 作業内容を退避する

git stash

このコマンドで、**ステージされていない作業内容(未コミットの変更)**が一時的に保存され、作業ディレクトリはクリーンな状態に戻ります。


2. 退避した作業内容を戻す(復元する)

git stash pop

popは、一番上のstashを適用し、同時に削除します。

※ 復元時に競合が発生する場合がありますので、慎重に行ってください。


3. stashの一覧を確認する

git stash list

stashに保存されている履歴を確認できます。

出力例:

stash@{0}: WIP on develop: a1b2c3d 修正中
stash@{1}: WIP on feature/login: 初期実装

4. 特定のstashを戻す

git stash apply stash@{1}

popとの違いは、「applyはstashを削除しない」という点です。


5. stashを削除する

  • 一つだけ削除:
git stash drop stash@{1}
  • すべて削除:
git stash clear

◆ よくある利用シーン

  • 作業途中で、他のブランチのバグ修正対応を頼まれた
  • コミットしたくないけど、一旦作業を中断したい
  • 別ブランチに切り替えたいけど、変更があると切り替えられない時
  • 最新をプルしたいけど、自分のローカルでの作業と同じファイルがあるからプルできない時

◆ 注意点

  • git stashで退避されるのは追跡されているファイルのみです。

    → 新規作成ファイル(git addしていないもの)は対象外です。

  • popapplyを行うときに、**コンフリクト(競合)**が起きることがあります。

    → 必ず、戻す前に現在の状態を確認しておきましょう。


◆ まとめ

コマンド 意味
git stash 変更内容を一時退避
git stash list stashの一覧を見る
git stash pop 退避内容を戻して削除
git stash apply 退避内容を戻す(削除しない)
git stash drop 指定したstashを削除
git stash clear stashをすべて削除

git stashは、作業を中断・切り替えたいときの強力な味方です。

安全に使いこなして、ストレスのないブランチ操作を実現しましょう。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?