Skip to content

Instantly share code, notes, and snippets.

@opencoca
Last active November 3, 2023 19:35
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 opencoca/17df2c165ea9c3dd541eda0600735117 to your computer and use it in GitHub Desktop.
Save opencoca/17df2c165ea9c3dd541eda0600735117 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Highlighting and emojis
YELLOW='\033[1;33m'
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
WARNING_EMOJI=$'\xE2\x9A\xA0'
CHECKMARK_EMOJI=$'\xE2\x9C\x85'
# Warning message
echo -e "${RED}${WARNING_EMOJI} WARNING: This script can be destructive. ${NC}"
echo -e "${YELLOW}It is designed to reset your local repository to match the production branch exactly. ${NC}"
echo -e "${YELLOW}Any local changes that are not committed will be lost. ${NC}"
read -p "Are you sure you want to proceed? [y/N] " -n 1 -r
echo # Move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # Handle running sourced or not
fi
echo -e "${GREEN}${CHECKMARK_EMOJI} Proceeding with the operation... ${NC}"
# Step 1: Fetch the latest changes from the remote repository
echo -e "${YELLOW}Fetching the latest changes from the remote... ${NC}"
git fetch origin
echo -e "${GREEN}${CHECKMARK_EMOJI} Fetch complete. ${NC}"
# Step 2: Checkout the production branch (assuming it's main)
echo -e "${YELLOW}Checking out the production branch (main)... ${NC}"
git checkout main
echo -e "${GREEN}${CHECKMARK_EMOJI} Checkout complete. ${NC}"
# Step 3: Pull the latest changes from the remote production branch
echo -e "${YELLOW}Pulling the latest changes from the remote production branch... ${NC}"
git pull origin main
echo -e "${GREEN}${CHECKMARK_EMOJI} Pull complete. ${NC}"
# Offer to proceed with hard reset and cleaning
echo -e "${YELLOW}Would you like to continue and reset your branch to exactly match the remote? ${NC}"
echo -e "${YELLOW}This will REMOVE all local uncommitted changes and untracked files/directories. ${NC}"
read -p "Are you sure you want to continue with this destructive action? [y/N] " -n 1 -r
echo # Move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
# Step 4: Hard reset to the remote production branch
echo -e "${YELLOW}Resetting local branch to match remote... ${NC}"
git reset --hard origin/main
echo -e "${GREEN}${CHECKMARK_EMOJI} Reset complete. ${NC}"
# Step 5: Clean out all untracked files and directories
echo -e "${YELLOW}Cleaning all untracked files and directories... ${NC}"
git clean -fdx
echo -e "${GREEN}${CHECKMARK_EMOJI} Clean complete. ${NC}"
else
echo -e "${YELLOW}Skipping reset and clean. Your local branch has been updated but no local changes have been removed. ${NC}"
fi
echo -e "${GREEN}${CHECKMARK_EMOJI} Operation completed. ${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment