Skip to content

Instantly share code, notes, and snippets.

@a7madgamal
Last active March 29, 2018 14:45
Show Gist options
  • Save a7madgamal/8315217 to your computer and use it in GitHub Desktop.
Save a7madgamal/8315217 to your computer and use it in GitHub Desktop.
shell script to commit local git repo,push current branch and pull on remote repo
#!/bin/bash
# usage:
# . /path/to/git_push_pull.sh "/local/git/repo/path" "remote/git/repo/path" "remoteuser@remotehost" "OPTIONAL:remote/source/folder" "OPTIONAL:remote/destination/location"
# it's better to create a separate script and name it project_name.sh for each project with this code:
#. /path/to/git_push_pull.sh "/local/git/repo/path" "remote/git/repo/path" "remoteuser@remotehost" "OPTIONAL:remote/source/folder" "OPTIONAL:remote/destination/location"
#the optional parameters are useful if you need to copy a folder from the repo (after pulling) to another location and not the whole git folder
reset;
local_folder="$1"
remote_folder="$2"
ssh_details="$3"
remote_source_folder="$4"
remote_dest_folder="$5"
echo -e "local folder \t [$local_folder]"
echo -e "remote folder \t [$remote_folder]"
echo -e "ssh details \t [$ssh_details]"
echo -e "remote copy s \t [$remote_source_folder]"
echo -e "remote copy d \t [$remote_dest_folder]"
cd $local_folder
pwd_test=`pwd`
if [ "$pwd_test" == "$local_folder" ]; then
current_branch=`git rev-parse --abbrev-ref HEAD`
echo -e "current branch \t [$current_branch]"
current_remote=`git remote`
echo -e "current remote \t [$current_remote]"
status_output=`git status --porcelain`
echo "$status_output"
if [ -n "$status_output" ]; then
echo "Enter Message to commit"
read commit_message
if [ -n "$commit_message" ]; then
git add .
commit_output=`git commit -am "$commit_message"`
echo $commit_output
else
echo "empty commit message,Aporting!"
exit 1
fi
else
echo "nothing to commit!"
fi
read -p "push [$current_branch] to remote [$current_remote] branch? [y][n]" do_push
if [ "$do_push" == "y" ]; then
git push $current_remote $current_branch;
if [ $? = 0 ]; then
backtc="\`";
echo "starting ssh commands..."
ssh $ssh_details "cd \"$remote_folder\";
echo \"whoami: $backtc whoami $backtc\"
pwd;
echo \"git pull $current_remote $current_branch\";
git pull $current_remote $current_branch;
if [ -d \"$remote_dest_folder\" ]; then
echo \"dest folder found $remote_dest_folder,removing\";
echo \"removing result: $backtc rm -rv '$remote_dest_folder' $backtc\";
else
echo \"source folder NOT found [$remote_source_folder]\";
fi;
if [ -d \"$remote_source_folder\" ]; then
echo \"starting remote copy...\";
cp -rv '$remote_source_folder' '$remote_dest_folder';
echo \"remote copy done\";
else
echo \"source folder NOT found [$remote_source_folder]\";
fi;";
if [ $? = 0 ]; then
echo "all done!!"
exit 0
else
echo "couldnt connect to ssh,Aporting!"
exit 1
fi
else
echo "couldnt push,Aporting!"
exit 1
fi
else
echo "bye!"
exit 1
fi
else
echo "current working directory [$pwd_test] doesnt match local folder[$local_folder],Aporting!"
exit 1
fi
@architeacher
Copy link

Enta ragel 100%

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