How to Create a Git Repository (And What a Repo Actually Is)
When you're starting a new coding project (or joining an existing one) the first thing to do is create (or clone) the project's Git repository. But what exactly is a repository?
A Git repository (or "repo") is at its core a special, hidden directory named .git located at the root of your project folder. This is where Git stores the complete record of your project's history:
- Commits are like snapshots or "save points" in your project. Each time you reach a good stopping point, you can create a commit to save the state of your entire project — and flip back to any of these saved points later.
- Branches let you create a separate copy of your project to try out a new idea or feature, without affecting the main codebase. If you like the result, you can merge it back in.
- Tags mark important milestones, making it easy to find and revisit specific versions later.
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
Creating a Brand New Repository
If you want to start a new project and put it under version control with Git, use git init:
#1: Create a folder for this project on your local hard drive
$ mkdir my-project
#2: Change into this folder
$ cd my-project
#3: Initialize a new, empty Git repository here
$ git init
...after having written some code and created some files...
#4: Add all changes to the next (= first) commit
$ git add .
#5: Create this first commit
$ git commit -m "Initial commit"
Note that git init only creates a blank repository — it does not automatically add any existing files in the directory to version control. It's your responsibility to then deliberately add files and create commits.
Tip
Creating a Repository in Tower
In case you are using the Tower Git client, you can simply drag and drop your project folder — and then create your first commit with a couple of simple clicks:
Cloning an Existing Repository
If you want to join an existing project that has already been started by someone else, you have to clone it. Cloning a repository from a remote server means downloading it to your computer so you can work on the project:
#1: Move to the location where you want to download the project
$ cd my-projects
#2: Download the project by cloning it
$ git clone https://github.com/gittower/git-crash-course.git
You might then have to provide your authentication details — and will then have the complete project, including all of its history, on your computer and ready to contribute to.
Tip
Cloning a Repository in Tower
In case you are using the Tower Git client, cloning a project is very easy: after you've connected your GitHub / GitLab / Bitbucket / ... account to Tower, you don't have to worry about usernames, passwords, or tokens anymore. You'll have access to all of your repositories on the remote server — and can clone any of them with just a single click:
Local and Remote Repositories
There are two main types of Git repositories:
- Local Repository: This is the repository that lives on your own computer. It's where you do all your work: creating new files, making changes, and committing them.
- Remote Repository: This is a repository hosted on a server, usually on a platform like GitHub, GitLab, or Bitbucket. A remote repository allows you to share your work with others, collaborate on projects, and back up your code.
You can have multiple remote repositories for a single project, but it's most common to have one central repository that everyone on the team uses to stay in sync.
The .git Directory
Every Git repository has a hidden .git folder at its root. This is where all of Git's internals live:
objects: Stores all the snapshots of your files as Git objects.refs: Stores pointers to your commits, such as branches and tags.HEAD: Points to the currently checked-out branch.config: Contains repository-specific configuration settings.
You should never manually edit files inside .git — Git manages this directory entirely on its own.
"fatal: not a git repository" — What This Error Means
Sooner or later, you'll probably run a Git command and be greeted by this message:
fatal: not a git repository (or any of the parent directories): .git
Git is telling you that neither the current directory nor any of its parent directories contains a .git folder — in other words, you're not inside a Git repository. The most common causes are:
- You're in the wrong directory. Simply
cdinto your project folder and try again. - The project isn't under version control yet. Run
git initto create a repository, as shown above. - You downloaded the project as a ZIP archive instead of cloning it. A ZIP download doesn't include the
.gitfolder — clone the repository withgit clone <url>to get a working copy with its full history.
Get our popular Git Cheat Sheet for free!
You'll find the most important commands on the front and helpful best practice tips on the back. Over 100,000 developers have downloaded it to make Git a little bit easier.
About Us
As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.
Just like with Tower, our mission with this platform is to help people become better professionals.
That's why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.