Git: --force-with-lease and multiple pushurls

What I have come up with is

git push --force-with-lease=`git rev-parse --abbrev-ref @{u} | sed 's![^/]*/!!'`:@{u}

where

  • git rev-parse --abbrev-ref @{u} | sed 's![^/]*/!!' gets the remote branch name (without the remote name)
  • @{u} is the commit where we expect the remote branch to be

I also tried out the + spec as suggested by Haralan Dobrev but was able to override changes using that. Therefore, I think that the + spec should not be used here because it equals a normal force push.


Very interesting question! I've tried it out and successfully reproduced what you are describing with Git 2.11.0 in these test repos:

  • https://github.com/hkdobrev/git-test
  • https://github.com/hkdobrev/git-test2

I was able to successfully push with --force-with-lease to both remote URLs by using the following form:

git push --force-with-lease origin +master

Notice the + sign before the branch name. Here is the output:

$ git push --force-with-lease origin +master
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 232 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To github.com:hkdobrev/git-test.git
 + 099b95f...08c7548 master -> master (forced update)
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 232 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To github.com:hkdobrev/git-test2.git
 + 099b95f...08c7548 master -> master (forced update)

From the git-push (1) man page:

Note that --force applies to all the refs that are pushed, hence using it with push.default set to matching or with multiple push destinations configured with remote.*.push may overwrite refs other than the current branch (including local refs that are strictly behind their remote counterpart). To force a push to only one branch, use a + in front of the refspec to push (e.g git push origin +master to force a push to the master branch). See the <refspec>... section above for details.

Tags:

Git