Justin
Justin

Reputation: 45350

Git clone without .git directory

Is there a flag to pass to git when doing a clone, say don't clone the .git directory? If not, how about a flag to delete the .git directory after the clone?

Upvotes: 224

Views: 188117

Answers (9)

sensorario
sensorario

Reputation: 21620

If the repository is on GitHub, you can simply download a ZIP file of any tag:

curl -L https://github.com/<user>/<repo-name>/archive/refs/tags/v1.2.3.zip | tar xz

This will download and uncompress the repository. The contents will be in <repo-name>-1.2.3, with no .git folder.

Upvotes: 2

Tigran Sargsyan
Tigran Sargsyan

Reputation: 19

git clone --depth=1 --branch=master git://someserver/somerepo dirformynewrepo1
rd /s /q  .\dirformynewrepo1\.git

this works for windows systems

Upvotes: 1

Guido Tarsia
Guido Tarsia

Reputation: 2162

git clone --separate-git-dir=$(mktemp -u) --depth=1 <repo> <dir> && rm <dir>/.git

I like this solution more because I don't like rm -rfing things automatically. It just rms a .git file, which means it could never accidentally rm -rf a wrong .git directory

It has a dependency on mktemp command so it'll work *nix systems (from what I see this needs further work for the mktemp to work on MacOS, so if anyone wants to comment a working solution I'll add it)

In zsh, I made that a function so I ensure a dir value is defined:

alias np='node-project'
function node-project() {
  dir=${1:-.}
  git clone --separate-git-dir=$(mktemp -u) --depth=1 <my-node-repo> $dir && rm $dir/.git
}

Explanation

The --separate-git-dir flag lets you specify a path for the .git directory. The resulting "project" will have a .git file (not a directory) whose content will be a single line:

gitdir: <the dir you specified in the flag>

Because we used a tmp dir with the mktemp command, the actual .git directory contents will end up in a tmp dir. We also use a --depth=1 so it takes less space on tmp dirs.

Upvotes: 12

L&#225;szl&#243; Papp
L&#225;szl&#243; Papp

Reputation: 53155

git archive --remote already implements this.

Upvotes: 1

M Imam Pratama
M Imam Pratama

Reputation: 1289

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.

Upvotes: 6

Adam Dymitruk
Adam Dymitruk

Reputation: 129566

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.

Upvotes: 305

Erik Vullings
Erik Vullings

Reputation: 5770

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...

Upvotes: 26

Huang Tao
Huang Tao

Reputation: 2264

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.

Upvotes: 36

user529758
user529758

Reputation:

You can always do

git clone git://repo.org/fossproject.git && rm -rf fossproject/.git

Upvotes: -3

Related Questions