Skip to content

Instantly share code, notes, and snippets.

@michaelgold
Last active April 10, 2024 12:36
Show Gist options
  • Save michaelgold/edaed1ff7b9d8cd4fbb80134b90b4ec2 to your computer and use it in GitHub Desktop.
Save michaelgold/edaed1ff7b9d8cd4fbb80134b90b4ec2 to your computer and use it in GitHub Desktop.
Shallow checkout / branch switching for Unreal
#!/bin/bash
# Script to fetch and checkout a shallow clone of a specific branch
# Check if a branch name was provided
if [ $# -eq 0 ]; then
echo "Usage: $0 <branch-name>"
exit 1
fi
# Store the provided branch name in a variable
BRANCH_NAME=$1
# Verify if the branch exists on the remote
if git ls-remote --heads origin $BRANCH_NAME | grep -q $BRANCH_NAME; then
echo "Branch $BRANCH_NAME found on remote. Proceeding with fetch and checkout."
else
echo "Branch $BRANCH_NAME does not exist on remote."
exit 1
fi
# Attempt to fetch the specified branch as a shallow clone
if git fetch --depth=1 origin $BRANCH_NAME; then
# Check if the branch already exists locally
if git show-ref --verify --quiet refs/heads/$BRANCH_NAME; then
# Reset the local branch to match the fetched commit
git checkout $BRANCH_NAME
git reset --hard FETCH_HEAD
else
# Create a new branch from FETCH_HEAD
git checkout -b $BRANCH_NAME FETCH_HEAD
fi
else
echo "Failed to fetch the branch: $BRANCH_NAME as a shallow clone."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment