# update-main-branch > Update the master or main branch to the latest version by fetching remote changes and fast-forwarding. Use when you need to synchronize the main branch with remote updates, typically before creating a new feature branch or merging changes. - Author: saiki - Repository: sasasaiki/claude_features - Version: 20260128164533 - Stars: 0 - Forks: 0 - Last Updated: 2026-02-06 - Source: https://github.com/sasasaiki/claude_features - Web: https://mule.run/skillshub/@@sasasaiki/claude_features~update-main-branch:20260128164533 --- --- name: update-main-branch description: Update the master or main branch to the latest version by fetching remote changes and fast-forwarding. Use when you need to synchronize the main branch with remote updates, typically before creating a new feature branch or merging changes. allowed-tools: Bash, Read, Glob --- # Update Main Branch A skill to safely fetch and update the master or main branch to the latest remote version. ## Instructions When asked to update the main branch, follow these steps: 1. **Determine the current branch**: First, check which branch is currently checked out 2. **Identify the main branch name**: Check if the repository uses 'main' or 'master' 3. **Stash any uncommitted changes** (if necessary): If there are uncommitted changes, stash them to avoid conflicts 4. **Switch to main/master**: Checkout the main or master branch 5. **Fetch remote updates**: Run `git fetch origin` to get the latest remote changes 6. **Fast-forward the branch**: Use `git pull origin --ff-only` to update with a fast-forward merge 7. **Return to previous branch**: If you switched branches, return to the original branch 8. **Restore stashed changes**: If you stashed changes, restore them 9. **Report status**: Let the user know if the update was successful ## Step-by-step process ### Identify the current branch and main branch name ```bash # Get current branch CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) # Check if main branch exists (prefers 'main' over 'master') if git rev-parse --verify origin/main >/dev/null 2>&1; then MAIN_BRANCH="main" elif git rev-parse --verify origin/master >/dev/null 2>&1; then MAIN_BRANCH="master" else echo "Error: Neither 'main' nor 'master' branch found" exit 1 fi ``` ### Check for uncommitted changes ```bash # Check if there are uncommitted changes if [[ -n $(git status -s) ]]; then git stash push -m "Auto-stash for main branch update" STASHED=true else STASHED=false fi ``` ### Fetch and update ```bash # Fetch latest changes git fetch origin # Switch to main/master if not already there if [ "$CURRENT_BRANCH" != "$MAIN_BRANCH" ]; then git checkout "$MAIN_BRANCH" fi # Pull with fast-forward only (safer) git pull origin "$MAIN_BRANCH" --ff-only ``` ### Return to previous branch ```bash # Return to original branch if we switched if [ "$CURRENT_BRANCH" != "$MAIN_BRANCH" ]; then git checkout "$CURRENT_BRANCH" fi # Restore stashed changes if any if [ "$STASHED" = true ]; then git stash pop fi ``` ## Examples **Example 1**: Updating main branch before creating a feature branch - Current branch: develop - Request: "Update main to the latest version" - Result: main is fast-forwarded to latest, you return to develop **Example 2**: Ensuring main is current before a merge - Current branch: feature/MAD-3735-deploy-notice - Request: "Make sure main is up to date" - Result: main is updated, you return to your feature branch **Example 3**: User on main branch with uncommitted changes - Current branch: main - Request: "Update main branch" - Result: Changes are stashed, main is updated, changes are restored ## Best practices - This skill uses `--ff-only` to ensure a safe fast-forward merge only - Always preserves your current working branch by returning to it after the update - Will stash uncommitted changes if needed and restore them after - Works with both `master` and `main` branch naming conventions - If the main branch has diverged from remote (cannot fast-forward), the skill will fail safely ## Error handling If the fast-forward fails (branch has diverged), inform the user that: - The local branch has diverged from the remote - They may need to manually resolve conflicts or force update - They should review the changes before proceeding