Skip to content

Instantly share code, notes, and snippets.

@virtualstaticvoid
Created February 9, 2021 13:37
Show Gist options
  • Save virtualstaticvoid/27b2cf98ba6698ef57395d862a91ba36 to your computer and use it in GitHub Desktop.
Save virtualstaticvoid/27b2cf98ba6698ef57395d862a91ba36 to your computer and use it in GitHub Desktop.
Resets a git branch, or switches over from master to main.
#!/bin/bash
set -e
#
# NOTE: can be used to switch over to another branch such from "master" to "main"
#
# git reset-branch main origin/main
#
if [ ! -d .git ]; then
echo "Error: must run this script from the root of a git repository"
exit 1
fi
# get the current branch name
branch=${1:-$(git rev-parse --abbrev-ref HEAD)}
# get tracking branch
tracking=${2:-$(git rev-parse --abbrev-ref --symbolic-full-name @{u} || echo "")}
if [[ -n "$tracking" ]]; then
printf "\033[1;31mResetting $branch to $tracking branch.\033[0m\n"
git reset --hard
git checkout -b tmp
git branch -D $branch
git checkout -b $branch $tracking
git branch -D tmp
else
printf "\033[1;31mERROR: Unable to reset branch. No tracking branch to use.\033[0m\n"
fi
# clean up
git tidy &> /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment