Sync a file from another branch via short PR (with gh)

Need to keep staging (or a release branch) aligned with a single change from main without merging everything else? This guide walks through a minimal-diff workflow using GitHub CLI: branch from the base, restore only the target file(s) from the source ref, commit, and open a PR. It’s perfect for CI workflows and configs, keeps review tidy, and respects protected branches—so you get the change you want without the surprises you don’t.

Table of Contents

When you maintain multiple long‑lived branches (e.g., main, staging, or release/*), you’ll often need to propagate one specific file—like a CI workflow, CODEOWNERS, or a config—without dragging along every other commit from the source branch. Merging or rebasing just to update a single file is risky: you can pull in unfinished code, hit merge conflicts, or run afoul of protected-branch rules. This playbook shows how to open a tiny PR that updates exactly the file(s) you choose from another branch, using GitHub CLI. It keeps the change reviewable, auditable, and safe for protected branches.

Example. Your main branch has the latest .github/workflows/frontend-deploy.yml, but staging is behind. Set the variables to:

Run the steps below and you’ll get a PR titled something like “Sync .github/workflows/frontend-deploy.yml from main” with a single‑file diff. Reviewers can approve quickly, and the workflow update applies on staging without merging unrelated code. This approach also works across remotes (e.g., upstream/main) and for multiple files.

Prerequisites

  • Git (2.23+ recommended for git restore; otherwise use the fallback shown).
  • GitHub CLI (gh) logged in: gh auth status -h github.com

0) Configure variables

Tip: For multiple files, set FILES="path1 path2" and replace $FILE with $FILES in steps below.

1) Fetch and branch off the base

2) Restore ONLY the specified file(s) from the source ref

3) Sanity-check the changes

4) Commit and push

5) Open the PR with gh

Notes & variations

  • Multiple files: use a space-separated list, e.g.
    FILES=".github/workflows/a.yml .github/workflows/b.yml" then:
    git restore --source="$SOURCE_REF" -- $FILES and git add -- $FILES.
  • Different remote or refs: change REMOTE, SOURCE_REF, and BASE (e.g., SOURCE_REF="upstream/main", BASE="release/1.9").
  • If the file doesn’t exist on the base branch: restore will add it; that’s fine.
  • If your Git is older: stick with the git checkout <ref> -- <path> fallback.

Troubleshooting

  • fatal: pathspec '<file>' did not match any files
    Check the path and ensure it exists in SOURCE_REF.
  • PR opens but shows other files changed
    Ensure you branched from the remote base (git checkout -b ... "$REMOTE/$BASE") and only restored the intended file(s).

Have a project or a problem?

Talk with a senior engineer for practical recommendations—no obligation.

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Categories

Get a free consultation from Reliable Penguin

Submit the form—or for immediate service call 866-649-7984.