1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

commitせずにブランチを移動する方法。

Posted at

作業中に別ブランチに移動してやりたいことがあるという時に作業をスムーズにする方法です。
commitしていない変更がある状態でブランチを移動しようとすると以下のようなエラーが出ます。

error: Your local changes to the following files would be overwritten by checkout:
        src/app/Http/Controllers/BicycleController.php
Please commit your changes or stash them before you switch branches.
Aborting

ブランチを切り替える前にcommitかstashをしてくださいと言われるのですが(Google翻訳)、まだ作業途中なのでcommitしたくないという時があると思います。
その場合、$ git stachで修正を一旦退避させることができます。

変更をstashした後、$ git stash list で退避した作業を確認します。

$ git stash list
stash@{0}: WIP on issue-6: 1a3ae7f bicyclesページの作成

別ブランチでの作業が終わった後、元いたブランチに戻り作業を$ git stash applyで復元させます。

$ git stash apply stash@{0}

stash@{0}の作業を戻したい場合はこのように指定すると、退避させた修正が元通りになります。

作業時に参考にさせていただいた記事:
【git stash】コミットはせずに変更を退避したいとき
コミットせずに他のブランチで作業したい

ちなみに

PowerShellで$ git stash apply stash@を実行しようとした時にエラーになってしまったのですが、
以下のようにシングルクォートで囲むと実行できました。

git stash apply 'stash@{0}'

PowerShellの場合中括弧内の文字列がコマンドとして実行されるらしいです。

こちらの記事より:
https://crieit.net/posts/PowerShell-Git-reset-stash

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?