0
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 5 years have passed since last update.

git操作を間違えてremoteブランチを全部削除してしまったと思ったら、何とか大丈夫だった話

Last updated at Posted at 2020-01-10

起きたオペレーションミス

リモートブランチを消すために

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

  • ローカルに存在するブランチで
  • リモートに同名のブランチがある

をブランチを示す。
今回だとmasterdevelop、開発していた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 <その他ローカルにあってリモートにもあったブランチ名>
...

を実行したのと同義だった。
masterdevelopはローカルを最新化していなかったので、

 ! [rejected]          develop -> develop (non-fast-forward)
 ! [rejected]          master -> master (non-fast-forward)

などと表示されたということだった。

開発で使うコマンドを覚えて置くだけだと、ミスした時とても焦る

普段開発で使うコマンドを調べてメモしたり覚えておけば、普段git操作で困ることはない。
しかしコマンドを間違えてしまった場合に、:など意味を知らないとテンパってしまう。
時折ドキュメントを見返したり、git関連の書籍を読んでおくは大事だと改めて思った。

git参考書籍

GitHub実践入門
新卒エンジニア向け課題図書だったけども、改めて読み直すつもり。

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