Git

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

While working on a large Git project, developers create many files for multiple features. A single branch can contain multiple files. Sometimes, they may want to duplicate a single file from one branch to another Git branch. For the corresponding purpose, they utilize the β€œgit checkout” command. This command updates a particular file in a working branch from another branch.

This study will explain the procedure of duplicating a single file from one Git branch to another branch.

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

To copy/duplicate a file from one branch to another Git branch, first, switch to the local Git repository. Then, view the list of files in the current working branch and select the file you want to copy to another branch. After that, switch to the target branch and execute the β€œgit checkout <old-branch-name> <file-name>” command. Then, verify the changes in the target branch.

Step 1: Go to Local Directory

First, redirect to the desired local directory using the below-provided command:

$ cd "C:\Git\RepoQ"

 
Step 2: View List of Files

Then, view the list of available files in the working branch:

$ ls

 
According to the below-provided output, the β€œmaster” branch contains two files. Choose the file that needs to be copied to another branch. For instance, we have selected the β€œtestFile.txt” file:


Step 3: View Available Branches

Next, check the list of available branches in the current repository:

$ git branch

 
It can be observed that the working repository contains β€œalpha” and β€œmaster” branches:


Step 4: Switch to Another Branch

Write out the below-provided command and specify the target branch name and switch to it. In our case, β€œalpha” is the target branch:

$ git switch alpha

 

Step 5: Check List of Files in Target Branch

Now, view the list of available files in the current branch:

$ ls

 
Here, it can be seen that the β€œalpha” branch contains two further files:


Step 6: Copy File to Target Branch

Then, execute the β€œgit checkout ” command along with the name of a particular file and branch from which you want to copy the file into the target branch:

$ git checkout master testFile.txt

 

Step 7: Verify Changes

At last, verify changes by viewing the list of files in the target branch:

$ ls

 
The below screenshot indicates that the β€œtestFile.txt” file has been copied from the β€œmaster” branch to the β€œalpha” branch:


We have provided the easiest method to copy a version of a single file from one Git branch to another.

Conclusion

In order to copy/duplicate a single file from one Git branch to another branch, first, redirect to the particular repository. Then, view the list of available files in the working branch and choose the particular file that needs to be copied to another branch. Next, navigate to another branch and run the β€œgit checkout <old-branch-name> <file-name>” command. This study explained the procedure of duplicating a file from one Git branch to another branch.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.