Skip to content

Git and “Get that file from the other branch”

I’ve been using git for about a decade now, and I learned something so simple that I feel like the person in the image above.

Have you ever been working in branch feature-2 and wanted a copy of a file that only exists right now in feature-5 ? I have, and I’ve always compared the two branches in my UI and copy & pasted the file, or done a checkout of feature-5, copied the file to my clipboard, and then checked out feature-2 again.

It turns out you can do this (from the feature-2 branch):

git checkout feature-5 src/the-new-file.php

and it will copy that file into your current branch without anything else that might have changed in that branch or commit.

I’d only ever used the checkout command to switch branches before, so this was a great discovery.


  1. @shooper What you want is `git restore -s feature-5 your-file`.

    `-s` is short for `–source`. And if `your-file` doesn't exist in your current working directory use `git restore -s feature-5 — your-file`.

    • @shooper And more generally I've totally stopped to use `git checkout` two years ago when I discovered `git restore` and `git switch`. Those commands make so much more sense!

      • Shawn Hooper Shawn Hooper

        Thanks for the tip. I’ll have to check those commands out. I’ve definitely been in the habit of using the same basic commands for years (and PHPStorm’s GUI for merge conflict resolution).

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.