Skip to content

Instantly share code, notes, and snippets.

@chrishiestand
Last active August 29, 2015 14:16
Show Gist options
  • Save chrishiestand/cac214ce82c47a90e8d3 to your computer and use it in GitHub Desktop.
Save chrishiestand/cac214ce82c47a90e8d3 to your computer and use it in GitHub Desktop.
Convert any directory into a git-based directory preserving original, non-intersecting, files
#!/bin/bash
# To backup a directory, and then replace it with a git version
# that is already available remotely
# Conflicting file versions will be overwritten with the remote version
# Files which existed locally but not in the git repo will be preserved (*not* removed)
set -e
PATH=/sbin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin:/usr/local/bin:$PATH
dest="$1"
origin="$2"
branch="$3"
if [ -z "$dest" -o -z "$origin" -o -z "$branch" ]; then
echo "usage: $0 destination-path origin branch"
echo "example: $0 /path/to/destination git@git.server.example.com:my-repo.git master"
exit 99
fi
if [ ! -d "$dest" ]; then
echo "$dest does not exist or is not a directory"
exit 98
fi
if [ -d "$dest/.git" ]; then
echo "$dest is already a git directory. aborting"
exit 97
fi
rsync -av "$dest/" "$dest.non-git"
cd "$dest"
git init
git checkout -b "$branch"
git remote add origin "$origin"
git fetch
git reset --hard "origin/$branch"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment