Git -- Easy Save

by Mark Nielsen
Copyright July 2021


I wanted to make it easy to save changes to the remote repository on GITHUB without using git commands or changing branches. Thus, this will add all files and upload them. For delete, or rename, the changes will be reflected at the next time you run the command. All committed changes locally will synced to the remote repository. Most of the time, its just add. Thus this makes it easy.

Steps

The Script

#!/bin/bash

# Must have an stuff defined.

COPY=~/GITHUB_COPY/main
LIVE=~/GITHUB/main
TMPDIR=~/tmp

echo "Updating copy of github."

cd $COPY
git checkout master
git pull

echo "Syncing changes to GITHUB"
cd $LIVE

# checks if we using master or not defined
# aborts if we are in another
status=""
b=`git branch --show-current`
if [ $? == 0 ];
  then
	git checkout master
	status="ok";
fi
if [ $status != "ok" ];
  then
    if [ $b == "master" ];
      then
        status = "ok";
    fi
fi

if [ $status == "ok" ]; then echo "git status good"; fi

# We just want the list of file compare to COPY, filter out things we don't want. 
rsync -nra --out-format="%n"  $LIVE/ $COPY/ --size-only  --exclude=".git" > $TMPDIR/gittmp.txt
egrep -v "^./" $TMPDIR/gittmp.txt | egrep -v "~$" |egrep -v "/$"  > $TMPDIR/gitdiff.txt

# Force adding each file
# This can result in some errors if done twice, but if later committed and pushed its okay. 
echo "adding files"
while IFS= read -r line; do
    echo "adding : $line"
    git add -v "$line"
done < $TMPDIR/gitdiff.txt

# Commit and sync to remote master
# committing remotely. 
git commit -m "auto "
git push origin -u master