Brand Shaw
Brand Shaw

Reputation: 261

How to manage more than one git repository in VS Code

I have to work with a directory like this:

ProjectRoot
|---- SubDirectory1
|---- SubDirectory2(Git Repository1)
|---- SubDirectory3(Git Repository2)
|---- ...
|---- Files

But I found that the embedded git function of VS-code does not automatically recognize those sub-directories managed by git.

How to solve this problem? Is there a extension about it?

Upvotes: 26

Views: 64408

Answers (6)

parsley72
parsley72

Reputation: 9047

You can use multi-root workspaces. Rather than opening the root directory of your project you can open the first separate git repo then add others using File->Add Folder to Workspace. Once you've added them all you can do File->Save Workspace As to save your new workspace configuration.

Upvotes: 25

parsley72
parsley72

Reputation: 9047

Version 1.72 (September 2022) has this in the release notes:

Source Control

Discover nested Git repositories

To address a long standing feature request, this milestone we made changes to repository discovery to add support for nested Git repositories. Depending on the location of the nested repositories, you may need to modify the git.repositoryScanMaxDepth setting (the default is 1 level).

Upvotes: 9

Matt
Matt

Reputation: 316

As of version 1.20 of VS Code (Jan 2018), VS Code has had the setting git.autoRepositoryDetection, which allows VS Code automatically detect git repositories. As of this response (2022), the valid settings are:

  1. true
  2. false
  3. "subFolders"
  4. "openEditors"

The default is true, and it will look for git repos in the folder above and in subfolders. The "subFolders" setting limits the search to subfolders only. And, the "openEditors" setting will limit the search to repos open in any open VS Code editor.

With these options in mind, I would reccomend adding an entry to your root workspace's settings.json file. This can be found in <workspace_root>/.vscode/settings.json, where you will be able to add "git.autoRepositoryDetection": "subFolders". You can also do this using the "Workspace" set of settings, or for all "User" settings of your VS Code through the settings editor found from "File -> Preferences -> Settings" and searching for the "git.autoRepositoryDetection" setting and then choosing whether you want to set it for all of your VS Code workspaces or just the workspace you're working, via the "User" and "Workspace" tabs at the top. (see picture) Setting git.autoRepositoryDetection for the Workspace

Upvotes: 15

Jan
Jan

Reputation: 16094

I had a similar problem:

- root project (git repo)
  |- subproject 1 (git repo)
  |- subproject 2 (git repo)

I wanna handle all three repos in VSCode.

Open subproject 1 regularly then "add subfolder to workspace" select subproject 2 then "add subfolder to workspace" select root project

voilà: you can manage all three repos in one Workspace now

Upvotes: 3

Harri Kaimio
Harri Kaimio

Reputation: 31

As already said by wgj, VScode assumes that GIT repo is in same directory level as workspace root. Although there is no way to change this currently, one potentially helpful workaround is to use the project manager extension which makes it easy to open VScode windows to different git repos and to switch between them with a few keystrokes. It is also part of the Git extension pack by Don Jayamanne.

Upvotes: 3

wgj
wgj

Reputation: 20164

Assuming that ProjectRoot is a not also a git repo (in which you should follow the issue from @hjpotter92's comment, and use the normal git command for now)...

VS Code is expecting your git repo (the directory with the .git folder) to be your "ProjectRoot" instead. This kind of implies that directories like "SubDirectory1" and "SubDirectory2", while in the same parent, aren't actually part of the same project, and won't be actively worked on together.

Upvotes: 2

Related Questions