Last active
September 7, 2023 20:22
-
-
Save jonathanduke/92eac20fe8a85eff8e8840b5d56551f9 to your computer and use it in GitHub Desktop.
List all Git worktrees recursively with the location of the repository (either a .git directory or the gitdir location specified in the .git file)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:::: | |
:: Original source: https://gist.github.com/jonathanduke/92eac20fe8a85eff8e8840b5d56551f9#file-git-list-worktrees-bat | |
:: Public domain: http://unlicense.org/ | |
:: Modify as you wish, but you are encouraged to leave this comment for future reference in case the original is updated. | |
:::: | |
@echo off | |
setlocal EnableDelayedExpansion | |
for /f "tokens=*" %%f in ('dir /a /s /b .git') do ( | |
if exist "%%f\*" ( | |
set "_dir=%%~dpf" | |
set "_tgt=%%f" | |
echo D !_dir:~0,-1! !_tgt:\=/! | |
) else ( | |
set "_dir=%%~dpf" | |
set /p _tgt=<"%%f" | |
echo F !_dir:~0,-1! !_tgt:gitdir: =! | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If a folder contains a .git directory, the line will start with a D and the third column will contain its full path. However, if a folder contains a .git file, the line will start with an F and the third column will contain the path listed in the file (which could be relative to the worktree folder).
For example:
In this case, the main worktree at C:\Code\Personal\Example1 would contain a standard .git folder, but the linked worktree at C:\Code\Business\Example2 would instead contain a .git file like this:
If the main worktree is detached and linked from a different folder, then the repository should be configured in D:/.repos/github.com/myworkplace/example2.git/config to point back to the worktree:
Note that it is possible for a single repository to have multiple worktrees: