PostReceive fails in Repository#up_to_date_with_upstream?

https://sentry.gitlap.com/gitlab/gitlabcom/issues/475699/

NoMethodError: undefined method `id' for nil:NilClass
  ee/repository.rb:67:in `up_to_date_with_upstream?'
    ancestor?(branch_commit.id, upstream_commit.id)
  git_push_service.rb:144:in `execute_related_hooks'
    mirror_update = project.mirror? && project.repository.up_to_date_with_upstream?(branch_name)
  git_push_service.rb:62:in `execute'
    execute_related_hooks
  post_receive.rb:45:in `block in process_project_changes'
    GitPushService.new(post_received.project, @user, oldrev: oldrev, newrev: newrev, ref: ref).execute
  gitlab/git_post_receive.rb:25:in `block in changes_refs'
    yield oldrev, newrev, ref
...
(43 additional frame(s) were not displayed)

undefined method `id' for nil:NilClass