1
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で作業を避難させる場合の対応法

1
Last updated at Posted at 2025-11-19

基本的なことですが、忘備もかねて書いていきます。

前提

  • コードを色々といじったが、うまくいかないので一旦退避させたい。

変更をまだコミットしていないとき

  • git stashでまだコミットしていない変更を一時的に避難
  • git stashはgit stash push の省略形
  • stash は作業内容の一時避難所であり、ブランチに紐づかない。
  • もしstashを復活させたい場合は、どのブランチに統合(適用)するかは自由。どのブランチに統合するかは、そのブランチに入って stash を適用するだけ
  • その際は、git stash pop(適用と削除を同時に行う)もしくはgit stash apply(適用のみ)を使う。
  • stashが複数ある場合、スタッシュ名を含む一覧はgit stash listで確認できる。git stash popやgit stash applyはスタッシュ名を指定することも可能。
git stash push -m "コード退避"  # 変更を退避

git stash pop   # 変更を元に戻す(適用と削除を同時に行う)
git stash apply  # 変更を元に戻す(適用のみ)

git stash list  # 一覧表示
実行結果の例
stash@{0}: WIP on main: ...
stash@{1}: WIP on feature: ...

git stash apply stash@{1} # スタッシュ名を指定して適用

変更をコミットしてしまったとき

  • すでにコミットしてしまったが、その変更をこれからの作業に含めたくない場合は、退避用ブランチを作成した上で、元のブランチを1つ前のコミットに戻す。
  • ただし、公開済みブランチではreset --hardは履歴を書き換えるため注意
git branch backup-branch   # 退避用ブランチ作成(退避用ブランチにはコミットが残る)
git reset --hard HEAD~1    # 元のブランチを1つ前のコミットに戻す

備考

  • 変更内容を破棄してもいい場合には、git restore .を使うと変更内容が破棄され直近のコミットの状態に戻る。
git restore . # 変更内容が破棄され直近のコミットの状態に戻る
1
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
1
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?