Skip to content

Instantly share code, notes, and snippets.

@koteitan
Last active September 25, 2025 14:16
Show Gist options
  • Select an option

  • Save koteitan/2415972b45346ecc36486bc211379f36 to your computer and use it in GitHub Desktop.

Select an option

Save koteitan/2415972b45346ecc36486bc211379f36 to your computer and use it in GitHub Desktop.
add, commit and push the submodule's update
#!/bin/bash
# Find the root of the git repository
git_root=$(git rev-parse --show-toplevel 2>/dev/null)
if [ $? -ne 0 ]; then
echo "Error: Not in a git repository"
exit 1
fi
# Check if we're in a submodule
git_super_root=$(git rev-parse --show-superproject-working-tree 2>/dev/null)
if [ -n "$git_super_root" ]; then
# We're in a submodule, so we need to find the actual submodule root
# Save current directory to understand depth
current_dir=$(pwd)
# Find the actual submodule root by going up until we find .git
submodule_root="$git_root"
while [ "$submodule_root" != "/" ]; do
if [ -f "$submodule_root/.git" ] || [ -d "$submodule_root/.git" ]; then
break
fi
submodule_root=$(dirname "$submodule_root")
done
# Now switch to the parent repository
git_root="$git_super_root"
cd "$git_root"
# Find the path to this submodule in the parent by checking all submodules
submodule_path=""
while IFS= read -r path; do
# Get the absolute path of this submodule
abs_path=$(cd "$path" 2>/dev/null && pwd)
if [ "$abs_path" = "$submodule_root" ]; then
submodule_path="$path"
break
fi
done < <(git config --file .gitmodules --get-regexp path | awk '{print $2}')
if [ -z "$submodule_path" ]; then
echo "Error: Could not find submodule path in parent repository"
exit 1
fi
else
# We're in the parent repository
cd "$git_root"
# Find submodule directory
submodule_path=$(git config --file .gitmodules --get-regexp path | awk '{print $2}' | head -1)
if [ -z "$submodule_path" ]; then
echo "Error: No submodule found in this repository"
exit 1
fi
fi
# Check if there are any other staged changes besides the submodule
other_staged=$(git diff --cached --name-only | grep -v "^${submodule_path}$")
if [ -n "$other_staged" ]; then
echo "Error: There are other staged changes. Please commit or unstage them
first."
exit 1
fi
# Check if remote is GitHub and pull first if needed
remote_url=$(git remote get-url origin 2>/dev/null)
owner=""
repo=""
if [[ "$remote_url" =~ ^git@github\.com:([^/]+)/([^/]+)(\.git)?$ ]]; then
owner="${BASH_REMATCH[1]}"
repo="${BASH_REMATCH[2]%%.git}"
elif [[ "$remote_url" =~ github\.com[:/]([^/]+)/([^/]+)(\.git)?$ ]]; then
owner="${BASH_REMATCH[1]}"
repo="${BASH_REMATCH[2]%%.git}"
fi
if [ -n "$owner" ] && [ -n "$repo" ]; then
echo "GitHub repository detected: $owner/$repo"
# Ask about pull
echo "pull github pages? (any/ctrl+c)"
read -n 1 -s
echo
# Pull with fast-forward only
echo "Pulling latest changes..."
if git pull --ff-only; then
echo "Pull successful"
else
echo "Warning: Could not fast-forward pull. You may need to resolve conflicts."
exit 1
fi
fi
# Stage the submodule changes
git add "$submodule_path"
# Check if there are any changes to deploy
if ! git diff --cached --quiet; then
# Show the submodule changes
echo "================================="
git diff --cached --submodule
echo "================================="
# Ask for confirmation
echo -n "Commit it to github pages? (any/ctrl+c): "
read -n 1 -s
echo
# Commit with the diff as commit message
git diff --cached --submodule | git commit -F -
echo "Commit complete."
if [ -n "$owner" ] && [ -n "$repo" ]; then
# Ask about push
echo "push github pages? (any/ctrl+c)"
read -n 1 -s
echo
# Push to remote
echo "Pushing to GitHub..."
if git push; then
echo "Push successful"
# Open GitHub Actions page in default browser
actions_url="https://github.com/$owner/$repo/actions"
echo "Opening GitHub Actions: $actions_url"
# Detect OS and open browser accordingly
if command -v xdg-open > /dev/null; then
xdg-open "$actions_url"
elif command -v open > /dev/null; then
open "$actions_url"
elif command -v start > /dev/null; then
start "$actions_url"
else
echo "Could not detect browser command. Please open manually: $actions_url"
fi
else
echo "Error: Push failed"
exit 1
fi
else
echo "Remote is not a GitHub repository."
fi
else
echo "No submodule changes to deploy."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment