首页 > 解决方案 > Git push atomically, only if the target branch is at the expected SHA

问题描述

Is there a way to push to a branch only if it hasn't changed since I last fetched it?

More specifically, I'd like to delete branches that have already been merged, but only if no one else pushes new commits to them between when I check that the branch has been merged and when I push the deletion.

From the git push documentation, I see that it does support a --atomic option, but that option is for making updates to multiple branches atomically with respect to each other, rather than for ensuring that my update to a remote branch will be atomic with respect to other users' updates.

Ie, I want to delete merged branches on a remote git repository without any risk of deleting anyone else's work, even if they are working concurrently.

标签: git

解决方案


是的,这可以通过使用该--force-with-lease选项来实现。例如:

$ git push --force-with-lease=refs/heads/foo origin :refs/heads/foo

这是为强制推送而设计的,但也适用于删除。该选项有许多变体,如果您愿意,可以采用显式值。


推荐阅读