How to checkout a file from another branch in Git

Checking out a file from another branch allows you to copy specific changes without merging entire branches or switching contexts. As the creator of CoreUI with extensive Git experience across numerous projects, I regularly copy files between branches when cherry-picking features or applying hotfixes. The most straightforward approach uses git checkout with the branch name and file path to copy the file to your current branch. This method enables selective file copying while maintaining your current working context and branch state.

Use git checkout with a branch name and file path to copy a specific file from another branch.

git checkout feature-branch -- src/components/Button.js

This command copies the Button.js file from feature-branch to your current branch, replacing your local version with the one from the specified branch. The copied file is automatically staged for commit, ready to be included in your next commit. This is particularly useful for applying specific fixes or features from other branches without full merges.

Best Practice Note:

This is the selective copying method we use in CoreUI development for applying component updates across different feature branches. Use git status after checkout to verify which files have been modified and staged, ensuring you commit only the intended changes.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
Maintaining Accessibility with React createPortal and aria-owns: A Complete Guide
Maintaining Accessibility with React createPortal and aria-owns: A Complete Guide

How to check if an array is empty in JavaScript?
How to check if an array is empty in JavaScript?

What Does javascript:void(0) Mean?
What Does javascript:void(0) Mean?

How to Hide Scrollbar with CSS
How to Hide Scrollbar with CSS

Answers by CoreUI Core Team