Adding a folder from one repo to another #23346
-
I have been playing around to create a
I want to copy the A few of the solutions I have seen requires deleting the Is there a way by which I can copy over the folder by only playing around with the |
Beta Was this translation helpful? Give feedback.
Replies: 18 comments 1 reply
-
If you’re referring to the solution described in this blog post, as one of the ones you’ve seen but disliked how it worked because it made changes to the source repository, then I’m afraid to say that it is the canonical solution for getting done what you’re describing. I also think you’re misunderstanding when you say that the solution requires "deleting the The goal here is to copy from a repo named
Now, we are in the directory
Next, we want to filter the commit history of the
But, as you’ll notice in the documentation for the
This means that everything that was in
Now we have a local repository that contains only the files and history that we want. We’re halfway there! 🎉And remember, this is just a temporary place to store some work. It is completely separated from your normal local Now, in order to merge this new history that we’ve created into the destination repository, let’s assume that the destination repository is already cloned locally in
in order to move from
This pulls all of the commit history of our temporary
This removes the link between our local I hope that helps explain things a bit better! Let me know if you have any questions. |
Beta Was this translation helpful? Give feedback.
-
very detailed and targeted answer, this is really useful |
Beta Was this translation helpful? Give feedback.
-
Really good answer. From my understanding, this will copy the most recent commit and the history for the specific subfolder. Is there a way to keep the source remote, and thus the hability to sync only that folder? Thank you |
Beta Was this translation helpful? Give feedback.
-
You can’t keep the source remote and sync only that folder, no. But, you could remove the source folder from the original repository and add the new repository back to the original one as a submodule. I believe this would provide essentially the functionality you’re looking for. I hope that helps! |
Beta Was this translation helpful? Give feedback.
-
Hello @lee-dohm , If I want to wrap your answer in a batch file, is there any way to revert the old repo to its original state before running this batch file? |
Beta Was this translation helpful? Give feedback.
-
I’m not sure I understand the question @agwatic. The whole point of my answer is for it to not change the original repo in any way, so I’m not sure what there would be to revert? |
Beta Was this translation helpful? Give feedback.
-
Hi @lee-dohm
We had a similar issue, and used your solution but ended up losing part of history. The folder we wanted to move to another repo was carried from different directory one year ago. In current repository we are able to see whole history but when I use filter-branch and extract the folder, the history before directory change was carried out is gone. Do you have any ideas how we can keep whole history for this folder? Thanks. |
Beta Was this translation helpful? Give feedback.
-
Unfortunately, I do not have any ideas @utkucan. I assume that the history before the directory change was carried out is gone because you’re using the You may want to experiment with other filter options to see if you can find something that would work the way you want. |
Beta Was this translation helpful? Give feedback.
-
After making your tutorial I didn’t manage to get back to the old state of the repo. How can I get back to the old state before extracting the directory from the repo? |
Beta Was this translation helpful? Give feedback.
-
As I said before, the process I described does not change the original repository. If your original repository was changed, can you tell me how exactly it was changed? |
Beta Was this translation helpful? Give feedback.
-
Thanks @lee-dohm . I’m using this exact method. But doing the following step in the “source” repo, causes all commit history to be lost/unviewable when I push my “destination” repo branch back up to GitHub:
If I omit that step, I can view all of the commit history in GitHub, in tact. Is there any way possible to reintroduce the original “foo” directory structure in the “destination” repo, and preserve commit history in GitHub? I know there are a lot of posts about this, indicating that git log will show the original history, and I’ve confirmed that as well. But is there a way to cause GitHub to show it? Thanks |
Beta Was this translation helpful? Give feedback.
-
Hi.This is very nice demo but there is one problem which I am struggling and didn’t find when I started google since last week. The file which I need to copy from source repo to target repo is actually copied in the root folder. I am unable to find which option i must use if i want to copy to specific folder at destination repo. eg. I have a file ‘myfile.txt’ in /sourcerepo/srcfolder/srcsubfolder and want to copy into /targetrepo/targetfolder/targetsubfolder. I tried so many options and asked so mamy friends, everyone treid but file copied only in /targetrepo not in /targetrepo/targetfolder/targetsubfolder. Please help me how to copy in targetsubfolder or where should i give path for copy file in target folder. I appreciate all for your help. Thanks. |
Beta Was this translation helpful? Give feedback.
-
Could you not do the same process again, this time with the older directory? |
Beta Was this translation helpful? Give feedback.
-
A very nice tutorial. Things can be simplified a bit by the use of the git subtree command. You can extract a history related to a subdirectory into a branch:
This way you end up with the split branch holding a history related to the subdir directory, with each commit prefixed with the (split) string. Then you can use standard branch operation tools to push/merge the branch, etc. A good thing is that you can safely do it in the main repo without fear of destroying anything. |
Beta Was this translation helpful? Give feedback.
-
Great answer and work! My complained about this line in terminal but it with a look at files and folder it had done it is job.
mv: rename foo to foo/foo: Invalid argument |
Beta Was this translation helpful? Give feedback.
-
I had to do this recently, and I ended up using git-filter-repo. I was moving a subfolder from my current repo in to a branch of another repo. I used the following commands: Note: This will bring out the history to your repo root and completely re-write it, destroying the old history in the process.
then in the new repo I created a new orphan branch
Run the following in the branch of the new repo, assuming you have them both cloned out locally
Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
I think this approach by default moves directory to the target repo’s ROOT level. But how can we move the source folder from repo 1 to a specific target location in repo 2 (not to the ROOT)? |
Beta Was this translation helpful? Give feedback.
-
Did you find a solution to your use case? I have the same issue. |
Beta Was this translation helpful? Give feedback.
If you’re referring to the solution described in this blog post, as one of the ones you’ve seen but disliked how it worked because it made changes to the source repository, then I’m afraid to say that it is the canonical solution for getting done what you’re describing. I also think you’re misunderstanding when you say that the solution requires "deleting the
origin
" or “create additional branch or commits”. Let me see if I can rephrase the solution more clearly.The goal here is to copy from a repo named
source
to a repo nameddestination
only the contents of folderfoo
, including all history that touches folderfoo
. In order to do that, because Git doesn’t track folders per se but track…