在 Visual Studio 中複製 Git 存放庫

Visual Studio 可讓您輕鬆地從 IDE 複製存放庫。 您可以從遠端與您選擇的 Git 提供者合作,例如 GitHub 或 Azure DevOps。 如果您需要建立新的存放庫,而不是複製現有的存放庫,請參閱從 Visual Studio 建立存放庫

必要條件

若要遵循這篇文章,您需要:

複製 GitHub 存放庫並登入

  1. 開啟 Visual Studio。

  2. 從 [Git] 功能表,選取 [複製存放庫]

    Screenshot of the Clone Repository option from the Git menu in Visual Studio.

    注意

    如果您之前尚未與 [Git] 功能表互動,您可能會看到 [複製],而不是 [複製存放庫]。 如果是,請選取 [複製]

    而且,如果 [Git] 不在功能表列上,請移至 [工具]>[選項]>[原始檔控制]>[外掛程式選取],然後 從 [目前原始檔控制外掛程式] 下拉式清單中選取 [Git]

  3. 在 [複製存放庫] 視窗的 [輸入 Git 存放庫 URL] 區段底下,於 [存放庫位置] 方塊中新增您的存放庫資訊。

    接下來,在 [路徑] 區段中,您可選擇接受本機來源檔案的預設路徑,或瀏覽至不同的位置。

    然後,在 [瀏覽存放庫] 區段中,選取 [GitHub]

    Screenshot of the Clone a Repository dialog box with GitHub highlighted.

  4. 在 [從 GitHub 開啟] 視窗中,您可以確認 GitHub 帳戶資訊,也可加以新增。 若要這麼做,請從下拉式功能表中選取 [登入]

    Screenshot of the Sign in drop-down section of the Open from GitHub window.

    如果您是第一次從 Visual Studio 登入 GitHub,則會出現 [授權 Visual Studio] 通知。 選擇您想要的選項,然後選取 [授權 github]

    Screenshot of the authorization dialog box.

    接下來,您會看到授權確認視窗。 輸入您的密碼,然後選取 [確認密碼]

    Screenshot of the confirm access dialog box.

    將 GitHub 帳戶與 Visual Studio 連結之後,[成功] 通知隨即出現。

    Screenshot of the Success notification you receive after you link your GitHub account with Visual Studio.

  5. 登入之後,Visual Studio 會返回 [複製存放庫] 對話方塊,其中 [從 GitHub 開啟] 視窗會列出您有權存取的所有存放庫。 選取您想要的存放庫,然後選取 [複製]

    如果存放庫清單未出現,請輸入存放庫的位置,然後選取 [複製]

    Screenshot of the Open from GitHub window where you can select a repo or add one.

  6. 接下來,Visual Studio 會在存放庫中顯示解決方案清單。 選擇您想要載入的解決方案,或在方案總管中開啟 [資料夾檢視]

    Screenshot of the Folder View in Solution Explorer in Visual Studio 2022.

    提示

    您可以從 [Git] 功能表將預設 [資料夾檢視] 變更為 [解決方案檢視]。 選取 [設定]>[原始程式碼控制]>[Git 全域設定]>[開啟 Git 存放庫時自動載入解決方案] 來執行此動作。

開啟現有的本機存放庫

複製存放庫或建立存放庫之後,Visual Studio 會偵測 Git 存放庫並將它新增至 Git 功能表中 [本機存放庫] 清單。 您可以從該處快速存取和切換 Git 存放庫。

瀏覽至 Azure DevOps 存放庫而後予以複製

  1. 開啟 Visual Studio。

  2. 從 [Git] 功能表,選取 [複製存放庫]

    Screenshot of the full Clone Repository option from the Git menu in Visual Studio.

  3. 在 [複製存放庫] 對話方塊的 [瀏覽存放庫] 區段中,選取 [Azure DevOps]

    Screenshot of the 'Browse a repository' section of the 'Clone a repository' dialog box in Visual Studio, Azure DevOps highlighted.

  4. [連線到專案] 對話方塊隨即出現。 依照提示登入您的 Azure 帳戶,然後瀏覽至裝載您所尋找檔案的 Azure DevOps Server。