Forward port the "funny ref avoidance" in clone and fetch from maint branch.