1
2

git stashでコミットせずに変更を一時保存しよう

Last updated at Posted at 2024-07-21

git stashは、作業中の変更を一時的に保存し、後で再適用するためのGitコマンドである。以下に主な使用方法と機能を示す。

変更を一時保存(stash)する:

git stash

または、メッセージを付けて保存:

git stash save "作業中の変更"

保存した変更(stash)の一覧を表示:

git stash list

最新のstashを適用する:

stashを適用とは作業ディレクトリにstash listに一時保存した状態を適用するということ.これをすると,最新のstashはstashリストから削除される

git stash pop

適用した後にコミットすれば作業ツリーに履歴が残る

git commit -m "コミットメッセージ"

最新のstashを適用するが、stashリストからは削除しない

git stash apply

特定のstashを適用

git stash apply stash@{n}

ここで、nはgit stash listで表示されるインデックス番号

最新のstashの内容を確認:

git stash show

より詳細な差分を見るには:

git stash show -p

特定のstashを削除:

git stash drop stash@{n}

全てのstashを削除:

git stash clear

新しいブランチを作成し、そこにstashを適用:

git stash branch <new-branch-name>

git stashは以下のような状況で特に有用である:

  • 作業中に緊急の修正が必要になった場合
  • ブランチを切り替える前に現在の作業を保存したい場合
  • プルする前に競合を避けたい場合

注意点:

  • stashはグローバルであり、全てのブランチで共有される。
  • 複数のstashを管理する場合は、わかりやすい名前を付けることが推奨される。
  • 長期間stashを放置すると、コンテキストを忘れる可能性があるため注意が必要。

git stashを効果的に使用することで、作業の流れを中断することなく、柔軟にタスクを切り替えることができる。

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