﻿# Git 儲存庫太大導致無法上傳 Visual Studio Online 如何處理

照理來說，版本控管通常用來控管**程式原始碼**，不過第一次使用版控的人總是抓不準尺度，硬是把動輒數百 MB 的影片放進版本庫，不然就是設計師把數十到數百 MB 的 ai 檔簽入到版本庫中。你知道的，Git 版控會把所有東西放進版本庫，刪掉的檔案也一樣，但 Git 優異的檔案系統設計，這點小檔案不成問題。不過，當你要 push 到遠端儲存庫時，像是 [GitHub](https://github.com/) 、 [Bitbucket](https://bitbucket.org/) 或 [Visual Studio Online](http://www.visualstudio.com/) 這類遠端服務，通常都會有些 push 的限制，這篇文章主講 Visual Studio Online 的解決方法。

我們先來看看錯誤訊息顯示什麼。當你 push 一個含有**大檔案**的 Git 儲存庫到 Visual Studio Online 上的 Git 遠端儲存庫，可能會遇到如下問題：

> Counting objects: 4311, done.  
> Delta compression using up to 8 threads.  
> Compressing objects: 100% (1567/1567), done.  
> **Unable to rewind rpc post data - try increasing http.postBuffer  
> error**: RPC failed; result=56, **HTTP code** = **0  
> ****fatal**: The remote end hung up unexpectedly  
> Writing objects: 100% (4311/4311), 250.71 MiB | 1.12 MiB/s, done.  
> Total 4311 (delta 2472), reused 4311 (delta 2472)  
> **fatal**: **The remote end hung up unexpectedly**  
> Everything up-to-date  
>   
> 如下圖示，錯誤訊息很難看，畢竟沒有高量提示：  
> ![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/309652d239247400071b-image_2525.png)

這問題通常不是立刻發生，而是上傳了十幾分鐘後才發生的錯誤，而且是從「伺服器端」切斷連線的，所以很難知道應該如何解決。

不過錯誤訊息中有提到【 **Unable to rewind rpc post data - try increasing http.postBuffer** 】，我也從 Visual Studio 論壇查到這篇文章 TFS 2013 Visual Studio Online Git Push Size Limitation，上面提到可以設定以下設定，將 http.postBuffer 調高數值： ( **※請先不要複製以下指令執行，這是錯誤示範** )

```
git config --global http.postBuffer 524288000
```

我也照做了，然後再重新 push 一次，結果立刻得到一個更怪的錯誤，直接得到 HTTP 404 的錯誤：

> Counting objects: 4311, done.  
> Delta compression using up to 8 threads.  
> Compressing objects: 100% (1567/1567), done.  
> Writing objects: 100% (4311/4311), 250.71 MiB | 298.67 MiB/s, done.  
> Total 4311 (delta 2472), reused 4311 (delta 2472)  
> fatal: The remote end hung up unexpectedly  
> **Error**: RPC failed; result=22, **HTTP code** = **404**  
> Everything up-to-date  
> 
> ![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/5356bc64dd1db88a3be4-image_2526.png)

其實是 **http.postBuffer** 被調太高了，我降低了一點點，結果還是不行，最後就一直試一直試，調到 260MB 大小才順利將專案 push 上去！

```
git config --global http.postBuffer 260000000
```

其他 Git 平台的解決方法不盡相同，不過如果遇到也可以先試試看，如果其他平台的問題我以後有遇到，我會再來整理成文章分享，如果你已經遇到，可參考以下相關連結，我有列出不同平台的解決方法。  

**相關連結**

-   Visual Studio Online

-   TFS 2013 Visual Studio Online Git Push Size Limitation

-   GitHub

-   [Working with large files · GitHub Help](https://help.github.com/articles/working-with-large-files)
-   [What is my disk quota? · GitHub Help](https://help.github.com/articles/what-is-my-disk-quota)
-   [Remove sensitive data · GitHub Help](https://help.github.com/articles/remove-sensitive-data)

-   BitBucket

-   [What kind of limits do you have on repository/file/upload size? – Bitbucket](https://confluence.atlassian.com/pages/viewpage.action?pageId=273877699)
-   [Repository size limits | Bitbucket Blog](http://blog.bitbucket.org/2014/05/30/repository-size-limits/)
-   [Reduce repository size - Bitbucket](https://confluence.atlassian.com/display/BITBUCKET/Reduce+repository+size;jsessionid=95B4173611B6DE6FFBB80D89C0A777C2.node1)
-   [How to handle big repositories with git - Atlassian Blogs](http://blogs.atlassian.com/2014/05/handle-big-repositories-git/)

-   其他相關資源

-   [BFG Repo-Cleaner by Roberto Tyley](http://rtyley.github.io/bfg-repo-cleaner/) ( 有效刪除**歷史大檔**的工具 )
-   [Rewriting Git project history with The BFG | Info | theguardian.com](http://www.theguardian.com/info/developer-blog/2013/apr/29/rewrite-git-history-with-the-bfg)
