Create or clone a repository

Git · Essentials · 6 min

Every Git workflow starts with a repository; these two commands are how you get one.

A repository is a project folder plus a hidden .git directory. That .git directory is the repository: it holds every commit, branch and setting. The files you see around it are just the currently checked-out version.

There are two ways to get one.

`git init` turns the current folder into a new, empty repository. Nothing is tracked yet; you choose what to add and commit. Use it for brand-new projects or to start versioning an existing folder.

`git clone <url>` copies an existing repository, usually from a server. It downloads the full history, checks out the default branch into a new folder and remembers the source as a remote named origin. URLs come in two common flavours:

  • HTTPS, https://github.com/user/repo.git, which authenticates with a token or credential helper.
  • SSH, git@github.com:user/repo.git, which authenticates with your SSH key.

You can pass a folder name as a second argument (git clone <url> myapp).

Because the repository lives in .git, deleting that folder deletes all history and leaves plain files behind. Moving or renaming the project folder is fine; Git doesn't care where it lives.

Don't create a repository inside another one by accident (for example running git init in your home directory). Run git rev-parse --show-toplevel if you are unsure which repository you are in.

Example

# Brand-new project
mkdir notes && cd notes
git init                      # Initialized empty Git repository in .../notes/.git/
ls -a                         # .  ..  .git

# Copy an existing project with its full history
git clone https://github.com/git/git.git
git clone git@github.com:me/app.git my-app   # SSH, into folder my-app
cd my-app
git remote -v                 # origin  git@github.com:me/app.git (fetch/push)

Remember

  • The .git directory is the repository; the rest is a working copy
  • git init starts empty; git clone copies full history and sets up origin
  • HTTPS uses tokens, SSH uses keys

Watch out for

  • Running git init in your home folder, which makes every file under it look untracked.

Check yourself

  1. What does git clone give you besides the files?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: git-scm.com/book/en/v2/Git-Basics-Getting-a-Git-Repository