Copy directory from repository to local folder #23772
-
Hi, I’m a single developer using git to document my project changes and versions. I’m pretty new to git and I still have problems understanding some of the concepts behind it. In the past decades I used to just make dater, commented backups of my projects as a “repository”. I have some projects in a git repository with several commits and three branches. Now I want to merge some files of my current project with the ones in a different branch of the repository. I just can’t find a way to save a copy of the other revision to a different directory. My Project has the branches “D7” and “D10”. It contains a Directory “Customers”. My active project directory is in sync with the branch “D7”. Now I want to merge some content from files in directory “Customers” of branch “D10” to my working directory. To do that I want to extract the complete directory “Customers” to a temporary folder so I can compare the two folders and merge file contents with Beyond Compare. Final question: How can I copy files from a certain revision of the repository to some local directory without destroying the contents of my working directory? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi heinzituberkel! 👋 Are you using Git on the command line? When you
This will bring a copy of the specified file from the specified branch into your current branch. Assuming the files in different branches have different names, this would work for you. If you’re on branch
If the files on each branch have the same names, though, you won’t be able to do this, as it will just swap out the In that case, you could save a copy of the file to a temporary directory that is outside the repository, switch to the other branch, and repeat the process. So, if you’re in the Customers folder: #copy the file to a folder outside the repo and rename it #switch branch #copy the file to the same temp folder outside the repo and rename it Then you can use Beyond Compare on the files. |
Beta Was this translation helpful? Give feedback.
-
heinzituberkel:
The
The new directory is bound to your repository for the git data, but the checkout is independent, so you can copy files and commit on either side as usual. You could also use this to work on two branches separately. Note however that if you want to fully merge the branches (rather than just importing a few files from one to the other, whether using the |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot! The “worktree add” was just what I was looking for. BTW: in the past weeks I did something similar to yamicat’s suggestion: I copied the complete working directory to a different location, checked out the other branch and did the compare. Problem solved 😄 |
Beta Was this translation helpful? Give feedback.
The
git worktree
command can do what you want. E.g. if your current directory is your existing repository, and you want the branchsome-branch
in a directory namedrepo-some-branch
next to your primary directory:The new directory is bound to your repository for the git data, but the checkout is independent, so you can copy files and commit on either side as usual. You could also use this to work on two branches separately.
Note however that if you want to fully merge the branc…