Is there a way to delete a remote branch only if it is at the commit where my local repo thinks it is? Something like –force-with-lease, but for branch deletion instead of force-pushing.
This is what I fear:
- I fetch branch X and confirm that it points to commit A, which is fully merged.
- Unbeknownst to me, someone pushes commit B to branch X.
- I delete the remote branch using
git push origin --delete X. - Commit B is now unreferenced in the remote repository!
This is what I would like:
- I fetch branch X and confirm that it points to commit A, which is fully merged.
- Unbeknownst to me, someone pushes commit B to branch X.
- I run some command that deletes remote branch X only it still points to commit A.
- The command reports to me that it failed because remote branch X has advanced in the meantime.
>Solution :
Is there a way to delete a remote branch only if it is at the commit where my local repo thinks it is? Something like –force-with-lease, but for branch deletion instead of force-pushing.
The answer appears to be, "--force-with-lease". If the remote repository has been updated since I last update my local repository, this fails:
$ git push --delete --force-with-lease origin testbranch
To .../upstream
! [rejected] (delete) -> testbranch (stale info)
error: failed to push some refs to '.../upstream'
Whereas this succeeds:
$ git push --delete origin testbranch
To .../upstream
- [deleted] testbranch