Pull All Git Repositories with a Simple Bash Script
Introduction
Managing multiple Git repositories can be a daunting task, especially when it comes to updating them all. Repetitive commands can lead to frustration and wasted time. Fortunately, with a simple Bash script, you can effortlessly pull updates from all your repositories at once. In this post, I'll share how to set up and use this script.
The Bash Script
Here's a straightforward script that allows you to pull all Git repositories within a given directory or the current directory if no path is specified.
#!/bin/bash
# Use the provided argument or default to the current directory
DIRECTORY=${1:-$(pwd)}
# Find .git directories and perform the operations
find "$DIRECTORY" -type d -name '.git' -execdir sh -c 'git --git-dir="{}" remote get-url origin && git --git-dir="{}" pull' \;
Explanation of the Script
Directory Argument: The script accepts a directory path as an argument. If no argument is provided, it defaults to the current working directory using
pwd
.Finding Git Repositories:
- It uses the
find
command to search for all.git
directories within the specified path.
- It uses the
Pulling Changes:
- For each found repository, the script retrieves the origin URL and then performs a
git pull
to update it.
- For each found repository, the script retrieves the origin URL and then performs a
How to Use the Script
Create the Script File:
- Save the provided script into a file, for example,
git_pull.sh
.
- Save the provided script into a file, for example,
Make the Script Executable:
chmod +x git_pull.sh
Run the Script:
Without a Path: If you run the script without any arguments, it will pull updates from repositories in the current directory:
./git_pull.sh
With a Specific Path: You can also specify a directory path:
bash ./git_pull.sh /path/to/your/directory
Conclusion
By using this simple Bash script, you can efficiently manage and update multiple Git repositories without manually running pull commands for each one. This is a small but powerful addition to your coding toolkit, particularly for developers and teams working with numerous projects.
Feel free to customize the script to fit your needs, and happy coding!