Skip to content

Instantly share code, notes, and snippets.

@akperkins
Last active August 3, 2016 23:19
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 akperkins/a4dd0afd0538d50251fc18caa5121b7d to your computer and use it in GitHub Desktop.
Save akperkins/a4dd0afd0538d50251fc18caa5121b7d to your computer and use it in GitHub Desktop.
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