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

Git

Git Problem Overview


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

Git Solutions


Solution 1 - Git

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

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionairnetView Question on Stackoverflow
Solution 1 - GitmichasView Answer on Stackoverflow
Solution 2 - GitjornaneView Answer on Stackoverflow