diff --git a/modules/convert/pull.go b/modules/convert/pull.go
index ccf64ef3e..6ff88c4d8 100644
--- a/modules/convert/pull.go
+++ b/modules/convert/pull.go
@@ -114,7 +114,7 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest {
 		if git.IsErrBranchNotExist(err) {
 			headCommitID, err := headGitRepo.GetRefCommitID(apiPullRequest.Head.Ref)
 			if err != nil && !git.IsErrNotExist(err) {
-				log.Error("GetCommit[%s]: %v", headBranch.Name, err)
+				log.Error("GetCommit[%s]: %v", pr.HeadBranch, err)
 				return nil
 			}
 			if err == nil {
diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go
index d20c6540e..618b89438 100644
--- a/modules/git/repo_commit.go
+++ b/modules/git/repo_commit.go
@@ -21,6 +21,11 @@ import (
 func (repo *Repository) GetRefCommitID(name string) (string, error) {
 	ref, err := repo.gogitRepo.Reference(plumbing.ReferenceName(name), true)
 	if err != nil {
+		if err == plumbing.ErrReferenceNotFound {
+			return "", ErrNotExist{
+				ID: name,
+			}
+		}
 		return "", err
 	}
 
diff --git a/routers/repo/branch.go b/routers/repo/branch.go
index ea4f68948..7127d50ed 100644
--- a/routers/repo/branch.go
+++ b/routers/repo/branch.go
@@ -17,7 +17,6 @@ import (
 	"code.gitea.io/gitea/modules/repofiles"
 	repo_module "code.gitea.io/gitea/modules/repository"
 	"code.gitea.io/gitea/modules/util"
-	"gopkg.in/src-d/go-git.v4/plumbing"
 )
 
 const (
@@ -253,7 +252,7 @@ func loadBranches(ctx *context.Context) []*Branch {
 					repoIDToGitRepo[pr.BaseRepoID] = baseGitRepo
 				}
 				pullCommit, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName())
-				if err != nil && err != plumbing.ErrReferenceNotFound {
+				if err != nil && !git.IsErrNotExist(err) {
 					ctx.ServerError("GetBranchCommitID", err)
 					return nil
 				}