Skip to content

Instantly share code, notes, and snippets.

@mjj2000
Last active October 25, 2018 11:00
Show Gist options
  • Save mjj2000/7924902 to your computer and use it in GitHub Desktop.
Save mjj2000/7924902 to your computer and use it in GitHub Desktop.
Backup git working status
#!/bin/bash
### Backup git working status
# Backup modified and untracked files without ignored ones in any git working directory. The destination is required and will be created automatically if not exists.
#
# Usage:
# ./git-backup [destination path]
# Ex:
# ./git-backup ~/backup20130101
#
# Note: Mac users have to install GNU Coreutils to support `--parents` of cp command
###
if [[ ! -e ".git" ]]; then
# check of current path is corresponding to a GIT repository
echo "Not a git repository (or any of the parent directories)"
exit 1
elif [[ -z "$1" ]]; then
# target path should not be empty
echo "Usage: git-backup.sh [destination path]"
exit 2
else
# create destination if not exists
(test -d "$1" || mkdir -p "$1") &&
# list modified and untracked files without ignored ones
(git ls-files --modified --others --exclude-standard) |
# copy files to destination with original folder structure
(xargs -I {} sh -c "file=\"{}\";test -f \$file && $CP_CMD_PATH -v \$file --parents $1")
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment