mainブランチで作業しちゃった

最終更新日
公開日

そんな時は...

目次

状況と対処法

git addする前に気付いた

# on main branch
git stash save # 一時退避
git stash list
git checkout -b <my-branch>
git stash apply # 復元

git addまでしちゃった

# on main branch
git reset HEAD # addの取り消し
git stash save # 一時退避
git stash list
git checkout -b <my-branch>
git stash apply # 復元

git 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までしちゃった

そもそもできないように設定しておく(Branch Protection Rule)

参考文献