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

git push --force は使わないようにする (--force-with-lease を使う)

Posted at

はじめに

git rebase -i などでコミット履歴を書き換えた後、リモートブランチへ push しようとすると、通常の git push は失敗します。
その際に --force オプションを使うケースが多いです。

しかしこのオプションを指定すると、他の人が同一ブランチで作業している場合に、変更を上書きしてしまうリスクがあります。
そういったリスクを抑えることができるのが、--force-with-lease オプションです。

git push --force のおさらい

--force は、リモートの状態を一切気にせず強制的に push するオプションです。

git push --force

このコマンドを実行すると、リモートブランチに誰かの変更があっても、自分のローカル履歴で完全に上書きします。そのため、誤って他人のコミットを消してしまう可能性があります。

git push --force-with-lease の場合

--force-with-lease は、自分が最後に取得した状態からリモートが変わっていない場合のみ force push を許可するオプションです。

git push --force-with-lease

リモートが自分の認識通りであれば push は成功し、他の誰かが先に push している場合は失敗します。--force の危険性を抑えた、安全寄りの force push です。

何がどう違うのか

  • --force:リモートの状態に関係なく強制的に上書きする
  • --force-with-lease:リモートが更新されていない場合のみ上書きを許可する

終わりに

--force--force-with-lease は似ていますが、安全性には大きな違いがあります。
自分のみコミットする feature ブランチを切って作業している場合、どちらを使っても問題はありません。
しかし、--force-with-lease を使う癖をつけておいた方が、いざというときの事故のリスクを抑えることができるため、極力後者を使っておく様にしましょう。

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