Last active
October 25, 2018 11:00
-
-
Save mjj2000/7924902 to your computer and use it in GitHub Desktop.
Backup git working status
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
#!/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