Skip to content

Instantly share code, notes, and snippets.

@myinusa
Created July 17, 2025 21:09
Show Gist options
  • Select an option

  • Save myinusa/904cadcbf7fa1fe99f98d7cad7f9a876 to your computer and use it in GitHub Desktop.

Select an option

Save myinusa/904cadcbf7fa1fe99f98d7cad7f9a876 to your computer and use it in GitHub Desktop.
PowerShell Git Pull Script

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:

  1. Get-ChildItem -Directory:

    • Get-ChildItem (alias gci or ls in PowerShell) lists items in a location.
    • -Directory filters the results to only include directories (folders), ignoring files. This ensures you only attempt to git pull within actual directories.
  2. | (Pipeline Operator):

    • This sends the output of Get-ChildItem (the list of directory objects) as input to the next command.
  3. 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.
  4. if (Test-Path -Path "$($_.FullName)\.git") { ... }:

    • Test-Path checks if a path exists.
    • $($_.FullName)\.git constructs the full path to the .git directory within the current folder.
      • $_.FullName gets the full path of the current directory (e.g., C:\Users\YourUser\Projects\MyProject).
      • \.git appends the .git directory name.
    • This if statement ensures that git pull is only attempted on folders that are actually Git repositories (indicated by the presence of a .git directory).
  5. Write-Host "Pulling changes in $($_.Name)...":

    • Displays a message indicating which folder is currently being processed.
    • $_.Name gets just the name of the current directory (e.g., MyProject).
  6. Push-Location $_.FullName:

    • Push-Location (alias pushd) 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.
  7. 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.
  8. Pop-Location:

    • Pop-Location (alias popd) returns you to the directory that was previously saved on the location stack by Push-Location. This ensures that after processing each repository, you are returned to your starting directory before moving to the next.
  9. else { Write-Host "$($_.Name) is not a Git repository. Skipping." -ForegroundColor Yellow }:

    • If the if condition is false (i.e., no .git directory is found), this block is executed.
    • It informs you that the folder is being skipped, and -ForegroundColor Yellow makes the message stand out.

How to Use:

  1. Open PowerShell.
  2. Navigate to the parent directory containing the folders you want to pull/update. For example, if you have C:\Projects\RepoA, C:\Projects\RepoB, and C:\Projects\NonGitFolder, you would cd C:\Projects.
  3. 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.

@myinusa
Copy link
Author

myinusa commented Sep 22, 2025

git fetch --all --prune; git branch -r | ForEach-Object { ($_ -replace 'origin/') } | ForEach-Object { git branch --track $_ origin/$_ -f }; git for-each-ref --format='%(refname:short)' refs/heads | ForEach-Object { git pull origin $_ }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment