How Do I Copy a Version of a Single File From One Git Branch to Another?

Better Stack Team
Updated on August 12, 2024

To copy a version of a single file from one Git branch to another, you can use the git checkout command to extract the file from the desired branch and then add and commit it to your current branch. Here’s a step-by-step guide on how to do this:

Steps to Copy a Single File from One Branch to Another

  1. Check Out the Target Branch

    First, switch to the branch where you want to copy the file to (the target branch).

     
    git checkout target-branch
    

    Replace target-branch with the name of your target branch.

  2. Copy the File from the Source Branch

    Use the git checkout command to extract the file from the source branch into your current branch.

     
    git checkout source-branch -- path/to/file
    

    Replace source-branch with the branch containing the version of the file you want, and path/to/file with the path to the file.

    For example, if you want to copy example.txt from the branch feature-branch to your current branch:

     
    git checkout feature-branch -- example.txt
    

    This command will check out the version of example.txt from feature-branch and place it in your working directory on the target-branch.

  3. Stage the Copied File

    After checking out the file, it will appear in your working directory. Stage the file to prepare it for committing.

     
    git add path/to/file
    

    For example:

     
    git add example.txt
    
  4. Commit the File

    Commit the copied file to your current branch.

     
    git commit -m "Copy file from source-branch to target-branch"
    
  5. Push the Changes (if applicable)

    If you are working with a remote repository and want to push the changes, use:

     
    git push origin target-branch
    

    Replace target-branch with the name of your branch.

Example Scenario

Assume you have a file config.json in feature-branch and you want to copy it to main:

  1. Switch to the main Branch:

     
    git checkout main
    
  2. Copy config.json from feature-branch:

     
    git checkout feature-branch -- config.json
    
  3. Stage and Commit the File:

     
    git add config.json
    git commit -m "Copy config.json from feature-branch to main"
    
  4. Push Changes to Remote (if needed):

     
    git push origin main
    

Additional Tips

  • Verify the File: Before committing, you can review the changes using git diff to ensure that the file is correct.
  • Check Out Specific Commits: If you need a specific version of the file from a commit rather than a branch, you can specify the commit hash:

     
    git checkout <commit-hash> -- path/to/file
    

    Replace <commit-hash> with the specific commit hash.

Summary

To copy a single file from one Git branch to another:

  1. Switch to the target branch.
  2. Use git checkout source-branch -- path/to/file to copy the file.
  3. Stage the file with git add.
  4. Commit the changes with git commit.
  5. Push the changes to the remote repository if needed.

This method allows you to selectively incorporate changes from one branch into another without merging the entire branch.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github