Skip to content

Instantly share code, notes, and snippets.

@seanbecker15
Created April 22, 2025 14:42
Show Gist options
  • Save seanbecker15/c119885a4cc41dbecab35765ee26a623 to your computer and use it in GitHub Desktop.
Save seanbecker15/c119885a4cc41dbecab35765ee26a623 to your computer and use it in GitHub Desktop.
Revert changes in specified directory
#!/bin/bash
# Usage: ./revert-dir.sh <target-branch> <relative-directory> [--dry-run]
# Example: ./revert-dir.sh main src/components --dry-run
set -e
TARGET_BRANCH=$1
TARGET_DIR=$2
DRY_RUN=$3
if [ -z "$TARGET_BRANCH" ] || [ -z "$TARGET_DIR" ]; then
echo "Usage: $0 <target-branch> <relative-directory> [--dry-run]"
exit 1
fi
# Get the merge base between current HEAD and target branch
BASE_COMMIT=$(git merge-base HEAD "$TARGET_BRANCH")
if [ "$DRY_RUN" == "--dry-run" ]; then
echo "[Dry Run] Would revert $TARGET_DIR to its state from common ancestor with $TARGET_BRANCH ($BASE_COMMIT)."
echo "Files that would change:"
git diff --name-only "$BASE_COMMIT" -- "$TARGET_DIR"
else
git checkout "$BASE_COMMIT" -- "$TARGET_DIR"
echo "Reverted $TARGET_DIR to its state from common ancestor with $TARGET_BRANCH ($BASE_COMMIT)."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment