開始在 Windows 子系統 Linux 版上使用 GitGet started using Git on Windows Subsystem for Linux

Git 是最常使用的版本控制系統。Git is the most commonly used version control system. 您可以使用 Git 來追蹤您對檔案所做的變更,以記錄已完成的動作,並可在需要時還原為舊版的檔案。With Git, you can track changes you make to files, so you have a record of what has been done, and have the ability to revert to earlier versions of the files if needed. Git 也可以簡化共同作業,讓多人所做的變更全部合併到一個來源中。Git also makes collaboration easier, allowing changes by multiple people to all be merged into one source.

Git 可以安裝在 Windows 和 WSL 上Git can be installed on Windows AND on WSL

重要考慮:當您啟用 WSL 並安裝 Linux 發行版本時,會安裝與 Windows NTFS C:\ 分開的新檔案系統電腦上的磁片磁碟機。An important consideration: when you enable WSL and install a Linux distribution, you are installing a new file system, separated from the Windows NTFS C:\ drive on your machine. 在 Linux 中,磁片磁碟機未提供字母。In Linux, drives are not given letters. 系統會提供掛接點給他們。They are given mount points. /在 WSL 的情況下,檔案系統的根目錄是根磁碟分割或資料夾的掛接點。The root of your file system / is the mount point of your root partition, or folder, in the case of WSL. 並非所有 / 的專案都是相同的磁片磁碟機。Not everything under / is the same drive. 例如,在我的膝上型電腦上,我安裝了兩個版本的 Ubuntu (20.04 和 18.04) ,以及 Debian。For example, on my laptop, I've installed two version of Ubuntu (20.04 and 18.04), as well as Debian. 如果我開啟這些散發套件,請選取具有命令的根目錄, cd ~ 然後輸入命令 explorer.exe . ,Windows 檔案總管將會開啟,並顯示該散發的目錄路徑。If I open those distributions, select the root directory with the command cd ~, and then enter the command explorer.exe ., Windows File Explorer will open and show me the directory path for that distribution.

Linux 發行版本Linux distro 存取主資料夾的 Windows 路徑Windows Path to access home folder
Ubuntu 20.04Ubuntu 20.04 \\wsl$\Ubuntu-20.04\home\username
Ubuntu 18.04Ubuntu 18.04 \\wsl$\Ubuntu-18.04\home\username
DebianDebian \\wsl$\Debian\home\username
Windows PowerShellWindows PowerShell C:\Users\username

提示

如果您想要從 WSL 散發命令列存取 Windows 檔案目錄,而不是使用來存取 C:\Users\username 目錄,因為 Linux 發行版本會將您的 /mnt/c/Users/username windows 檔案系統視為載入的磁片磁碟機。If you are seeking to access the Windows file directory from your WSL distribution command line, instead of C:\Users\username, the directory would be accessed using /mnt/c/Users/username, because the Linux distribution views your Windows file system as a mounted drive.

您將需要在您想要搭配使用的每個檔案系統上安裝 Git。You will need to install Git on each file system that you intend to use it with.

依發行版本顯示 Git 版本

安裝 GitInstalling Git

Git 已隨大部分的 Windows 子系統 Linux 版散發套件一起安裝,不過您可能會想要更新至最新版本。Git comes already installed with most of the Windows Subsystem for Linux distributions, however, you may want to update to the latest version. 您也必須設定您的 git 設定檔。You also will need to set up your git config file.

若要安裝 Git,請參閱 適用于 Linux 的 Git 下載 網站。To install Git, see the Git Download for Linux site. 每個 Linux 發行版本都有自己的套件管理員和安裝命令。Each Linux distribution has their own package manager and install command.

針對 Ubuntu/Debian 中最新穩定的 Git 版本,請輸入下列命令:For the latest stable Git version in Ubuntu/Debian, enter the command:

sudo apt-get install git

注意

您也可能想要 安裝適用于 Windows 的 Git ( 如果尚未安裝)。You also may want to install Git for Windows if you haven't already.

Git 設定檔設定Git config file setup

若要設定您的 Git 設定檔,請開啟您正在使用之散發套件的命令列,並使用下列命令來設定您的名稱, (使用您的 Git 使用者名稱) 來取代「您的名稱」:To set up your Git config file, open a command line for the distribution you're working in and set your name with this command (replacing "Your Name" with your Git username):

git config --global user.name "Your Name"

使用這個命令設定您的電子郵件, (youremail@domain.com 以您在 Git 帳戶) 上使用的電子郵件來取代 "":Set your email with this command (replacing "youremail@domain.com" with the email you use on your Git account):

git config --global user.email "youremail@domain.com"

提示

