diff --git a/app/controllers/projects/edit_tree_controller.rb b/app/controllers/projects/edit_tree_controller.rb
index 5d05c585ecc635d24959be93364e40fb879994e7..0e51ff59f39297eb6d204ce5792d4ee36cc1a809 100644
--- a/app/controllers/projects/edit_tree_controller.rb
+++ b/app/controllers/projects/edit_tree_controller.rb
@@ -45,5 +45,9 @@ class Projects::EditTreeController < Projects::ApplicationController
               end
 
     return access_denied! unless allowed
+
+    unless @repository.branch_names.include?(@ref)
+      redirect_to project_blob_path(@project, @id), notice: "You can only edit this file if you are on top of a branch"
+    end
   end
 end
diff --git a/app/views/projects/blob/_actions.html.haml b/app/views/projects/blob/_actions.html.haml
index 5641c528a4fc7b56d47fdda294d465832058806a..f6cc62e707e09473c875d47d5fad35ac1bea402c 100644
--- a/app/views/projects/blob/_actions.html.haml
+++ b/app/views/projects/blob/_actions.html.haml
@@ -1,7 +1,10 @@
 .btn-group.tree-btn-group
   -# only show edit link for text files
   - if @blob.text?
-    = link_to "edit", project_edit_tree_path(@project, @id), class: "btn btn-small", disabled: !allowed_tree_edit?
+    - if allowed_tree_edit?
+      = link_to "edit", project_edit_tree_path(@project, @id), class: "btn btn-small"
+    - else
+      %span.btn.btn-small.disabled Edit
   = link_to "raw", project_raw_path(@project, @id), class: "btn btn-small", target: "_blank"
   -# only show normal/blame view links for text files
   - if @blob.text?