使用 GitWorking with Git

Git 是一種分散式版本控制系統,可讓小組同時處理相同的文件。Git is a distributed version control system that allows teams to work on the same documents simultaneously. 這表示有中央伺服器包含所有檔案,但從這個中央來源簽出存放庫時,會將整個存放庫複製至本機電腦。This means that there is a central server that contains all the files, but when a repository is checked out from this central source, the entire repository is cloned to the local machine.

下列各節將探討如何在 Visual Studio for Mac 中使用 Git 進行版本控制。The sections below will explore how Git can be used for version control in Visual Studio for Mac.

Git 版本控制功能表Git version control menu

下圖說明 Visual Studio for Mac 透過 [版本控制] 功能表項目所提供的選項:The image below illustrates the options provided by Visual Studio for Mac by the Version Control menu item:


推送和提取Push and Pull

推送和提取是 Git 內的兩個最常用動作。Pushing and Pulling are two of the most commonly used actions within Git. 若要同步處理其他人已對遠端存放庫進行的變更,您必須從該處提取To synchronize changes that other people have made to the remote repository, you must Pull from there. 這是在 Visual Studio for Mac 中完成,方法是選取 [版本控制] > [更新方案] 。This is done in Visual Studio for Mac by selecting Version Control > Update Solution.

在您更新、檢閱並認可檔案之後,接著必須將它們推送到遠端存放庫,讓其他人可以存取您的變更。Once you have updated your files, reviewed and committed them, you must then Push them to the remote repository to allow others to access your changes. 這是在 Visual Studio for Mac 中完成,方法是選取 [版本控制] > [推送變更] 。This is done in Visual Studio for Mac by selecting Version Control > Push Changes. 這會顯示 [推送] 對話方塊以讓您檢視已認可的變更,並選取要推送到其中的分支:This will display the Push dialog, allowing you to view the committed changes, and select the branch to push to:


您也可以透過 [認可] 對話方塊,同時認可並推送您的變更:You can also Commit and Push your changes at the same time, via the Commit dialog:


改動者、記錄和合併Blame, Log, and Merge

在視窗底部,會顯示五個索引標籤,如下所示:At the bottom of the window, there are five tabs displayed, as illustrated below:


這些允許下列動作:These allow the following actions:

  • 來源 - 顯示原始程式碼檔。Source - Displays your source code file.

  • 變更 - 顯示您的本機檔案與基底檔案之間的程式碼變更。Changes - Displays the change in code between your local file and the base file. 您也可以比較不同雜湊中檔案的不同版本:You can also compare different versions of the file from different hashes:


  • 改動者 - 顯示與每個程式碼區段建立關聯之使用者的使用者名稱。Blame - Displays the username of the user associated with each section of code.

  • 記錄 - 顯示所有認可、時間、日期、訊息,以及負責檔案的使用者:Log - Displays all the commits, times, dates, messages, and users that are responsible for the file:


  • 合併 - 如果您在認可工作時發生合併衝突,則可以使用此項目。Merge - This can be used if you have a merge conflict when committing your work. 它會以視覺方式呈現您和其他開發人員所做的變更,讓您可以完全合併這兩個程式碼區段。It shows a visual representation of the changes made by you and the other developer, allowing you to combine both sections of code cleanly.

切換分支Switching branches

根據預設,存放庫中所建立的第一個分支稱為  Master  分支。By default, the first branch created in a repository is known as the Master branch. 就技術上而言,master 分支與任何其他分支沒有任何差別,但 master 分支是開發小組最常視為 'live' 或 'production' 分支的分支。There isn't technically anything different between the master branch and any other, but the master branch is the one that is most often thought of in development teams as the 'live' or 'production' branch.

將 Master 進行分支,即可建立獨立開發生產線 (在該情況下,為任何其他分支)。An independent line of development can be created by branching off Master (or any other branch, for that matter). 這提供某個時間點的新版 master 分支,允許與 'live' 分支獨立進行開發。This provides a new version of the master branch at a point in time, allowing for development independently of what is 'live.' 以這種方式使用分支,通常用於軟體開發中的功能Using branches in this way is often used for features in software development

使用者可以針對每個存放庫建立所需數目的分支,但建議在完成分支的使用之後,將它刪除,以保持存放庫的組織性。Users can create as many branches as they like for each repository, but it is recommended that once they have finished using a branch, it is deleted it to keep the repository organized.

瀏覽至 [版本控制] > [管理分支和遠端] ,以在 Visual Studio for Mac 中檢視分支:Branches are viewed in Visual Studio for Mac by browsing to Version Control > Manage Branches and Remotes...:


切換至另一個分支,方法是在清單中選取它,並按 [切換至分支] 按鈕。Switch to another branch by selecting it in the list and pressing the Switch to Branch button.

若要建立新的分支,請選取 [Git 存放庫組態] 對話方塊中的 [新增] 按鈕。To create a new branch select the New button in the Git repository configuration dialog. 輸入新的分支名稱:Enter the new branch name:


您也可以將遠端分支設定為 tracking 分支。You can also set a remote branch to your tracking branch. 請深入閱讀 Git 文件,以了解如何追蹤分支。Read more about tracking branches in the Git documentation.

查看專案名稱旁之 Solution Pad 中的目前分支:See the current branch in the Solution Pad, next to the project name:

Solution Pad 中顯示的目前分支

檢閱並認可Reviewing and committing

若要檢閱檔案中的變更,請使用每個文件上的 [變更]、[改動者]、[記錄] 和 [合併] 索引標籤,如本主題稍早所示。To review changes in the files, use the Changes, Blame, Log, and Merge tabs on each document, illustrated earlier in this topic.

瀏覽至 [版本控制] > [Review Solution and Commit] (檢閱方案並認可) 功能表項目,以檢閱專案中的所有變更:Review all changes in your project by browsing to the Version Control > Review Solution and Commit menu item:


這允許使用 [還原]、[建立修補檔案] 或 [認可] 的選項來檢視專案之每個檔案中的所有變更。This allows viewing of all the changes in each file of a project with the option to Revert, Create a Patch, or Commit.

若要將檔案認可到遠端存放庫,請按 [認可] ,並輸入認可訊息,然後使用 [認可] 按鈕確認:To commit a file to the remote repository, press Commit, enter a commit message, and confirm with the Commit Button:


在您認可變更之後,請將它們推送到遠端存放庫,讓其他使用者可以看到它們。Once you have committed your changes, push them to the remote repository to allow other users to see them.

另請參閱See also