如果您還沒有 Git 帳戶,您可以在 GitHub 上註冊一個 (英文)。If you don't yet have a Git account, you can sign-up for one on GitHub. 如果您之前從未使用過 Git,GitHub 指南可以協助您開始使用。If you've never worked with Git before, GitHub Guides can help you get started. 如果您需要編輯自己的 Git 設定,可以使用內建的文字編輯器,例如 nano:nano ~/.gitconfigIf you need to edit your git config, you can do so with a built-in text editor like nano: nano ~/.gitconfig.

我們建議您 使用雙因素驗證來保護您的帳戶 (2FA) We recommend that you secure your account with two-factor authentication (2FA).

Git 認證管理員設定Git Credential Manager setup

Git 認證管理員可讓您驗證遠端 Git 伺服器,即使您有複雜的驗證模式(例如雙因素驗證)、Azure Active Directory,或使用需要 SSH 金鑰密碼來進行每個 Git 推送的 SSH 遠端 Url。Git Credential Manager enables you to authenticate a remote Git server, even if you have a complex authentication pattern like two-factor authentication, Azure Active Directory, or using SSH remote URLs that require an SSH key password for every git push. Git 認證管理員會整合到類似 GitHub 等服務的驗證流程中,一旦您向主機服務提供者進行驗證之後,就會要求新的驗證權杖。Git Credential Manager integrates into the authentication flow for services like GitHub and, once you're authenticated to your hosting provider, requests a new authentication token. 然後,它會在 Windows 認證管理員中安全地儲存權杖。It then stores the token securely in the Windows Credential Manager. 第一次設定完之後,就可以使用 git 與您的主機服務提供者對話,而不需要重新驗證。After the first time, you can use git to talk to your hosting provider without needing to re-authenticate. 其只會在 Windows 認證管理員中存取權杖。It will just access the token in the Windows Credential Manager.

若要設定 Git 認證管理員以與 WSL 散發套件搭配使用,請開啟您的散發套件並輸入下列命令:To set up Git Credential Manager for use with a WSL distribution, open your distribution and enter this command:

git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"

現在,您在 WSL 散發套件內執行的任何 git 作業都會使用認證管理員。Now any git operation you perform within your WSL distribution will use the credential manager. 如果您已為主機快取了認證,則其會從認證管理員來存取認證。If you already have credentials cached for a host, it will access them from the credential manager. 如果沒有,則即使在 Linux 主控台中,也會收到對話回應要求您提供認證。If not, you'll receive a dialog response requesting your credentials, even if you're in a Linux console.

注意

如果您使用 GPG 金鑰進行程式碼簽署安全性,您可能需要 將 GPG 金鑰與您的 GitHub 電子郵件建立關聯If you are using a GPG key for code signing security, you may need to associate your GPG key with your GitHub email.

新增 Git 略過檔案Adding a Git Ignore file

建議您將 .gitignore 檔案新增至您的專案。We recommend adding a .gitignore file to your projects. GitHub 提供 一組有用的 .gitignore 範本 ,其中包含建議的 .gitignore 檔案。根據您的使用案例組織的檔案。GitHub offers a collection of useful .gitignore templates with recommended .gitignore file setups organized according to your use-case. 例如,以下是 Node.js 專案的 GitHub 預設 .gitignore 範本For example, here is GitHub's default gitignore template for a Node.js project.

如果您選擇 使用 GitHub 網站來建立新的存放庫,則有一些核取方塊可用來初始化具有讀我檔案的存放庫,.gitignore 檔案會針對您的特定專案類型進行設定,以及視需要新增授權的選項。If you choose to create a new repo using the GitHub website, there are check boxes available to initialize your repo with a README file, .gitignore file set up for your specific project type, and options to add a license if you need one.

Git 和 VS CodeGit and VS Code

Visual Studio Code 隨附 Git 的內建支援,包括 [原始檔控制] 索引標籤,其中會顯示您的變更,並為您處理各種 Git 命令。Visual Studio Code comes with built-in support for Git, including a source control tab that will show your changes and handle a variety of git commands for you. 深入瞭解 VS Code 的 Git 支援Learn more about VS Code's Git support.

Git 行尾結束符號Git line endings

如果您在 Windows、WSL 或容器之間使用相同的存放庫資料夾,請務必設定一致的行尾結束符號。If you are working with the same repository folder between Windows, WSL, or a container, be sure to set up consistent line endings.

由於 Windows 和 Linux 使用不同的預設行尾結束符號,因此 Git 可能會報告大量修改過的檔案,這些檔案的行尾結束符號不會有任何差異。Since Windows and Linux use different default line endings, Git may report a large number of modified files that have no differences aside from their line endings. 若要避免發生這種情況,您可以使用檔案或在 .gitattributes Windows 端全域停用行尾轉換。To prevent this from happening, you can disable line ending conversion using a .gitattributes file or globally on the Windows side. 請參閱這 VS Code 檔, 瞭解如何解決 Git 行結束問題See this VS Code doc about resolving Git line ending issues.

其他資源Additional resources