Git Copy File Preserving History

The Git repository contains several project files, and sometimes developers need to make the copy of the files for utilizing them later. To perform the copy operation and preserve history, users need first to select the target repository, view content, choose a file and execute the “cp <repo-name/target-file> <target-repo-name/target-file>” command.

This guide will discuss the easiest way to copy the file preserving history.

How to Copy File Preserving History in Git?

Follow the provided steps to copy the file preserving history:

    • Switch to the desired directory.
    • Check the repository content list.
    • Navigate to the root directory.
    • Move to the target repository and show its content list.
    • Run the “cp <repo-name/target-file> <target-repo-name/target-file>” command.
    • Add changes to the target repository and move to it.

Step 1: Go to Particular Repository

At first, provide the Git repository path and execute the “cd” command:

$ cd "C:\Users\nazma\Git\test1-repo"

Step 2: View Repository Content

Then, run the “ls” command to list the current repository content:

$ ls


Step 3: Switch to Git root Directory

Next, move to the previous repository through the “cd ..” command:

$ cd ..


Step 4: Move to Another Git Local Repository

After that, execute the “cd” command to navigate to the target repository:

$ cd "C:\Users\nazma\Git\test1"


Step 5: Show Repository

Run the “ls” command and view the list of the existing files:

$ ls

It can be seen that the target repository is empty:

After that, revert back to the Git directory by executing the “cd” command:

$ cd ..


Step 6: Copy File History

Utilize the “cp” command along with the desired repository name and file name:

$ cp test1-repo/file1.txt test1/file1.txt


Step 7: Add Changes

Now, update the staging index by running the “cd” command:

$ git add test1/file1.txt


Step 8: Commit Changes

To push the added changes into the Git repository, use the “git commit” command with the commit message:

$ git commit -m "copy file from test1-repo/ to test1/"


Step 9: Move to Target Repository

Finally, switch to the target repository in which copied the files the copied file is present:

$ cd "C:\Users\nazma\Git\test1"


Step 10: Verify Copy File

Lastly, use the “ls” command to check the copied file from another repository:

$ ls

According to the below-given output, the file has been copied successfully:

That’s all! We have provided the method of copying the file and preserving history.


To copy the file preserving history, first, switch to the desired directory and check its content list. Then, navigate to the root directory, move to the target repository, and show the content list. After that, run the “cp <repo-name/target-file> <target-repo-name/target-file>” command and add changes to the target repository and move to it. This guide explained the way to copy file preserving history.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.