LoginSignup
516
331

More than 3 years have passed since last update.

git push -f をやめて --force-with-lease を使おう

Last updated at Posted at 2017-09-17

TL;DR

git push -fをやめて --force-with-leaseを使いましょう!

terminal
$ git push --force-with-lease origin main

--force-with-lease とは

PUSHの際、ローカルrefとリモートrefを比較しローカルが最新か判定し、最新でなければPUSHが失敗するというもの。
※ただし、直前にfetchしているとPUSHが成功してしまうので注意

エイリアスに登録する

--force-with-lease とか長すぎるので、エイリアスに登録する

terminal
# ~/.gitconfigに追加
$ git config --global alias.push-f "push --force-with-lease"

terminal
$ git push-f origin main

force push問題

rebaseなどの作業の際、強制PUSHが必要なタイミングが出てくるが --forceではローカルの内容を破壊的にリモートレポジトリを上書きしてしまう。

同じブランチで複数人開発していた場合にタイミングによっては
「◯◯さんのコミットを吹き飛ばしちゃった//」 が発生する可能性が十分ある。

そもそも上記の運用方法に問題がある気はするが、どんな運用をしていたとしても
force pushする際は --force-with-leaseオプションを必ずつけるようにしておいて損はないと思う。

TIPS: Github上でブランチを削除できないようにする

消されるとまずいブランチは [Settings] → [Branches]でさまざまなprotectionルールをかけておきましょう!

スクリーンショット 2021-03-25 19.22.50.png

516
331
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
516
331