Skip to content

Instantly share code, notes, and snippets.

@david-pm
Last active January 25, 2019 02:41
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 david-pm/9618a0c60f57ae33cb263b91d743d900 to your computer and use it in GitHub Desktop.
Save david-pm/9618a0c60f57ae33cb263b91d743d900 to your computer and use it in GitHub Desktop.
bash script that resets and force pushes
#!/bin/bash
# resets HEAD back one commit, recommits with provided message and force pushes to origin
commitmsg=$1
# stole function from: .oh-my-zsh/lib/git.zsh
function current_git_branch() {
local ref
ref=$(command git symbolic-ref --quiet HEAD 2> /dev/null)
local ret=$?
if [[ $ret != 0 ]]; then
[[ $ret == 128 ]] && return # no repo
ref=$(command git rev-parse --short HEAD 2> /dev/null) || return
fi
echo ${ref#refs/heads/}
}
# exit if no commitmsg
if [ -z "$commitmsg" ]
then
echo "\$commitmsg is empty"
else
echo "\$commitmsg is NOT empty"
# output cmds as they run
set -x
git reset HEAD~1
git add .
git commit -m "$commitmsg"
git push origin $(current_git_branch) -f
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment