Do I need to do 'git init' before doing 'git clone' on a project

I'm doing a git clone on a project following the instructions. But, do I need to do an init in the directory beforehand?


Solution 1:

git clone is basically a combination of:

  • git init (create the local repository)
  • git remote add (add the URL to that repository)
  • git fetch (fetch all branches from that URL to your local repository)
  • git checkout (create all the files of the main branch in your working tree)

Therefore, no, you don't have to do a git init, because it is already done by git clone.

Solution 2:

git init will create a new repository. When running git clone, what actually happens in the background is a git init, followed by git remote add origin ${URL} and then a git pull.

Typically, you only use git init if you already have code and you want to put it in a new Git repository.

In answer to your question: if you want to clone a project, then you do not need git init.