Copy directory from repository to local folder


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?

Hi heinzituberkel! :wave: Are you using Git on the command line?

When you git checkout to a different branch, the entire contents of the repository change to the version of the files on that branch. You can, however, checkout just specific files from other branches, using:

git checkout branch_name file_path/file_name.ext

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 D7 currently you can use:

git checkout D10 Customers/file_name.ext

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 D7 version for the D10 version of the file with that name.

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
cp file_name.ext ../../temp/filenameD7version.ext

#switch branch
git checkout d10

#copy the file to the same temp folder outside the repo and rename it
cp file_name.ext ../../temp/filenameD10version.ext

Then you can use Beyond Compare on the files.

The git worktree command can do what you want. E.g. if your current directory is your existing repository, and you want the branch some-branch in a directory named repo-some-branch next to your primary directory:

git worktree add ../repo-some-branch some-branch

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 checkout method described by @yamiacat or worktrees) a git merge may be a better solution.


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 :smile: