Skip to content

Instantly share code, notes, and snippets.

@akperkins

akperkins/remoteSnapshot

Last active Aug 3, 2016
Embed
What would you like to do?
a bash script that will push a snapshot of your current repo to a remote repository
#!/bin/bash
#This script performs pushes to a remote repo but does not contain the history of the branch
#that it was pushed from. This is useful in the case of consulting work when you want to share
#the current state of a repository but want to obscure the history of the work.
#
#Author:Andre Perkins <akperkins1@gmail.com>
#Description:This script pushs the current ref that the chosen local without history (without a reference to it's parent) to the abinbev repo.
#Options:
#b-local branch that should be used to push from. If not supplied, the default branch of "dev" is used
#d-the directory in which the git repo is located. If not supplied, the current PWD will be used
#r-name of the remote that we are pushing to.
optstring=b:d:r:
local_branch=dev
directory=$PWD
#processes arguments
while getopts $optstring opt; do
case $opt in
r)
remote=$OPTARG
[[ -z remote ]] && exit 2
;;
b)
local_branch=$OPTARG
;;
d)
directory=$OPTARG
[[ -d $directory ]] || printf "invalid directory passed in via arguemnts %s\n" $directory
;;
*)
printf "invalid options passed. script terminating\n"
exit 1
;;
esac
done;
#creates an orphan branch (a branch that points to a commit with no parent)
#from the current state of the repo and force pushes it to the remote branch master
cd $directory
printf -v commit_message "repo snapshot for %s @ %s" $(date +%Y-%m-%d) $(date +%H:%M:%S)
git checkout $local_branch
printf -v orphan_branch "weeklyUpdate%s-%d" $(date +%Y-%m-%d) $RANDOM
git checkout --orphan $orphan_branch
git commit -a -m "$commit_message"
git push $remote +$orphan_branch:master
git checkout $local_branch
git branch -D $orphan_branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment