Symbolic links (symlinks) are useful for pointing to files or directories using relative paths. Here's a step-by-step guide to creating symlinks with relative paths in Windows.
Suppose you have the following directory structure:
C:\Repo
│
├── FolderA
│ └── FileToLink.txt
└── FolderB
You want to create a symlink in FolderB
that points to FileToLink.txt
in FolderA
using a relative path.
Open PowerShell and navigate to the directory where you want to create the symlink. In this case, navigate to FolderB
:
cd C:\Repo\FolderB
Use the cmd
tool's mklink
command, which handles relative paths reliably:
-
For a file:
cmd /c mklink LinkedFile.txt ..\FolderA\FileToLink.txt
-
For a directory:
cmd /c mklink /D LinkedFolder ..\FolderA
cmd /c
: Runs acmd
command from PowerShell./D
: Specifies a directory symlink (omit for files)...\
: Moves up one directory level.
Check if the symlink was created successfully:
dir
You should see the symlink with an arrow (->
) pointing to the target.
Once the symlink is created, add it to your Git repository:
git add LinkedFile.txt
git commit -m "Added symlink with relative path"
git push
-
Administrator Privileges:
- Run PowerShell as an administrator to create symlinks unless Developer Mode is enabled.
-
GitHub Behavior:
- Git stores symlinks as text files containing the target path. When cloning the repository, ensure the relative structure is preserved for the symlink to work correctly.
-
Developer Mode:
- On Windows 10/11, enabling Developer Mode allows non-administrator users to create symlinks. Enable it via:
- Settings → Update & Security → For developers → Developer mode.
- https://stackoverflow.com/questions/5917249/git-symbolic-links-in-windows/52097145#52097145
- On Windows 10/11, enabling Developer Mode allows non-administrator users to create symlinks. Enable it via:
By following these steps, you can reliably create symlinks with relative paths in a Windows environment.