Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ImaginativeShohag/322a4979b35b958f1a6afa1e9a58ff36 to your computer and use it in GitHub Desktop.
Save ImaginativeShohag/322a4979b35b958f1a6afa1e9a58ff36 to your computer and use it in GitHub Desktop.
This script will push source code to two repositories at a time to maintain the same commit history.
#!/bin/bash
# ================================================================================
# This script will push source code to two repositories at a time to maintain the
# same commit history.
# Version: 1.0
#
# Coded by:
# Md. Mahmudul Hasan Shohag
# imaginativeshohag@gmail.com
# github.com/ImaginativeShohag
#
# The script is licensed under the MIT License.
# ================================================================================
# ================================================================================
# Copyright (c) 2019 Md. Mahmudul Hasan Shohag
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ================================================================================
# ================================================================================
# HOW TO USE?
#
# 0. Both repositories MUST be empty, or the script will not work correctly.
#
# 1. Change the following variables value:
# - git_repo_one : Current working repository ssh or http url
# - git_repo_two : Backup repository ssh or http url
# - default_commit_msg : Default commit message
#
# 2. Make this script executable:
# $ chmod +x this-script-name.sh
#
# 3. Finally run the script:
# $ ./this-script-name.sh
# ================================================================================
# ================================================================================
# Repositories
#
# git_repo_one -> is the working repository
# git_repo_two -> is the backup/other repository
# ================================================================================
git_repo_one="git@github.com:ImaginativeShohag/Test-Repo-One.git"
git_repo_two="git@github.com:ImaginativeShohag/Test-Repo-Two.git"
# ================================================================================
# Default commit message
#
# default_commit_msg -> is the default commit message
# ================================================================================
default_commit_msg="Commit at $(date)"
# ================================================================================
# Other variables
# ================================================================================
git_folder=".git"
bottom_new_line="\n"
# ================================================================================
# Colors
# ================================================================================
# Reset
Color_Off='\033[0m' # Text Reset
# Regular Colors
Black='\033[0;30m' # Black
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
Yellow='\033[0;33m' # Yellow
Blue='\033[0;34m' # Blue
Purple='\033[0;35m' # Purple
Cyan='\033[0;36m' # Cyan
White='\033[0;37m' # White
# Bold
BBlack='\033[1;30m' # Black
BRed='\033[1;31m' # Red
BGreen='\033[1;32m' # Green
BYellow='\033[1;33m' # Yellow
BBlue='\033[1;34m' # Blue
BPurple='\033[1;35m' # Purple
BCyan='\033[1;36m' # Cyan
BWhite='\033[1;37m' # White
# Background
On_Black='\033[40m' # Black
On_Red='\033[41m' # Red
On_Green='\033[42m' # Green
On_Yellow='\033[43m' # Yellow
On_Blue='\033[44m' # Blue
On_Purple='\033[45m' # Purple
On_Cyan='\033[46m' # Cyan
On_White='\033[47m' # White
function show_msg_unfortunately_stopped() {
echo -e "${Red}================================
Error occurred!
The script stopped!
================================${Color_Off}$bottom_new_line"
}
function show_msg_type_info() {
echo -e "${BBlue}INFO: ${Blue}$1${Color_Off}$bottom_new_line"
}
function show_msg_type_success() {
echo -e "${Green}$1${Color_Off}$bottom_new_line"
}
function show_msg_type_error() {
echo -e "${BRed}ERROR: ${Red}$1${Color_Off}$bottom_new_line"
}
# ================================================================================
# Print the repos
# ================================================================================
echo -e "${Blue}================================================================================${Color_Off}"
echo -e "${On_Black}${BWhite} REPOSITORIES ${Color_Off}"
echo -e "${BBlue}General Repo:${Color_Off} ${Purple}${git_repo_one}${Color_Off}"
echo -e "${BBlue}Backup Repo:${Color_Off} ${Purple}${git_repo_two}${Color_Off}"
echo -e "${Blue}================================================================================${Color_Off}"
echo ""
# ================================================================================
# Check if git initialized?
# ================================================================================
if ! [[ -e ${git_folder} ]]; then
show_msg_type_error "[Git] is not initialized yet!"
show_msg_type_info "Initializing git..."
git init
git remote add origin ${git_repo_one}
fi
# ================================================================================
# > git remote -v
# --------------------------------------------------------------------------------
# origin git@github.com:ImaginativeShohag/Test-Repo-One.git (fetch)
# origin git@github.com:ImaginativeShohag/Test-Repo-One.git (push)
# --------------------------------------------------------------------------------
#
#
# - check current git remote is $git_repo_one? (git remote -v)
# - if not:
# - show message and stop script!
# ================================================================================
if ! git remote -v | grep -q ${git_repo_one}; then
show_msg_type_error "[$git_repo_one] is not current remote repo of this project! :("
show_msg_unfortunately_stopped
exit
else
show_msg_type_info "Remote repo [$git_repo_one] found in the project."
fi
# ================================================================================
# - > git add .
# - > git commit -m "commit at $date"
# - > git push -u origin master
# ================================================================================
echo -e "${BGreen}Please enter a commit message: (Default: $default_commit_msg)${Color_Off}"
read input_commit_msg
if ! [[ -z ${input_commit_msg} ]]; then
default_commit_msg=${input_commit_msg}
fi
echo ""
show_msg_type_info "Source code pushing to the first repository ($git_repo_one) with commit message: '$default_commit_msg'"
git add .
git commit -m "$default_commit_msg"
git push -u origin master
echo ""
# ================================================================================
# Change origin to the second one
# ================================================================================
show_msg_type_info "Changing the origin to [$git_repo_two]."
git remote set-url origin $git_repo_two
git remote -v
echo ""
# ================================================================================
# Push code again
# ================================================================================
show_msg_type_info "Source code pushing to the second repository ($git_repo_two) with commit message: '$default_commit_msg'"
git push -u origin master
echo ""
# ================================================================================
# Change origin to the first one
# ================================================================================
show_msg_type_info "Changing the origin to [$git_repo_one]."
git remote set-url origin $git_repo_one
git remote -v
echo ""
# ================================================================================
# Success message
# ================================================================================
show_msg_type_success "The script successfully executed! :)"
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment