What is the difference between `git init` and `git clone`?
Show answer — try answering out loud first
git init creates a new, empty repository in a local folder (you start from scratch). git clone copies an existing repository (usually remote, like one on GitHub) to your machine, with its entire history and already connected to the remote.
They are the two ways to start working with a repository:
git init: use it when you're starting a project from scratch. It turns a normal folder into a Git repository by creating the hidden.gitfolder. It has no history or remote yet.git clone <url>: use it when you want to work on a project that already exists on a server. It downloads all the files and the entire history, creates the folder, runsgit initunder the hood, and also configures theoriginremote pointing to the source URL.
In short: init to start something new, clone to join something that already exists.
# git init: new repository from scratch
mkdir mi-proyecto
cd mi-proyecto
git init
# Output: Initialized empty Git repository in /ruta/mi-proyecto/.git/
# No commits or remote yet
# git clone: copy an existing repository
git clone https://github.com/usuario/proyecto.git
# Creates the "proyecto" folder, downloads the entire history
# and automatically configures the "origin" remote
cd proyecto
git remote -v
# origin https://github.com/usuario/proyecto.git (fetch)
# origin https://github.com/usuario/proyecto.git (push)
Running git init inside a folder you already cloned, thinking it's "needed." It's not: when you clone, you already have a complete repository. Another mistake is forgetting that git init does not connect to any remote; afterward you have to add it yourself with git remote add origin ... if you want to push the project.
"
git initcreates a new, empty repository in a local folder; I use it when I'm starting a project from scratch.git clonecopies a repository that already exists, usually a remote one, with its entire history, and along the way sets up theoriginremote for me. So init is for starting something new and clone is for joining something that already exists."
You've just run git clone on a repo. Do you need to run git remote add origin ... in order to git push?
See answer
No. git clone already automatically configures the origin remote pointing to the URL you cloned from. You can verify this with git remote -v. git remote add origin ... is only necessary when you created the repo with git init and it doesn't have a remote yet.