diff --git a/app/controllers/projects/compare_controller.rb b/app/controllers/projects/compare_controller.rb
index 7c20b81c0b1b434d037f4425faec9806c04972f2..c5f085c236f70d4acb35f4d9a92d77130f7fb870 100644
--- a/app/controllers/projects/compare_controller.rb
+++ b/app/controllers/projects/compare_controller.rb
@@ -6,11 +6,12 @@ class Projects::CompareController < Projects::ApplicationController
   before_action :authorize_download_code!
 
   def index
+    @ref = Addressable::URI.unescape(params[:to])
   end
 
   def show
     base_ref = Addressable::URI.unescape(params[:from])
-    head_ref = Addressable::URI.unescape(params[:to])
+    @ref = head_ref = Addressable::URI.unescape(params[:to])
 
     compare_result = CompareService.new.execute(
       current_user,
diff --git a/app/views/projects/commits/_head.html.haml b/app/views/projects/commits/_head.html.haml
index a714f5f79e0f602ffcfcc754bd61edf224bcfa62..66101f3f0da684c2da8d4eb56db78edb796263bd 100644
--- a/app/views/projects/commits/_head.html.haml
+++ b/app/views/projects/commits/_head.html.haml
@@ -1,6 +1,6 @@
 %ul.nav.nav-tabs
   = nav_link(controller: [:commit, :commits]) do
-    = link_to namespace_project_commits_path(@project.namespace, @project, @repository.root_ref) do
+    = link_to namespace_project_commits_path(@project.namespace, @project, @ref || @repository.root_ref) do
       Commits
       %span.badge= number_with_precision(@repository.commit_count, precision: 0, delimiter: ',')
   = nav_link(controller: :compare) do