Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aont/1c117d5fac7c847d33e8698d846469b8 to your computer and use it in GitHub Desktop.

Select an option

Save aont/1c117d5fac7c847d33e8698d846469b8 to your computer and use it in GitHub Desktop.

Copy branch branch-b state into branch-a as a new commit

Summary: A short, simple guide to take the file state from branch-b (for example the tree at commit commit-b) and make that state a new commit on branch branch-a — without creating a merge commit or linking histories.


When to use this

Use this when you want branch-a to contain the same file contents as branch-b (the working tree of commit-b) but you do not want to perform a git merge or record branch-b as a parent in history. This produces a normal commit on branch-a whose snapshot equals commit-b.


Step-by-step

  1. Make sure your working tree is clean (stash or commit any local changes).
git status    # verify clean working tree
  1. Switch to branch branch-a.
git switch branch-a
  1. Overwrite the working tree with the files from branch-b.
git checkout branch-b -- .

This replaces the files in your working tree and index (for tracked files) with the versions from branch-b.

  1. Create the new commit on branch-a.
git commit -m "Apply state of branch-b (commit commit-b) to branch-a"
  1. Verify.
git log --oneline -n 3
git diff HEAD^ HEAD   # show the snapshot change you just made

Notes & caveats

  • git checkout branch-b -- . updates tracked files to the tree from branch-b. Untracked files on branch-b are not copied. If branch-b has generated files you need, handle them separately.
  • This creates a new commit on branch-a. History does not reference branch-b or commit-b; it just has the same content snapshot.
  • If there are important local changes on branch-a, make sure to commit or stash them first.
  • Conflict handling: if git checkout ... -- . fails for some paths or you want a more controlled apply, consider using git restore --source=branch-b --worktree --staged . (modern command) or a temporary merge strategy — but the above is simplest.
  • Pushing: because this is a normal commit on top of branch-a, a regular git push will work (no force required) unless remote diverged.

Example commit message

Apply state of branch-b (commit commit-b) to branch-a

Copied file tree from branch-b (commit-b) and committed as a new snapshot on branch-a.
This is NOT a merge — history remains linear on branch-a.

Quick checklist before you start

  • git status is clean
  • You are on branch-a (git switch branch-a)
  • You have reviewed the changes (git diff after step 3)

That’s it — a simple, safe way to copy the exact file state from branch-b into branch-a as a new commit (no merge parent). Want a tiny script that runs these steps and prints checks? I can add one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment