How to Clone Only a Subdirectory with Git or SVN
Git is already possible, but SVN is much simpler.
In this tip I will show something that seems simple (in Git), but it is very complicated! =) Because I actually created a Shell Script that you can run as a command in your
For that I recommend you to know our specific Git and GitHub beginner page: https://terminalroot.com.br/git. There you’ll find text and video tips, including a [Git and GitHub for Beginners] mini course (https://www.youtube.com/watch?v=CsyumQN6ZdA&list=PLUJBQEDDLNclXAjLaiPWakoO9VosPDzC1).
In short, cloning a repository means: copying all its files to your computer.
git clone is a Git command line utility that is used to direct an existing repository and create a clone or copy of the destination repository. It is very simple to clone Git repositories: GitHub, GitLab, BitBucket and others.
The command summarizes in:
For example, I have a repository that is https://github.com/terroo/fonts, assuming you want to clone this repository, the command would be:
You can still clone already renaming and/or with the same name in a new path of your computer, example:
- Clone with a new name: new-fonts:
- Clone with a new name within the Downloads directory:
Or even clone with the same name in another path:
Note: If you enter a new path, you must enter a name, either the same name or a new one. If you do so, without entering a name, it will report an error:
fatal: destination path 'Downloads' already exists and is not an empty directory..
In addition there are several parameters you can use, here we will see a few, but you can get the full documentation on the official Git website more specifically at the URL: https://git-scm.com/docs/git-clone.
Cloning a Subdirectory
This repository that I used as an example was totally poorly designed, there you will not only find fonts, but also: Wallpapers, WM configuration files and other things. And because of that, sometimes I want to copy only one * sub directory * into it.
For example, in this repository there is a folder/directory named files and let’s suppose I want to copy/clone only this directory/sub-directory if I do this:
will not work, Git will not allow it, clone will fail:
git clone https://github.com/terroo/fonts/files
And it’s quite inconvenient for you to clone the entire repository (which by the way is quite large) which will take a long time just to use files from a specific repository directory.
Until Git 2.18 this was not possible, but as of 2.19 it is already possible, so check your Git version before proceeding, use the
git --version command to know the version. If it is equal to or greater than 2.19 you can clone only a specific directory.
To clone this directory (files, inside the repository) we will use the following commands, explanation in the comments (#):
For each command, if you do not understand well, I recommend you look over the manual, for example:
man git remote.
If you find a lot of process running all these commands, you can use a shell script, as I said to get automatic and run only one command, see the script by clicking the link below:
If you want to make a command to use in any directory you are in, save it with the name:
git-dir (or the name you want) and move it where you want, for example:
And to use just run
git-dir anywhere you are that it will already show help displaying the options.
Alternative - Using SVN
Note that when we use Git for this, this clone, despite being from a subdirectory, will still be very slow. For this there is a much faster solution using SVN which is an alternative to the [Apache Software Foundation] Git (https://en.wikipedia.org/wiki/Apache_Software_Foundation).
The difference between SVN and Git, in my opinion, is summarized in these two images below, that is, there is no better or worse, there is one that meets your specific needs for each case.
Apache Subversion (also known as SVN) is a version control system designed specifically to be a modern replacement for CVS, which is considered to have some limitations. Subversion uses Berkeley BD database.
First of all, you need to have it installed on your system. To do this use your distro’s package manager:
And to clone just use the
export parameter with fake-dir:
trunk between the repository and the subdirectory, example:
Example for our case:
If you want it to be your command, simply create a command or add this function to your
In this case I treated the end of the repository name if the user copies with * .git * or with / at the end of the URL name.
And to use, just run:
git-svn [repository] [subdirectory]
Much simpler, right?
It is your choice which mode to use. Leave your tips/questions in the comments for us to interact in the solution or discussion on related issues!