Skip to content

Instantly share code, notes, and snippets.

@samirkape
Created April 2, 2021 06:46
Show Gist options
  • Save samirkape/75d4586fba8ea6902914afed3195ee11 to your computer and use it in GitHub Desktop.
Save samirkape/75d4586fba8ea6902914afed3195ee11 to your computer and use it in GitHub Desktop.
Create a backup of changed git files for some reason
function gback()
{
#Setup backup dir name
name=`date +"%I:%M:%S"`
date=`date +"%d-%m-%y"`
dir="/home/"$USER"/Desktop/store/$date/$name"
mkdir -p $dir
#Store git diff result for reference
echo "Generate: git diff results.."
git diff --color . > $dir/git.diff
#Copy only changed files
echo "Copy: only changed files.."
zip -r $dir/src.zip `git diff --relative --name-only .`
#Check if copy was successful
if [ $? -ne 0 ]
then
echo " "
echo "Copy: Failed.."
echo "Copy: Aborting git reset --hard"
return
else
#Run git reset --hard
echo "Backup created at $dir"
fi
}
@samirkape
Copy link
Author

Running a gback command

Creates a backup of

  1. Changed files
  2. Colored git diff result for reference

Where?
/home/user/Desktop/.store/Date/Time/src.zip


Usage

Step 1 -
Save this file in your home directory and name it .gitback

Step 2 -
Open ~/.bashrc and add the below line at the end.
source ~/.gitback

Step 3 - run gback to create a backup

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