Created
April 2, 2021 06:46
-
-
Save samirkape/75d4586fba8ea6902914afed3195ee11 to your computer and use it in GitHub Desktop.
Create a backup of changed git files for some reason
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Running a gback command
Creates a backup of
git diff
result for referenceWhere?
/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