Bootstrap FreeKB - Git (Version Control) - Copy file between repos saving commit history
Git (Version Control) - Copy file between repos saving commit history

Updated:   |  Git (Version Control) articles

Let's say you have a file in a git repository that contains commits. The git log command will display the history of commits in all of the branches of the cloned repository.

commit  mks910122020slsmm3lsosos020399489sl
Author: John Doe <john.doe@example.com>
Date:   Wed May 31 14:51:14 2020 -0500

  updated LDAP queries to include Active Directory (AD)

commit dkci85474fjfdkd9393934k49f9fk002kd01
Author: John Doe <john.doe@example.com>
Date:   Tue May 30 18:23:36 2020 -0500

  imported the ldap3 module

commit fj83m3ld0d0d3m3ld0389303l3ld0d0d39dl
Author: John Doe <john.doe@example.com>
Date:   Mon May 29 20:26:09 2020 -0500

  updated logger to include stderr

 

And let's say you want to copy or more the file into some a totally different git repo and to include the files commit history. This can be done by first creating a file that contains the files commits.

For example, let's say you've cloned foo-repo.git at /home/john.doe on a Linux system, and in foo-repo there is a file named path/to/example.txt. The following command will redirect the commits and various other output that is needed to create the file in the new repo with it's commits history.

git -C /home/john.doe/foo-repo log \
--pretty=email \
--patch-with-stat \
--reverse \
--full-index \
--binary \
-m \
--first-parent -- path/to/example.txt > /tmp/patch

 

And let's say your other git repo is named bar-repo and you've cloned bar-repo.git at /home/john.doe on a Linux system and bar-repo has an identical directory structure to foo-repo, meaning there is a directory in bar-repo named path/to. In this example, the following command can be used to add path/to/example.txt to bar-repo with it's history of commits from foo-repo.

git -C /home/john.doe/bar-repo am --committer-date-is-author-date < /tmp/patch

 

Let's say bar-repo has a different directory structure than foo-repo. For example, perhaps example.txt will be located at my/files/ in bar-repo. In this example, you would replace "path/to/" with "my/files" in the /tmp/patch file.

sed -i 's|path/to/|my/files/|g' /tmp/patch

 

And now you should be able to issue the following command to add my/files/example.txt to bar-repo with it's history of commits from foo-repo.

git -C /home/john.doe/bar-repo am --committer-date-is-author-date < /tmp/patch

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 4a9572 in the box below so that we can be sure you are a human.