Effortless Git Automation: Create and Upload Repositories to GitHub Using Bash Scripts
For Ubuntu (Linux) and Mac Os X
Introduction:
In today's fast-paced development environment, automation is essential for streamlining workflows and improving productivity. As developers and SEO professionals, managing multiple GitHub repositories can be time-consuming. In this blog post, we'll guide you through the process of automating the creation and uploading of Git repositories to GitHub using Bash scripts on both Ubuntu and macOS systems. With this automation, you can effortlessly manage your repositories and focus on more important tasks.
1. Prerequisites
Before we begin, ensure you have the following:
An Ubuntu or macOS system
A GitHub account
Basic understanding of Git and GitHub
2. Installation and Configuration:
To automate the repository creation and uploading process, we'll need Git and the GitHub CLI (gh) installed on your system.
2.1. Installing Git:
Ubuntu: Install Git using the following command:
sudo apt-get install git
macOS: Install Git using Homebrew (brew.sh) or download it from the official website (git-scm.com/download/mac).
2.2. Installing GitHub CLI:
Ubuntu: Follow the instructions here: github.com/cli/cli/blob/trunk/docs/install_..
macOS: Follow the instructions here: github.com/cli/cli/blob/trunk/docs/install_..
2.3. Authenticating with GitHub CLI:
After installing the GitHub CLI, authenticate with your GitHub account using the following command:
gh auth login
3. The Automation Script:
Create a Bash script called create_and_upload_repos.sh
with the following content:
#!/bin/bash
projects_directory="/path/to/your/project/templates"
cd "$projects_directory"
for project in *; do
if [ -d "$project" ]; then
echo "Processing $project..."
cd "$project"
# Create a .gitignore file if it does not exist and add node_modules to it
if [ ! -f ".gitignore" ]; then
echo "node_modules/" > .gitignore
elif ! grep -q "node_modules/" ".gitignore"; then
echo "node_modules/" >> .gitignore
fi
# Add common entries to .gitignore
entries=("node_modules/" ".DS_Store" ".env" "build/" "dist/")
for entry in "${entries[@]}"; do
if ! grep -q "^${entry}$" ".gitignore"; then
echo "$entry" >> .gitignore
fi
done
# Initialize a new Git repository
git init
# Stage and commit all files
git add .
git commit -m "Initial commit"
# Create a new GitHub repository
gh repo create "$project" --private -y
# Push the project to the newly created GitHub repository
git push --set-upstream origin master
cd ..
fi
done
Replace /path/to/your/project/templates
with the actual path to the folder containing your project templates.
4. Using the Script on Ubuntu and macOS:
Follow these steps to use the script on both Ubuntu and macOS systems:
4.1 Make the script executable:
chmod +x create_and_upload_repos.sh
4.2 Run the script:
./create_and_upload_repos.sh
5. Conclusion
The script will go through each folder in the specified directory, create a .gitignore
file (if needed), initialize a new Git repository, commit