在原始檔控制系統中省略 NuGet 套件Omitting NuGet packages in source control systems

開發人員通常會省略其原始檔控制存放庫的 NuGet 套件,改為依賴套件還原先重新安裝專案的相依性再建置。Developers typically omit NuGet packages from their source control repositories and rely instead on package restore to reinstall a project's dependencies before a build.

依賴套件還原的原因包括:The reasons for relying on package restore include the following:

  1. 分散式版本設定系統,例如 Git,包含存放庫內每個檔案每個版本的完整複本。Distributed version control systems, such as Git, include full copies of every version of every file within the repository. 經常更新的二進位檔案會導致明顯膨脹,並會增加複製存放庫所花費的時間。Binary files that are frequently updated lead to significant bloat and lengthens the time it takes to clone the repository.
  2. 當套件包含在存放庫中時,開發人員傾向直接參考磁碟上的套件內容,而不是透過 NuGet 參考套件,這會導致專案中的硬式編碼路徑名稱。When packages are included in the repository, developers are liable to add references directly to package contents on disk rather than referencing packages through NuGet, which can lead to hard-coded path names in the project.
  3. 清除任何未使用過的套件資料夾的解決方案會變得愈來愈困難,因為需要確保不刪除仍在使用的任何套件。It becomes harder to clean your solution of any unused package folders, as you need to ensure you don't delete any package folders still in use.
  4. 透過略過套件的方式,維護程式碼和來自其他相依人員的套件之間的清楚擁有權界限。By omitting packages, you maintain clean boundaries of ownership between your code and the packages from others that you depend upon. 許多 NuGet 套件已在自己的原始檔控制存放庫中進行維護。Many NuGet packages are maintained in their own source control repositories already.

雖然套件還原是 NuGet 的預設行為,但仍需某些手動工作從原始檔控制省略套件,—也就是專案中的 packages 資料夾—,如本文所述。Although package restore is the default behavior with NuGet, some manual work is necessary to omit packages—namely, the packages folder in your project—from source control, as described in this article.

使用 Git 省略套件Omitting packages with Git

使用 .gitignore 檔案略過 NuGet 套件 (.nupkg) packages 資料夾,以及 project.assets.json 和其他項目。Use the .gitignore file to ignore NuGet packages (.nupkg) the packages folder, and project.assets.json, among other things. 如需參考,請參閱 Visual Studio 專案的範例 .gitignoreFor reference, see the sample .gitignore for Visual Studio projects:

.gitignore 檔案的重要部分為:The important parts of the .gitignore file are:

# Ignore NuGet Packages
*.nupkg

# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*

# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/

# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config

# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets

# Ignore other intermediate files that NuGet might create. project.lock.json is used in conjunction
# with project.json (NuGet v3); project.assets.json is used in conjunction with the PackageReference
# format (NuGet v4 and .NET Core).
project.lock.json
project.assets.json

使用 Team Foundation 版本設定略過套件Omitting packages with Team Foundation Version Control

注意

如果可能,請先遵循下列指示再 將專案新增至原始檔控制。Follow these instructions if possible before adding your project to source control. 否則,請手動刪除存放庫的 packages 資料夾,並先簽入該變更再繼續。Otherwise, manually delete the packages folder from your repository and check in that change before continuing.

停用原始檔控制與所選檔案的 TFVC 整合:To disable source control integration with TFVC for selected files:

  1. 在解決方案資料夾 (.sln 檔案所在的資料夾) 中建立名為 .nuget 的資料夾。Create a folder called .nuget in your solution folder (where the .sln file is).

    • 提示:在 Windows 中,在 Windows 檔案總管中建立這個資料夾,要使用名稱 .nuget.「加」 結尾後置點。Tip: on Windows, to create this folder in Windows Explorer, use the name .nuget. with the trailing dot.
  2. 在該資料夾中,建立名為 NuGet.Config 的檔案,再開啟進行編輯。In that folder, create a file named NuGet.Config and open it for editing.

  3. 新增下列文字作為最小值,其中 disableSourceControlIntegration 設定會指示 Visual Studio 略過 packages 資料夾中的所有內容:Add the following text as a minimum, where the disableSourceControlIntegration setting instructs Visual Studio to skip everything in the packages folder:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <solution>
            <add key="disableSourceControlIntegration" value="true" />
        </solution>
    </configuration>
    
  4. 如果您使用的是 TFS 2010 或更舊的版本,請隱匿工作區對應中的 packages 資料夾。If you are using TFS 2010 or earlier, cloak the packages folder in your workspace mappings.

  5. 在 TFS 2012 或更新版本上,或使用 Visual Studio Team Services,建立 .tfignore 檔案,如新增檔案到伺服器 (英文) 所述。On TFS 2012 or later, or with Visual Studio Team Services, create a .tfignore file as described on Add Files to the Server. 在該檔案中納入以下內容,明確忽略在存放庫層級和其他幾個中繼檔案中對 \packages 資料夾的修改。In that file, include the content below to explicitly ignore modifications to the \packages folder on the repository level and a few other intermediate files. (您可以在 Windows 檔案總管中,使用 .tfignore. 加結尾後置點的名稱來建立檔案,但可能需要先停用 [Hide known file extensions] (隱藏已知副檔名) 選項。):(You can create the file in Windows Explorer using the name a .tfignore. with the trailing dot, but you might need to disable the "Hide known file extensions" option first.):

    # Ignore NuGet Packages
    *.nupkg
    
    # Ignore the NuGet packages folder in the root of the repository. If needed, prefix 'packages'
    # with additional folder names if it's not in the same folder as .tfignore.   
    packages
    
    # Omit temporary files
    project.lock.json
    project.assets.json
    *.nuget.props
    
  6. NuGet.Config.tfignore 新增至原始檔控制,並簽入變更。Add NuGet.Config and .tfignore to source control and check in your changes.