Skip to content

Instantly share code, notes, and snippets.

@xunker
Last active March 12, 2025 17:57
Show Gist options
  • Save xunker/266e4fb6905a993f84b21759c9a6519a to your computer and use it in GitHub Desktop.
Save xunker/266e4fb6905a993f84b21759c9a6519a to your computer and use it in GitHub Desktop.
Shell script to auomate resetting a local git branch to be the same as the remote branch
# This script will reset your current local git branch to be the same as the remote branch with the same name.
# Usage:
# ./reset_branch_from_origin.sh
#
# Remember to make the script executable first: `chmod +x reset_branch_from_origin.sh`
#
# Credit for the bash conformation prompt: https://stackoverflow.com/a/1885534
# Update local branches from origin
git fetch origin
# get current branch name
CURRENT_BRANCH=$(git branch | grep --color \* | awk '{print $2}')
echo "I'm going to reset the local branch \"${CURRENT_BRANCH}\" to match \"origin/${CURRENT_BRANCH}\"."
echo "ALL LOCAL CHANGES WILL BE LOST!"
read -p "Are you sure you want me to do this? " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
GIT_CMD="git reset --hard origin/${CURRENT_BRANCH}"
echo "executing: ${GIT_CMD}"
$GIT_CMD
else
echo "Cancelled."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment