Skip to content

Instantly share code, notes, and snippets.

@anver
Forked from mityukov/git-rsync.sh
Created April 23, 2021 20:53
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 anver/f9f9590a452e70404aebdd66e6674c8c to your computer and use it in GitHub Desktop.
Save anver/f9f9590a452e70404aebdd66e6674c8c to your computer and use it in GitHub Desktop.
Sync only files, added or modified since last commit
#!/bin/bash
## Installation:
# - make a file, called `git-rsync` in a directory, covered by your $PATH variable;
# - `chmod a+x` this file.
# - do the same for `listfiles` script
#
## Usage:
# 1. Syncing "dirties" (all the files you can see in the `git status` output)
# - before commiting, issue `git-rsync login@host:/destination/path/` command to sync all local changes to your stage server
# - if destination is not found in the arguments, the script will use `cat .stage` instead
#
# 2. Syncing already commited changes: `git-rsync [ DEST ] [ <commit-hash-1 ] <commit-hash-2>`
# Examples:
# - sync files from last commit: `git-rsync [ DEST ] HEAD`
# - sync files from selected commit: `git-rsync [ DEST ] <40-bytes-hash>`
# - sync files changed between selected commits: `git-rsync [ DEST ] <40-char-hash-from> <40-char-hash-to>`
# - sync files changed starting from selected commit and up until now: `git-rsync [ DEST ] <40-char-hash-from> HEAD`
LF_ARGS=""
DEST=`cat .stage`
for arg in $@
do
if [[ $arg =~ ^[0-9a-z]{40}$ || $arg = "HEAD" ]]
then
LF_ARGS="$LF_ARGS $arg"
else
DEST=$arg
fi
done
echo Destination: $DEST
echo
if [[ ${#LF_ARGS} -gt 0 ]]
then
echo "syncing commited changes ($LF_ARGS)"
FLIST=`listfiles $LF_ARGS`
else
echo "syncing uncomited changes (staged and unstaged)"
FLIST=`git status -s | awk '{print $2}' | grep -v "^db\/"`
fi
echo
echo "List of files to sync:"
echo "$FLIST"
echo
echo "$FLIST" | rsync -arz --files-from=- --no-dirs --whole-file --verbose . $DEST
#!/bin/zsh
## Installation:
# - make a file, called `listfiles` in a directory, covered by your $PATH variable;
# - `chmod a+x` this file.
if [ $# -eq 0 ]
then
COMMIT_FROM="HEAD~"
COMMIT_TO="HEAD"
elif [ $# -eq 1 ]
then
COMMIT_FROM="$1~"
COMMIT_TO="$1"
elif [ $# -eq 2 ]
then
COMMIT_FROM="$1~"
COMMIT_TO="$2";
else
echo 'Usage: [<first commit id> [<last commit id>]]'
echo 'Two arguments max!';
exit
fi
git diff --name-only --relative $COMMIT_FROM $COMMIT_TO | fgrep -v '/vendor/' | sort > /tmp/file_list_git
find . -type f | sed 's#^./##' | sort > /tmp/file_list_all
FLIST=`comm -12 /tmp/file_list_git /tmp/file_list_all`
echo $FLIST
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment