How to cherry-pick from separate repos with separate paths (with history)

If you ever wanted to extract parts of code from one repository to another one, but you also wanted to keep the history of commits for those parts, there is a way.

You can use

git log –pretty=email –patch-with-stat –reverse –full-index –binary -…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Cristian-Florin Calina

If you ever wanted to extract parts of code from one repository to another one, but you also wanted to keep the history of commits for those parts, there is a way.

You can use

git log --pretty=email --patch-with-stat --reverse --full-index --binary -- <file_path | folder_path> > ~/toggle.patch

Explained:
--pretty=email formats the output to show the hash, the date, the author, author date and subject + the commit message.

--patch-with-stat generates a diffstat for the file/folder

--reverse outputs the commits in reverse order (so that they can be applied in the correct order in the other repository).

--full-index this shows the full index of the commit when outputing the diffs (instead of showing just the first characters).

--binary outputs the diff in binary so that git-apply can use it.

This will generate a patch file with the differences that were done on the file or folder that you pass to it (and will keep commits history as well).

After that, you must mass edit the generated toggle.patch file by changing the paths in diffs to match the new paths in the new repo.

After you changed the patch, you can apply it in your new repo by using

git am --committer-date-is-author-date < ~/toggle.patch.

Explained:
--commiter-date-is-author-date allows the user to lie about the committer date by using the same value as the author date.

This way, you keep all of the history for this extracted file/folder (and you keep the dates as well when looking over the file history).

Hope this helps.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Cristian-Florin Calina


Print Share Comment Cite Upload Translate Updates
APA

Cristian-Florin Calina | Sciencx (2023-02-05T22:09:47+00:00) How to cherry-pick from separate repos with separate paths (with history). Retrieved from https://www.scien.cx/2023/02/05/how-to-cherry-pick-from-separate-repos-with-separate-paths-with-history/

MLA
" » How to cherry-pick from separate repos with separate paths (with history)." Cristian-Florin Calina | Sciencx - Sunday February 5, 2023, https://www.scien.cx/2023/02/05/how-to-cherry-pick-from-separate-repos-with-separate-paths-with-history/
HARVARD
Cristian-Florin Calina | Sciencx Sunday February 5, 2023 » How to cherry-pick from separate repos with separate paths (with history)., viewed ,<https://www.scien.cx/2023/02/05/how-to-cherry-pick-from-separate-repos-with-separate-paths-with-history/>
VANCOUVER
Cristian-Florin Calina | Sciencx - » How to cherry-pick from separate repos with separate paths (with history). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/05/how-to-cherry-pick-from-separate-repos-with-separate-paths-with-history/
CHICAGO
" » How to cherry-pick from separate repos with separate paths (with history)." Cristian-Florin Calina | Sciencx - Accessed . https://www.scien.cx/2023/02/05/how-to-cherry-pick-from-separate-repos-with-separate-paths-with-history/
IEEE
" » How to cherry-pick from separate repos with separate paths (with history)." Cristian-Florin Calina | Sciencx [Online]. Available: https://www.scien.cx/2023/02/05/how-to-cherry-pick-from-separate-repos-with-separate-paths-with-history/. [Accessed: ]
rf:citation
» How to cherry-pick from separate repos with separate paths (with history) | Cristian-Florin Calina | Sciencx | https://www.scien.cx/2023/02/05/how-to-cherry-pick-from-separate-repos-with-separate-paths-with-history/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.