Skip to content

Instantly share code, notes, and snippets.

@Explosion-Scratch
Last active March 16, 2023 14:14
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 Explosion-Scratch/9c160893e46afe20a2cdc407143fda16 to your computer and use it in GitHub Desktop.
Save Explosion-Scratch/9c160893e46afe20a2cdc407143fda16 to your computer and use it in GitHub Desktop.
Backup a folder to a GitHub URL without deleting history on the repo or messing with merge conflicts → Keep local changes
#!/bin/bash
stop(){
echo "Stopping"
exit 1
}
cont(){
echo "Waiting 10 seconds"
sleep 10
}
gum confirm "Continue?" && cont || stop
# Move all files to ORIGINAL then clone to CLONED
echo "Cloning repo"
zip -r ../backup.zip ./
git config core.filemode false
mkdir ORIGINAL && ls -A | grep -v ORIGINAL | xargs -I {} mv {} ORIGINAL && mkdir CLONED && cd CLONED && git clone --filter=blob:none --no-checkout --depth 1 --sparse $(gum input --prompt="What's the URL of your repo? ") ./ && cd ..
echo "Moving files"
ls -A ORIGINAL | grep -v .git | xargs -I {} mv ORIGINAL/{} CLONED && cd CLONED
echo "Commiting files"
git add . --sparse && git commit -m "$(gum input --width 50 --placeholder "Commit message" --value="Backup $(date)" --prompt.foreground="#0ff")" -m "$(gum write --width 80 --placeholder "Details of changes (CTRL+D to finish)")"
echo "Pushing..."
gum confirm "Push to upstream?" && git push -u origin main || echo "Didn't push"
echo "Cleaning up..."
cd .. && ls -A CLONED | xargs -I {} mv CLONED/{} ./
gum confirm "Remove .git folder?" && rm -rf .git || echo "Done!"
gum confirm "Move original files to trash?" && sudo rm ORIGINAL ~/.
gum confirm "Move cloned files to trash?" && mv CLONED ~/.Trash
@Explosion-Scratch
Copy link
Author

Requires gum

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