Last active
November 3, 2023 19:35
-
-
Save opencoca/17df2c165ea9c3dd541eda0600735117 to your computer and use it in GitHub Desktop.
curl -sL https://gist.githubusercontent.com/opencoca/17df2c165ea9c3dd541eda0600735117/raw/c983d0aa3cf414901bedf5759ee429687645b45b/git_cleaner.sh > /tmp/git_cleaner.sh && bash /tmp/git_cleaner.sh; rm /tmp/git_cleaner.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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