WebStorm 2021.2 Help

Set up a Git repository设置Git存储库

When you clone an existing Git repository, or put an existing project under Git version control, WebStorm automatically detects if Git is installed on your computer. 当您克隆现有Git存储库或将现有项目置于Git版本控制之下时,WebStorm会自动检测您的计算机上是否安装了Git。If the IDE can't locate a Git executable, it suggests downloading it.如果IDE找不到Git可执行文件,建议下载它。

WebStorm supports Git from the Windows Subsystem for Linux 2 (WSL2), which is available in Windows 10 version 2004.WebStorm支持Linux 2的Windows子系统(WSL2)中的Git,该子系统在Windows 10版本2004中提供。

If Git is not installed on Windows, WebStorm searches for Git in WSL and uses it from there. 如果Windows上没有安装Git,WebStorm将在WSL中搜索Git并从那里使用它。Also, WebStorm automatically switches to Git from WSL for projects that are opened when you use the \\wsl$ path.此外,对于使用\\wsl$路径打开的项目,WebStorm会自动从WSL切换到Git。

If for some reason you need to manually configure WebStorm to use Git from WSL, go to the Version Control | Git page of the IDE settings Ctrl+Alt+S, click the Browse icon in the Path to Git executable field and select Git from WSL via the \wsl$ path, for example, \\wsl$\debian\usr\bin\git.如果出于某种原因需要手动配置WebStorm以从WSL使用Git,请转到IDE设置Ctrl+Alt+S“版本控制”|“Git”页面,单击“指向Git可执行文件的路径”字段中的浏览图标,并通过\wsl$路径选择来自WSL的Git,例如,\\WSL$\debian\usr\bin\Git

Check out a project from a remote host (clone)从远程主机(克隆)签出项目

WebStorm allows you to check out (in Git terms clone) an existing repository and create a new project based on the data you've downloaded.WebStorm允许您签出(用Git术语来说是克隆)现有存储库,并根据下载的数据创建新项目。

  1. From the main menu, select Git | Clone, or, if no project is currently opened, click Get from VCS on the Welcome screen.从主菜单中选择“Git”|“克隆”,或者,如果当前没有打开任何项目,请单击欢迎屏幕上的Get From VCS

  2. In the Get from Version Control dialog, specify the URL of the remote repository you want to clone, or select one of the VCS hosting services on the left在“从版本控制获取”对话框中,指定要克隆的远程存储库的URL,或选择左侧的一个VCS托管服务.

    If you are already logged in to the selected hosting service, completion will suggest the list of available repositories that you can clone.如果您已经登录到所选的托管服务,则完成操作将提示您可以克隆的可用存储库列表。

  3. Click Clone. 单击"克隆"。If you want to create a project based on the sources you have cloned, click Yes in the confirmation dialog. 如果要基于已克隆的源创建项目,请在确认对话框中单击“”。Git root mapping will be automatically set to the project root directory.Git根映射将自动设置为项目根目录。

    If your project contains submodules, they will also be cloned and automatically registered as project roots.如果您的项目包含子模块,它们也将被克隆并自动注册为项目根。

Put an existing project under Git version control将现有项目置于Git版本控制之下

You can create a local Git repository based on an existing project sources.您可以基于现有项目源创建本地Git存储库。

Associate the entire project with a single Git repository将整个项目与单个Git存储库关联

  1. Open the project that you want to put under Git.打开要放在Git下的项目。

  2. Choose Enable Version Control Integration from the VCS Operations Popup Alt+` or from the main VCS menu.VCS操作弹出窗口Alt+`VCS主菜单中选择启用版本控制集成

  3. Choose Git as the version control system and click OK.选择Git作为版本控制系统,然后单击“确定”。

  4. After VCS integration is enabled, WebStorm will ask you whether you want to share project settings files via VCS. 启用VCS集成后,WebStorm将询问您是否希望通过VCS共享项目设置文件。You can choose Always Add to synchronize project settings with other repository users who work with WebStorm.您可以选择“始终添加”以与使用WebStorm的其他存储库用户同步项目设置。

    Notification prompting to select how to treat configuration files

Associate different directories within the project with different Git repositories将项目中的不同目录与不同的Git存储库相关联

  1. Open the project that you want to put under Git.打开要放在Git下的项目。

  2. From the main menu, choose VCS | Create Git Repository.从主菜单中,选择“VCS”|“创建Git存储库”

  3. In the dialog that opens, specify the directory where a new Git repository will be created.在打开的对话框中,指定将在其中创建新Git存储库的目录。

    Git does not support external paths, so if you choose a directory that is outside your project root, make sure that the folder where the repository is going to be created also contains the project root.Git不支持外部路径,因此如果选择项目根目录之外的目录,请确保要创建存储库的文件夹也包含项目根目录。

  4. If you are creating multiple Git repositories inside the project structure, repeat the previous steps for each directory.如果要在项目结构中创建多个Git存储库,请对每个目录重复前面的步骤。

After you have initialized a Git repository for your project, you need to add project files to the repository.为项目初始化Git存储库后,需要将项目文件添加到存储库中。

