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:
Step 2: View Repository Content
Then, run the “ls” command to list the current repository content:
Step 3: Switch to Git root Directory
Next, move to the previous repository through the “cd ..” command:
Step 4: Move to Another Git Local Repository
After that, execute the “cd” command to navigate to the target repository:
Step 5: Show Repository
Run the “ls” command and view the list of the existing files:
It can be seen that the target repository is empty:
After that, revert back to the Git directory by executing the “cd” command:
Step 6: Copy File History
Utilize the “cp” command along with the desired repository name and file name:
Step 7: Add Changes
Now, update the staging index by running the “cd” command:
Step 8: Commit Changes
To push the added changes into the Git repository, use the “git commit” command with the commit message:
Step 9: Move to Target Repository
Finally, switch to the target repository in which copied the files the copied file is present:
Step 10: Verify Copy File
Lastly, use the “ls” command to check the copied file from another repository:
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.
Conclusion
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.