Okay, to mass git pull or update folders in the first level of your current directory using PowerShell on Windows, you can use a foreach loop. Here's the command:
Get-ChildItem -Directory | ForEach-Object {
if (Test-Path -Path "$($_.FullName)\.git") {
Write-Host "Pulling changes in $($_.Name)..."
Push-Location $_.FullName
git pull
Pop-Location
} else {
Write-Host "$($_.Name) is not a Git repository. Skipping." -ForegroundColor Yellow
}
}Explanation of the Command:
-
Get-ChildItem -Directory:Get-ChildItem(aliasgciorlsin PowerShell) lists items in a location.-Directoryfilters the results to only include directories (folders), ignoring files. This ensures you only attempt togit pullwithin actual directories.
-
|(Pipeline Operator):- This sends the output of
Get-ChildItem(the list of directory objects) as input to the next command.
- This sends the output of
-
ForEach-Object { ... }:- This cmdlet (alias
foreach) iterates through each object it receives from the pipeline. - The code inside the curly braces
{}is executed for each directory. $_represents the current directory object in the loop.
- This cmdlet (alias
-
if (Test-Path -Path "$($_.FullName)\.git") { ... }:Test-Pathchecks if a path exists.$($_.FullName)\.gitconstructs the full path to the.gitdirectory within the current folder.$_.FullNamegets the full path of the current directory (e.g.,C:\Users\YourUser\Projects\MyProject).\.gitappends the.gitdirectory name.
- This
ifstatement ensures thatgit pullis only attempted on folders that are actually Git repositories (indicated by the presence of a.gitdirectory).
-
Write-Host "Pulling changes in $($_.Name)...":- Displays a message indicating which folder is currently being processed.
$_.Namegets just the name of the current directory (e.g.,MyProject).
-
Push-Location $_.FullName:Push-Location(aliaspushd) changes the current directory to the specified path ($_.FullName) and also saves the previous location on a stack. This is important for being able to easily return.
-
git pull:- This is the standard Git command to fetch and merge changes from the remote repository. It will operate within the directory you just
Push-Location'd into.
- This is the standard Git command to fetch and merge changes from the remote repository. It will operate within the directory you just
-
Pop-Location:Pop-Location(aliaspopd) returns you to the directory that was previously saved on the location stack byPush-Location. This ensures that after processing each repository, you are returned to your starting directory before moving to the next.
-
else { Write-Host "$($_.Name) is not a Git repository. Skipping." -ForegroundColor Yellow }:- If the
ifcondition is false (i.e., no.gitdirectory is found), this block is executed. - It informs you that the folder is being skipped, and
-ForegroundColor Yellowmakes the message stand out.
- If the
How to Use:
- Open PowerShell.
- Navigate to the parent directory containing the folders you want to pull/update. For example, if you have
C:\Projects\RepoA,C:\Projects\RepoB, andC:\Projects\NonGitFolder, you wouldcd C:\Projects. - Paste the command into the PowerShell window and press Enter.
This script will then iterate through each first-level folder, check if it's a Git repository, and if so, navigate into it, perform a git pull, and then return to the parent directory before moving to the next.