在 Visual Studio 中暫存程式碼行

暫存行支援 (也稱為互動式暫存) 適用於 Visual Studio 2022 17.3 版和更新版本。 暫存行可讓您直接從程式碼編輯器暫存檔案中的變更區塊。 當您想要將變更分割到不同的認可時,暫存行也很有用。

Screenshot of an example of line-staging support in Visual Studio 2022.

從 Visual Studio 2022 17.6 版開始,您使用暫留選項也可逐行「取消暫存」變更。

Screenshot of an example of how to unstage changes in Visual Studio 2022.

色彩邊界支援

您可能會注意到行預備支援的第一件事,就是編輯器如何視覺化捲軸和邊界上的 Git 變更。

Screenshot of the color margins in the Visual Studio 2022 editor.

色彩和註釋可讓您輕鬆地區分您已新增、刪除或修改的已儲存和未儲存變更。

Screenshot of differentiated and annotated color margin examples in Visual Studio 2022.

提示

您可以變更 Visual Studio 顯示的色彩,協助您管理變更。 移至 [工具]>[選項]>[環境]>[字型和色彩],然後從下列選項中選擇:

  • 在原始檔控制下追蹤文件新增
  • 在原始檔控制下追蹤文件刪除
  • 在原始檔控制下追蹤文件修改

Screenshot of the Options dialog box where you can change the colors Visual Studio uses for source control.

瞄核差異支援

色彩邊界為互動式。 當您選取色彩邊界時,Visual Studio 會開啟 [瞄核差異] 使用者介面 (UI),供您在編輯器中檢視變更,而不切換內容。 [瞄核差異 UI] 摘要說明已新增和移除的行數,並可讓您使用向上和向下箭號輕鬆地在變更間巡覽。

Screenshot of the Peek Difference user interface in Visual Studio as it relates to source control.

當您以滑鼠右鍵按一下包含變更的行時,您也可以存取 [瞄核差異]。 捷徑功能表隨即出現,您可以從中選取 [瞄核差異]。 或者,您可以使用 Alt+F8 鍵盤快速鍵。

Screenshot of the right-click context menu in Visual Studio where you can select Peek Difference.

注意

如果您偏好內嵌差異檢視,則可使用 [差異] 編輯器右上角的 [設定] 控制項切換至 [內嵌模式]

Screenshot of the Diff options available from the gear icon, where you can select the 'Inline mode' option.

提示

您可以從 [工具]>[選項]>[文字編輯器]>[進階],使用 [按一下以在邊界瞄核] 選項,在 [瞄核差異] UI 中修改 Visual Studio 與程式碼變更互動的方式。 Screenshot of the 'Click to peek in margin' setting.例如,您可以將預設的 [按一下] 設定變更為 [按兩下],也可以選取 [無] 來關閉 [瞄核差異 UI]。

暫存程式碼區塊

您可以使用瞄核差異使用者介面 (UI) 來暫存任何程式碼區塊。 若要這麼做,請將滑鼠暫留在您要暫存的變更上,然後選取 [暫存變更]。 或者,如果想要暫存您對文件進行的「所有」變更,請使用全域 [暫存] 按鈕。

Screenshot of the Stage Change option in Visual Studio 2022.

如果您偏好全螢幕差異檢視,可以切換至 [差異] 編輯器。 以下說明如何:選取瞄核差異 UI 右上角的 [升階為文件] 按鈕 。 或者,您可以使用 Ctrl+Alt+Home 鍵盤快速鍵。 [瞄核差異] UI 提供的所有功能也可在 [差異] 編輯器中使用。

Screenshot of the Stage Change difference view in Visual Studio 2022.

認可暫存變更

藉由檢閱並暫存您要包含的變更來準備認可之後,即可使用 [Git 變更] 視窗來建立認可。 接下來,輸入認可訊息,然後選取 [認可暫存]

Screenshot of the Git Changes dialog in Visual Studio 2022.