diff --git a/app/models/repository.rb b/app/models/repository.rb
index 2a12b36a84ddd8ce2f8de1c5c837838770300a0c..c0c5816d1514c1e936f0e0452f2a01f6cb233461 100644
--- a/app/models/repository.rb
+++ b/app/models/repository.rb
@@ -312,11 +312,13 @@ class Repository
     if !branch_name || branch_name == root_ref
       branches.each do |branch|
         cache.expire(:"diverging_commit_counts_#{branch.name}")
+        cache.expire(:"commit_count_#{branch.name}")
       end
     # In case a commit is pushed to a non-root branch we only have to flush the
     # cache for said branch.
     else
       cache.expire(:"diverging_commit_counts_#{branch_name}")
+      cache.expire(:"commit_count_#{branch_name}")
     end
   end
 
@@ -496,6 +498,14 @@ class Repository
   end
   cache_method :commit_count, fallback: 0
 
+  def commit_count_for_ref(ref)
+    return 0 if empty?
+
+    cache.fetch(:"commit_count_#{ref}") do
+      raw_repository.commit_count(ref)
+    end
+  end
+
   def branch_names
     branches.map(&:name)
   end
diff --git a/changelogs/unreleased/1381-use-commit-count-for-pagination.yml b/changelogs/unreleased/1381-use-commit-count-for-pagination.yml
new file mode 100644
index 0000000000000000000000000000000000000000..ae84f64ed032b62cf1fa393f28e2600e56fc6593
--- /dev/null
+++ b/changelogs/unreleased/1381-use-commit-count-for-pagination.yml
@@ -0,0 +1,4 @@
+---
+title: Add pagination to project commits API endpoint
+merge_request: 5298
+author: Jordan Ryan Reuter
diff --git a/lib/api/commits.rb b/lib/api/commits.rb
index b0aa10f8bf2e7c5a5a4c55963de4297ee3b24977..6205bff3bc096800dfeb065326c30b6413eca454 100644
--- a/lib/api/commits.rb
+++ b/lib/api/commits.rb
@@ -33,7 +33,10 @@ module API
                                                   after: params[:since],
                                                   before: params[:until])
 
-        present commits, with: Entities::RepoCommit
+        commit_count = user_project.repository.commit_count_for_ref(ref)
+        paginated_commits = Kaminari.paginate_array(commits, total_count: commit_count)
+
+        present paginate(paginated_commits), with: Entities::RepoCommit
       end
 
       desc 'Commit multiple file changes as one commit' do
diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb
index eb992e1354e12da54d64e1ec269379ea2bcc7ca8..45acdc52d08fee698ae427f21e2db953e4ff8f61 100644
--- a/spec/models/repository_spec.rb
+++ b/spec/models/repository_spec.rb
@@ -1042,7 +1042,7 @@ describe Repository, models: true do
 
     it 'expires the cache for all branches' do
       expect(cache).to receive(:expire).
-        at_least(repository.branches.length).
+        at_least(repository.branches.length * 2).
         times
 
       repository.expire_branch_cache
@@ -1050,14 +1050,14 @@ describe Repository, models: true do
 
     it 'expires the cache for all branches when the root branch is given' do
       expect(cache).to receive(:expire).
-        at_least(repository.branches.length).
+        at_least(repository.branches.length * 2).
         times
 
       repository.expire_branch_cache(repository.root_ref)
     end
 
     it 'expires the cache for a specific branch' do
-      expect(cache).to receive(:expire).once
+      expect(cache).to receive(:expire).twice
 
       repository.expire_branch_cache('foo')
     end
@@ -1742,6 +1742,21 @@ describe Repository, models: true do
     end
   end
 
+  describe '#commit_count_for_ref' do
+    context 'with a non-existing repository' do
+      it 'returns 0' do
+        expect(repository).to receive(:raw_repository).and_return(nil)
+        expect(repository.commit_count).to eq(0)
+      end
+    end
+
+    context 'when searching for the root ref' do
+      it 'returns the same count as #commit_count' do
+        expect(repository.commit_count_for_ref(repository.root_ref)).to eq(repository.commit_count)
+      end
+    end
+  end
+
   describe '#cache_method_output', caching: true do
     context 'with a non-existing repository' do
       let(:value) do
diff --git a/spec/requests/api/commits_spec.rb b/spec/requests/api/commits_spec.rb
index 5190fcca2d1f2d5f9cd5c7ae869eb4f001fdb422..1d298988f174c7a8659e5b1b6a4c0922a01616a5 100644
--- a/spec/requests/api/commits_spec.rb
+++ b/spec/requests/api/commits_spec.rb
@@ -86,6 +86,43 @@ describe API::Commits, api: true  do
         expect(json_response.first["id"]).to eq("570e7b2abdd848b95f2f578043fc23bd6f6fd24d")
       end
     end
+
+    context 'pagination' do
+      it_behaves_like 'a paginated resources'
+
+      let(:page) { 0 }
+      let(:per_page) { 5 }
+      let(:ref_name) { 'master' }
+      let!(:request) do
+        get api("/projects/#{project.id}/repository/commits?page=#{page}&per_page=#{per_page}&ref_name=#{ref_name}", user)
+      end
+
+      it 'returns the commit count in the correct header' do
+        commit_count = project.repository.commit_count_for_ref(ref_name).to_s
+
+        expect(response.headers['X-Total']).to eq(commit_count)
+      end
+
+      context 'viewing the first page' do
+        it 'returns the first 5 commits' do
+          commit = project.repository.commit
+
+          expect(json_response.size).to eq(per_page)
+          expect(json_response.first['id']).to eq(commit.id)
+        end
+      end
+
+      context 'viewing the second page' do
+        let(:page) { 1 }
+
+        it 'returns the second 5 commits' do
+          commit = project.repository.commits('HEAD', offset: per_page * page).first
+
+          expect(json_response.size).to eq(per_page)
+          expect(json_response.first['id']).to eq(commit.id)
+        end
+      end
+    end
   end
 
   describe "Create a commit with multiple files and actions" do