Automating Script - ADD, COMMIT, PUSH - for Multiple Repos - GitHub & GitLab

Automating Script - ADD, COMMIT, PUSH - for Multiple Repos - GitHub & GitLab

Setting up a GitHub Repo

Git global setup -
git config --global user.name "Esther White"
git config --global user.email "<youremail>"

*please replace "Esther White" with your name.

Quick setup — if you’ve done this kind of thing before

…or create a new repository on the command line

echo "# <project-name>" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/<username>/<repo>
git push -u origin main

…or push an existing repository from the command line

git remote add origin https://github.com/<username>/<repo>
git branch -M main
git push -u origin main

…or import code from another repository

Setting up a GitLab Repo

Command line instructions

You can also upload existing files from your computer using the instructions below.

Git local setup -
if you already have GitHub set as global, omit the --global tag when adding GitLab and vice versa.
git config user.name "Esther White"
git config user.email "<youremail>"

*please replace "Esther White" with your name.

Create a new repository
git clone https://gitlab.com/<username>/<repo>
cd appointmentscheduler-c969
git switch --create main
touch README.md
git add README.md
git commit -m "add README"
git push --set-upstream origin main
Push an existing folder
cd existing_folder
git init --initial-branch=main
git remote add origin https://gitlab.com/<username>/<repo>
git add .
git commit -m "Initial commit"
git push --set-upstream origin main
Push an existing Git repository
cd existing_repo
git remote rename origin old-origin
git remote add origin https://gitlab.com/<username>/<repo>
git push --set-upstream origin --all
git push --set-upstream origin --tags

Renaming origin to gitlab for clarification

For the script to work there has to be some difference between the two origin repos that you are pushing changes to. In my case, since GitLab was added after GitHub which is my primary version control, I did the following:

git remote rename origin gitlab

see more below.

Creating a Script to add ., commit and push to both repos

In the root directory of your project, create a file named something like

git-push-all.sh

The contents of the file should be something like this:

#!/bin/bash

# Add all changes
git add .

# Commit changes
git commit -m "Update: $(date)"

# Push to GitHub
git push origin main

# Push to GitLab
git push gitlab main

This is the simpler version of the script, assuming that you want to link the same directory with two repos - one on GitHub and one on GitLab.

However, there are different cases - in my case, I had a main directory with many projects which was already connected to a repo on GitHub and what I wanted to do was to connect only one of the sub-directories - only a specific project, to a GitLab repo.

So in a case like this the git-push-all.sh can be modified like this:

#!/bin/bash

# Add all changes
git add .

# Prompt for a commit message
read -p "Enter a commit message: " COMMIT_MESSAGE

# Commit changes
git commit -m "$COMMIT_MESSAGE"

# Push to GitHub
git push origin main

# Navigate to GitLab subdirectory
cd "path/to/gitlab/subdirectory"

# Add changes in GitLab subdirectory
git add .

# Commit changes in GitLab subdirectory
git commit -m "$COMMIT_MESSAGE"

# Push to GitLab
git push gitlab main

This way one script can handle both repos from the same place - the root directory. This version also allows adding a custom message that will be applied to both commits, and you type it without the quotes :)

Making the script executable

The final step is to make the script executable, so in a bash terminal run the following line of code:

chmod +x ./git-push-all.sh

Now you should be able to run ./git-push-all.sh in your terminal, in Visual Studio Code for example, you will get to type your message and you will have the new commits showing in both of your repos.

Happy Coding :)