Add files to the local repository将文件添加到本地存储库

  1. In the Commit tool window Alt+0, expand the Unversioned Files node.在“提交”工具窗口Alt+0中,展开“无版本文件”节点。

  2. Select the files you want to add to Git or the entire changelist and press Ctrl+Alt+A or choose Add to VCS from the context menu.选择你想要添加到Git的文件,或整个更改列表,并按下Ctrl+Alt+A或从上下文菜单中选择“添加到VCS”

    You can also add files to your local Git repository from the Project tool window: select the files you want to add, and press Ctrl+Alt+A or choose Git | Add from the context menu.您还可以从项目工具窗口向本地Git存储库添加文件:选择要添加的文件,然后按Ctrl+Alt+A或从关联菜单中选择“Git”|“添加”

When Git integration is enabled in your project, WebStorm suggests adding each newly created file under Git, even if it was added from outside WebStorm. 当项目中启用Git集成时,WebStorm建议将每个新创建的文件添加到Git下,即使它是从WebStorm外部添加的。You can change this behavior in the Version Control | Confirmation page of the IDE settings Ctrl+Alt+S. 您可以在IDE设置Ctrl+Alt+S“版本控制”|“确认”页面中更改此行为。If you want certain files to always remain unversioned, you can ignore them.如果希望某些文件始终保持不变,可以忽略它们

Exclude files from version control (ignore)从版本控制中排除文件(忽略)

Sometimes you may need to leave certain files unversioned. 有时,您可能需要保持某些文件未设置版本。These can be VCS administration files, artifacts of utilities, backup copies, and so on. 这些文件可以是VCS管理文件、实用程序工件、备份副本等。You can ignore files through WebStorm, and the IDE will not suggest adding them to Git and will highlight them as ignored.您可以通过WebStorm忽略文件,IDE不会建议将它们添加到Git中,而是将它们突出显示为已忽略。

You can only ignore unversioned files, that is files that you see in the Unversioned Files changelist. 您只能忽略未版本化文件,即您在“未版本化文件”更改列表中看到的文件。If a file is added to Git but not committed, you can right-click it in the Local Changes view and choose Rollback.如果文件已添加到Git未提交,则可以在“本地更改”视图中右键单击该文件并选择“回滚”。

Git lets you list ignored file patterns in two kinds of configuration files:Git允许您在两种配置文件中列出被忽略的文件模式:

  • .git/info/exclude file. 文件。
    Patterns listed in this file only apply to the local copy of the repository.此文件中列出的模式仅适用于存储库的本地副本。

    This file is created automatically when you initialize or check out a Git repository.当您初始化或签出Git存储库时,会自动创建此文件。

  • One or more .gitignore files in the VCS root directory and its subdirectories. VCS根目录及其子目录中的一个或多个.gitignore文件。
    These files are checked into the repository so that the ignore patterns in them are available to the entire team. 这些文件将签入存储库,以便整个团队都可以使用其中的忽略模式。Therefore, it is a most common place to store the ignored file patterns.因此,它是存储被忽略的文件模式的最常见的地方。

    If there is no .gitignore file in the VCS root directory, you can right-click anywhere in the Project window, choose New | File and type .gitignore in the New File dialog.如果VCS根目录中没有.gitignore文件,则可以右键单击项目窗口中的任意位置,选择“新建”|“文件”并在“新建文件”对话框中键入.gitignore

Add files to .gitignore or .git/info/exclude将文件添加到.gitignore或.git/info/exclude

  1. Decide what kind of Git configuration file you are going to use to ignore files. 决定要使用哪种Git配置文件来忽略文件。If in doubt, use .gitignore.如果有疑问,请使用.gitignore

  2. Locate the unversioned file or folder you want to ignore in the Local Changes view or in Project tool window. 在“本地更改”视图或“项目工具”窗口中找到要忽略的未版本化文件或文件夹。File colors in these views help you identify the status of the file.这些视图中的文件颜色有助于识别文件的状态。

  3. Right click the selection and choose Git | Add to .gitignore or Git | Add to .git/info/exclude. 右键单击所选内容并选择“Git”|“添加到.gitignore”“Git”|“添加到.Git/info/exclude”
    File colors in these views help you identify the status of the file.这些视图中的文件颜色有助于识别文件的状态。

If you need to exclude files by a certain pattern, or files of a certain type, you can edit the .gitignore or .git/info/exclude file directly. 如果需要按特定模式排除文件,或者需要排除特定类型的文件,可以直接编辑.gitignore.git/info/exclude文件。See .gitignore patterns format请参阅.gitignore模式格式

Check project status检查项目状态

WebStorm allows you to check the status of your local working copy compared to the repository version of the project. WebStorm允许您检查本地工作副本的状态,并与项目的存储库版本进行比较。It uses specific colors to let you see which files have been modified, which new files have been added to the VCS, and which files are not being tracked by Git.它使用特定的颜色让您看到哪些文件已被修改,哪些新文件已添加到VCS,哪些文件未被Git跟踪。

Open the Local Changes view.打开“本地更改”视图。

