LoginSignup
321

More than 1 year has passed since last update.

posted at

updated at

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

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

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
What you can do with signing up
321