60

I want to clone a repository from Github. It has lot of history so I'd like to clone without any history.

Is this possible?

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
Dilip Kumar
  • 2,504
  • 2
  • 17
  • 33
  • 7
    Possible duplicate of [copy a git repo without history](http://stackoverflow.com/questions/29368837/copy-a-git-repo-without-history) – nwn Mar 27 '17 at 20:54

3 Answers3

88

You can get a shallow clone using the --depth option with value 1

git clone --depth 1 reponame.git

If you want more commit history, increase the value to meet your needs as required.

mathematician1975
  • 21,161
  • 6
  • 59
  • 101
  • 4
    that does not work, you still get history when you wanna mirror it to another repo – PositiveGuy Dec 19 '18 at 00:26
  • For a shallow clone of submodules you should also pass `--shallow-submodules`. Source: https://git-scm.com/docs/git-clone – S. Doe Jan 03 '23 at 12:40
22

After cloned, you can delete the .git directory, then re-init the git to generate a totally new repo.

$ git clone ...
$ cd path/to/repo
$ rm -rf .git
$ git init
Luca Cappa
  • 1,925
  • 1
  • 13
  • 23
Kjuly
  • 34,476
  • 22
  • 104
  • 118
  • 1
    In fact, this is the only and perfect answer to the question. It is not possible to clone without any history, but you can erase the history afterwards and start anew. Thank you:) – viery365 Apr 30 '18 at 11:08
  • that does not work, because you then are not able to mirror it to a new repo because you've deleted the database and it loses references and can't wire up, so the mirror fails – PositiveGuy Dec 19 '18 at 00:27
  • 1
    @PositiveGuy sorry, what do u mean? Maybe let me make it more clear: after having deleted the history by `$ rm -rf .git`, the cmd `$ git init` will create a pure new repo history w/o legacy one, that's what the OP expected. However, if u expect to send PR to source repo after deleting the history, then that's not a correct workflow, cause git's PR is based on history (w/o history, the PR will have a mess conflicts), i.e., u need to keep the repo as what it was if you want to send PR. Hope it helps. :) – Kjuly Dec 19 '18 at 07:10
  • 1
    @viery365: this will take time to download the entire history, which is what the OP wants to avoid. – Dan Dascalescu Mar 07 '21 at 08:31
  • 1
    @DanDascalescu u'r right, the accepted answer will be better with considering the pull time cost. – Kjuly Mar 08 '21 at 10:03
  • @PositiveGuy Mirror serves a different purpose that what is asked for. A mirror, by definition (Set up a mirror of the remote repository), is asking for history to be included. It is supposed to be an exact copy of the repository being copied AND when updates are made to the remote repository, all refs are pulled again from origin, overwriting refs in the remote. The op asked simply how to git clone without history. The use of --mirror option is not a way to accomplish that. An example of using mirror is if you want to backup your repo in multiple places. – DubStep Oct 19 '21 at 15:38
16

Use the --depth option in git clone:

--depth <depth> Create a shallow clone with a history truncated to the specified number of revisions.

Usage: git clone --depth 1 <remote_repo_url>

And in future if you want your shallow clone to have full history then you can unshallow it using this command

git fetch --unshallow

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184