Files with various statuses in the Version Control tool window
  • The Default changelist shows all files that have been modified since you last synchronized with the remote repository (highlighted in blue), and all new files that have been added to the VCS but have not been committed yet (highlighted in green).默认更改列表显示自上次与远程存储库同步以来已修改的所有文件(以蓝色突出显示),以及已添加到VCS但尚未提交的所有新文件(以绿色突出显示)。

  • The Unversioned Files changelist shows all files that have been added to your project, but that are not being tracked by Git.未版本文件”更改列表显示已添加到项目中但未被Git跟踪的所有文件。

For more info on changelists, see Group changes into different changelists.有关变更列表的更多信息,请参阅将变更分组到不同的变更列表中

Track changes to a file in the editor在编辑器中跟踪对文件的更改

You can also track changes to a file as you modify it in the editor. 在编辑器中修改文件时,还可以跟踪对文件的更改。All changes are highlighted with change markers that appear in the gutter next to the modified lines, and show the type of changes introduced since you last synchronized with the repository. 所有更改都将使用更改标记高亮显示,这些标记显示在修改行旁边的左侧列中,并显示自上次与存储库同步以来引入的更改类型。When you commit changes to the repository, change markers disappear.将更改提交到存储库时,更改标记将消失。

The changes you introduce to the text are color-coded:您对文本所做的更改以颜色编码:

  • marker for a newly added line line added.

  • marker for a modified line line changed.

  • marker for a deleted line line deleted.

You can manage changes using a toolbar that appears when you hover the mouse cursor over a change marker and then click it. 您可以使用将鼠标光标悬停在更改标记上,然后单击该标记时显示的工具栏来管理更改。The toolbar is displayed together with a frame showing the previous contents of the modified line:工具栏将与显示修改行以前内容的框架一起显示:

the modified line marker

You can roll back changes by clicking the Revert icon and explore the differences between the current and the repository version of the current line by clicking the Show diff icon.您可以通过单击the Revert icon回滚更改,并通过单击the Show diff icon查看当前行的当前版本和存储库版本之间的差异。

Instead of reverting the whole file, you can copy any part of the contents of this popup and paste it into the editor.您可以复制此弹出窗口的任何部分内容并将其粘贴到编辑器中,而不是还原整个文件。

Add a remote repository添加远程存储库

If you created a Git repository based on local sources, you need to add a remote repository to be able to collaborate on your Git project, as well as to eliminate the risks of storing all of your codebase locally. 如果基于本地源创建了Git存储库,则需要添加远程存储库,以便能够在Git项目上进行协作,并消除在本地存储所有代码库的风险。You push changes to a remote repository when you need to share your work and pull data from it to integrate changes made by other contributors into your local repository version.当您需要共享您的工作时,您可以将更改推送到远程存储库,还可以从远程存储库中拉取数据,以便将其他贡献者所做的更改集成到本地存储库版本中。

If you have cloned a remote Git repository, for example from GitHub, the remote is configured automatically and you do not have to specify it when you want to sync with it. 如果您克隆了远程Git存储库(例如从GitHub),则会自动配置远程存储库,并且您不必在希望与它同步时指定它。The default name Git gives to the remote you've cloned from is origin.Git给您克隆的远程设备的默认名称是origin

Define a remote定义远程

  1. Create an empty repository on any Git hosting, such as Bitbucket or GitHub. 在任何Git主机(如BitbucketGitHub)上创建空存储库。You can create a repository on GitHub without leaving WebStorm: see Share a project on GitHub.您可以在GitHub上创建存储库,而无需离开WebStorm:请参阅在GitHub上共享项目

  2. Invoke the Push dialog when you are ready to push your commits by selecting Git | Push from the main menu, or press Ctrl+Shift+K.当您准备好从主菜单中选择“Git”|“推送”或按Ctrl+Shift+K来推送提交时,调用推送对话框。

  3. In the dialog that opens, specify the remote name and the URL where it will be hosted, and click OK.在打开的对话框中,指定远程名称及其将承载的URL,然后单击“确定”。

Add a second remote添加第二个遥控器

In some cases, you also need to add a second remote repository. 在某些情况下,还需要添加第二个远程存储库。This may be useful, for example, if you have cloned a repository that you do not have write access to, and you are going to push changes to your own fork of the original project. 例如,如果您克隆了一个没有写入权限的存储库,并且要将更改推送到原始项目您自己的分支中,这可能会很有用。Another common scenario is that you have cloned your own repository that is somebody else's project fork, and you need to synchronize with the original project and fetch changes from it.另一种常见的情况是,您已经克隆了自己的存储库,该存储库是其他人的项目分支,您需要与原始项目同步并从中获取更改。

  1. From the main menu, choose Git | Manage Remotes. 从主菜单中,选择“Git”|“管理远程”The Git Remotes dialog will open.Git远程”对话框将打开。

  2. Click the Add add button on the toolbar or press Alt+Insert.单击工具栏上的“添加”add按钮或按Alt+Insert

  3. In the dialog that opens, specify the remote name and URL and click OK.在打开的对话框中,指定远程名称和URL,然后单击“确定”。

Learn more from this video:从本视频了解更多信息:

Last modified: 20 May 2021