在适用于 Linux 的 Windows 子系统上开始使用 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.

可以在 Windows 和 WSL 上安装 Git。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 使用访问该目录, /mnt/c/Users/username 因为 Linux 发行版会将 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 已安装了大多数适用于 Linux 分发的 Windows 子系统,但你可能想要更新到最新版本。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

备注

如果尚未 安装 Git For Windows ,则还可能需要安装。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 ~/.gitconfig)来执行此操作。If 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 Ignore 文件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. 有关解决 Git 行结束问题的 VS Code,请参阅此文档。See this VS Code doc about resolving Git line ending issues.

其他资源Additional resources