起きたオペレーションミス
リモートブランチを消すために
git push oirign :<消したかったブランチ>
をやろうとしたところ、ブランチ名をペーストする前にエンターを押してしまい、
git push oirign :
を実行してしまった。
**ブランチ名を指定しなかったということは、全てのブランチが消されてしまうのでは?!**と非常に焦った。
実行して表示されたもの
$ git push origin :
To git@hoge.com:fuga/fuga.git
! [rejected] develop -> develop (non-fast-forward)
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@hoge.com:fuga/fuga.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
failed to push some refs
とあるということは、いくつかのブランチに対してはpushがされたということか?!
non-fast-forward
でローカルのブランチが古かったものは何もされていないけど、最新だったブランチは削除されてしまったの?!
後から冷静に読めば
最新でないブランチだったからpush出来なかったと書いてあるだけなのだけど、
git push origin :<ブランチ名>
でブランチを削除出来るのだから、pushという操作にブランチ削除も含まれるのではと思い焦っていた。
急いでremoteのブランチ一覧を確認
元々いくつbranchがあったかは分からないけど、どうやらブランチは残っていそう。
でも何が起きたか分からないと不安で仕方ない。
git push origin : の挙動を落ち着いて調べてみる
git pushドキュメントの例に
git push origin :
があったの読んでみた。
https://git-scm.com/docs/git-push#Documentation/git-push.txt-codegitpushorigincode
Push "matching" branches to origin. See <refspec> in the OPTIONS section above for a description of "matching" branches.
"matching" branches
をpushするらしい。
"matching" branches
が何を示すのか確認してみた。
:は"matching branch"を表す
https://git-scm.com/docs/git-push#OPTIONS
に説明があった。
The special refspec : (or +: to allow non-fast-forward updates) directs Git to push "matching" branches: for every branch that exists on the local side, the remote side is updated if a branch of the same name already exists on the remote side.
"matching" branches
は
- ローカルに存在するブランチで
- リモートに同名のブランチがある
をブランチを示す。
今回だとmaster
やdevelop
、開発していたfeatureブランチなどがローカルにあったので、
今回間違って打った
git push origin :
は、
git push origin master
git push origin develop
git push origin feature/xxxx
git push origin feature/yyyy
git push origin <その他ローカルにあってリモートにもあったブランチ名>
...
を実行したのと同義だった。
master
やdevelop
はローカルを最新化していなかったので、
! [rejected] develop -> develop (non-fast-forward)
! [rejected] master -> master (non-fast-forward)
などと表示されたということだった。
開発で使うコマンドを覚えて置くだけだと、ミスした時とても焦る
普段開発で使うコマンドを調べてメモしたり覚えておけば、普段git操作で困ることはない。
しかしコマンドを間違えてしまった場合に、:
など意味を知らないとテンパってしまう。
時折ドキュメントを見返したり、git関連の書籍を読んでおくは大事だと改めて思った。
git参考書籍
GitHub実践入門
新卒エンジニア向け課題図書だったけども、改めて読み直すつもり。