mainブランチで作業しちゃった
最終更新日
公開日
そんな時は...
目次
状況と対処法
git add
する前に気付いた
- 一時退避
- 正しいブランチで復元
# on main branch
git stash save # 一時退避
git stash list
git checkout -b <my-branch>
git stash apply # 復元
git add
までしちゃった
add
の取り消し- 一時退避
- 正しいブランチで復元
# on main branch
git reset HEAD # addの取り消し
git stash save # 一時退避
git stash list
git checkout -b <my-branch>
git stash apply # 復元
git commit
までしちゃった
- 正しいブランチにmerge/正しいブランチを作成
commit
の取り消し
# on main branch
# if <my-branch> is already created
git checkout <my-branch>
git merge main
# elif <my-branch> is not created yet
git checkout -b <my-branch>
# endif
git checkout main
git reset --hard HEAD # commitの取り消し
git push
までしちゃった
git push
までしちゃったそもそもできないように設定しておく(Branch Protection Rule)