2025.10.16(更新日: 2025.10.16)
【Git】git stash
はじめに
git stashは、作業途中の変更を一時的に保存して、ワークツリーを綺麗な状態に戻すためのコマンド。
git stashの使用例

hiroki@shibatahiroshitakanoMacBook-Air ki-hi-ro.com % git stash
Saved working directory and index state WIP on main: d9f2e53 タグ名
git stashの使用前後
使用前は、git pullができなかった。

hiroki@shibatahiroshitakanoMacBook-Air ki-hi-ro.com % git pull origin main
error: cannot pull with rebase: You have unstaged changes.
error: please commit or stash them.
使用後は、git pullができた。CONFLICTが起こっているが、この状態に持ってくることができた。

git stashを元に戻す
git stash popを行うと、git stashで退避した変更が適用された後に削除される。
変更を適応するときにコンフリクトが起こることがある。

hiroki@shibatahiroshitakanoMacBook-Air ki-hi-ro.com % git stash pop
error: Your local changes to the following files would be overwritten by merge:
template-parts/_tag-names.php
Please commit your changes or stash them before you merge.
Aborting
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: template-parts/_tag-names.php
no changes added to commit (use "git add" and/or "git commit -a")
The stash entry is kept in case you need it again.
このケースでは、単純に変更をステージ→コミットすればいい。
git statusの結果はこちら。

hiroki@shibatahiroshitakanoMacBook-Air ki-hi-ro.com % git status
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: template-parts/_tag-names.php
no changes added to commit (use "git add" and/or "git commit -a")
ステージとコミット

hiroki@shibatahiroshitakanoMacBook-Air ki-hi-ro.com % git add template-parts/_tag-names.php
hiroki@shibatahiroshitakanoMacBook-Air ki-hi-ro.com % git commit -m "スペースによる違い"
[main 81b3996] スペースによる違い
Committer: 柴田浩貴 <hiroki@shibatahiroshitakanoMacBook-Air.local>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:
git config --global --edit
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
1 file changed, 1 insertion(+), 1 deletion(-)
template-parts/_tag-names.phpのコンフリクトを解消する。入力側の変更を取り込んだ。

そして、git addとgit commit。

無事にgit pushすることができた。

コメントを残す