Skip to content

Instantly share code, notes, and snippets.

@LC43
Created May 16, 2024 09:41
Show Gist options
  • Save LC43/e4bb358c50ab435491d3c6a7ce05ae5d to your computer and use it in GitHub Desktop.
Save LC43/e4bb358c50ab435491d3c6a7ce05ae5d to your computer and use it in GitHub Desktop.
.bash git function
#!/bin/bash
function reorient_usage() {
e=$(printf "\e")
RESET="$e[0m"
RED="$e[1;37;41m"
BOLD="$e[1m"
UNBOLD="$e[21m"
RED="$e[1;37;41m"
YELLOW="$e[1;38;05;232;48;05;220m"
GREEN="$e[1;37;42m"
BLUE="$e[96m"
PROGNAME="reorient"
cat << EOF
${BLUE}Usage: $PROGNAME [-h] [-u|-r] [-s] -t"${RESET}
Example: $PROGNAME [-u|-r <remote>] [-s <source_branch>] -t <target_branch>
Reset secondary branch ( -t : target) to the main branch ( -s : source) on a specific remote ( -u or -r : remote name).
It will reset HEAD of the target branch to be the same as source branch.
${YELLOW} !! NOTE !! Use with caution as it forces and rewrites the target branch with --hard!!${RESET}
Options:
-u | -r:
(optional). The remote repo name.
Defaults to origin
-s :
(optional). The source branch name. The branch that has the HEAD.
Defaults to master
-t :
${RED}(required)${RESET}. The target branch name. The one that gets reset.
-h :
Show this help
EOF
}
function gireorient {
RESET="\e[0m"
BLUE="\e[96m"
local OPTIND # Must be local to prevent other scripts
# set initial values
local vremote=origin
local vsrc=master
local vtarget=''
local vtarget_flag=false
# extract options and their arguments into variables.
# : after an option means it requires a parameter.
# example: -t hotfix
while getopts "u:r:s:t:h" opt ; do
case "$opt" in
r | u )
vremote="$OPTARG"
;;
s )
vsrc="$OPTARG"
;;
t )
vtarget_flag=true
vtarget="$OPTARG"
;;
h )
# echo the help file
reorient_usage
return 1
;;
\? )
reorient_usage
return 1
;;
: )
reorient_usage
return 1
;;
*)
echo "Internal error!"
reorient_usage
return 1
;;
esac
done
if ! $vtarget_flag
then
echo "-t must be included" >&2
reorient_usage
return 1
fi
# Print the variables
echo -e "${BLUE}Remote${RESET} = $vremote"
echo -e "${BLUE}Source${RESET} = $vsrc"
echo -e "${BLUE}Target${RESET} = $vtarget"
git push "${vremote}" +"${vsrc}:${vtarget}" && git reset --hard "${vremote}"/"${vtarget}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment