Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Delete remote branch with lease

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:

  1. I fetch branch X and confirm that it points to commit A, which is fully merged.
  2. Unbeknownst to me, someone pushes commit B to branch X.
  3. I delete the remote branch using git push origin --delete X.
  4. Commit B is now unreferenced in the remote repository!

This is what I would like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. I fetch branch X and confirm that it points to commit A, which is fully merged.
  2. Unbeknownst to me, someone pushes commit B to branch X.
  3. I run some command that deletes remote branch X only it still points to commit A.
  4. 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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading