Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git clone without .git directory

People also ask

How do I clone a git repository without a directory?

If the current directory is empty, you can do that with: git clone [email protected]:me/name.

Do I need .git folder?

A . git folder is required to log every commit history and every other information required for your remote repository, version control, commits etc. These things are saved in different folders which have different meanings. Once the folder is created, open it and see the contents of the folder.

Can I just copy the .git folder?

Thanks. Yes you can, but there's no need to copy the full working tree. You can copy just the . git folder without the working tree (i.e. as a "bare" repo) and then checkout the latest working tree on the other machine.

Does git clone create directory?

The standard approach to clone is repository is using the git-clone command. But when you simply clone a repository with git clone <repository> , it creates a new directory with repository name at the current path in the file system and clones the repository inside it.


Use

git clone --depth=1 --branch=master git://someserver/somerepo dirformynewrepo
rm -rf ./dirformynewrepo/.git
  • The depth option will make sure to copy the least bit of history possible to get that repo.
  • The branch option is optional and if not specified would get the default branch.
  • The second line will make your directory dirformynewrepo not a Git repository any more.
  • If you're doing recursive submodule clone, the depth and branch parameter don't apply to the submodules.

since you only want the files, you don't need to treat it as a git repo.

rsync -rlp --exclude '.git' user@host:path/to/git/repo/ .

and this only works with local path and remote ssh/rsync path, it may not work if the remote server only provides git:// or https:// access.


Alternatively, if you have Node.js installed, you can use the following command:

npx degit GIT_REPO

npx comes with Node, and it allows you to run binary node-based packages without installing them first (alternatively, you can first install degit globally using npm i -g degit).

Degit is a tool created by Rich Harris, the creator of Svelte and Rollup, which he uses to quickly create a new project by cloning a repository without keeping the git folder. But it can also be used to clone any repo once...


For those who doubt the --depth 1 solution because it still download the .git directory and you need to manually remove it afterward, maybe you need to know how git clone actually works.

When you normally clone a repo, git download all your files (spanning across commits) into the .git directory. When you clone with --depth 1, git only downloads the latest version of the files into .git. After that, git will checkout or retrieve those files from .git into the working directory (no more download).

And oftentimes, because the file objects inside .git is compressed, you will save more bandwidth by downloading the files with git clone --depth 1 rather than downloading the uncompressed files. And for some people with slow internet, that is worth the price (the need to run rm -rf).

I personally think the git archive solution is better but since it's not supported by GitHub, --depth 1 is the way to go.