Git Init: Creating a Repository
A folder on your computer is only a folder. You can put files in it and change them, but nothing remembers what they looked like yesterday.
One command changes that. git init turns an ordinary folder into a repository: a folder whose history Git records. It is the first command of every new project, and it takes less than a second.
In this lesson, you will create your first repository, see exactly what git init adds to the folder, and learn the second way to get a repository: copying an existing one with git clone.
Before git init
Git commands work only inside a repository. In an ordinary folder, Git has nothing to work with and says so:
git statusfatal: not a git repository (or any of the parent directories): .gitYou will see this message many times in your career. It always means the same thing: the folder you are in is not part of a repository. Usually you are in the wrong folder, and now and then you forgot git init.
Create a Repository
Navika starts the Fee Tracker project. She creates a folder, moves into it, and runs git init:
mkdir fee-tracker
cd fee-tracker
git initInitialized empty Git repository in /home/navika/projects/fee-tracker/.git/How it works
mkdir fee-trackermakes a new folder.cd fee-trackermoves into it. Git works on the folder you are in, so this step matters.git initcreates the repository and tells you where it put it.
The repository is "empty" because it has no commits yet. It is ready to record them.
Syntax
git init [<directory>]| Form | It is used to |
|---|---|
git init | Make the current folder a repository |
git init <directory> | Create the folder if needed, and make it a repository |
git init -b <name> | Name the first branch (also written --initial-branch=<name>) |
git init --quiet | Do the same without printing the message (also written -q) |
What git init Added
Look at the folder, including hidden files. The -a option of ls shows names that begin with a dot, which are normally hidden:
ls -a.
..
.gitThe single dot is the folder itself and the two dots are the folder above it. They are in every folder. The new item is .git.
The .git folder is the repository. Every commit you make, every branch and every setting of this project will be stored inside it. Your own files stay next to it, where they always were. Git added nothing else and changed nothing else.
Now git status has a repository to report on:
git statusOn branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)You are on the branch main, there are no commits yet, and there are no files to save. The branch is named main because of the init.defaultBranch setting from the lesson "Git Config: Setting Up Git".
Start Tracking an Existing Project
git init is not only for empty folders. If you already have a project full of files, move into its folder and run git init there. Your files are not changed, moved or deleted. Git starts watching the folder, and all your existing files show up as new files that you can commit.
Running git init a second time by accident is harmless too:
git initReinitialized existing Git repository in /home/navika/projects/fee-tracker/.git/The message says "Reinitialized", but nothing was reset. Your commits and settings are kept.
Which Repository Am I In
A repository covers its folder and every folder inside it. From any subfolder, however deep, Git searches upward until it finds a .git folder. That is what "or any of the parent directories" meant in the error message above.
To see where the repository you are in begins, ask for its top folder:
git rev-parse --show-toplevel/home/navika/projects/fee-trackerThis matters because of a classic accident. If you run git init in the wrong place, such as your home folder or your Desktop, everything below it becomes one giant repository: your documents, your downloads and every project. Git then reports thousands of files, and your real projects end up nested inside it.
If you did create a repository in the wrong folder and it has no commits you need, remove that folder's .git folder with rm -rf .git. The files are untouched, and the folder is ordinary again.
The Other Way: git clone
git init starts a new history. When a project already exists, for example on GitHub, you don't start again. You clone it: git clone downloads a complete copy of the repository, with every commit.
git clone https://github.com/your-username/fee-tracker.gitA clone doesn't need a website. Git can clone from any repository it can reach, including a folder on the same computer. To see what a clone contains, Navika first gives her repository one commit:
echo "Fee Tracker" > README.md
git add README.md
git commit -m "Add project README"[main (root-commit) b5759a1] Add project README
1 file changed, 1 insertion(+)
create mode 100644 README.mdThen she moves up one folder and clones the repository into a new folder:
cd ..
git clone fee-tracker fee-tracker-copyCloning into 'fee-tracker-copy'...
done.cd fee-tracker-copy
ls -a
git log --oneline.
..
.git
README.md
b5759a1 (HEAD -> main, origin/main, origin/HEAD) Add project READMEThe new folder has the file, its own .git folder, and the full history: the same commit with the same ID, b5759a1. The names origin/main and origin/HEAD are new. They show that this copy remembers where it came from. A clone sets up that connection for you, under the name origin.
git init | git clone | |
|---|---|---|
| Use it when | You start a new project, or want to track a folder you already have | The project already exists somewhere |
| History | Empty | The complete history of the original |
| Remote | None | origin, pointing to the original |
| Creates a folder | Only with git init <directory> | Yes, named after the project |
Cloning from a hosting service, and everything about remotes, is covered in the lessons on remote repositories. For now, remember that there are two doors into Git: init for new, clone for existing.
Common Mistakes
Running git init in the wrong folder. The most common place is the home folder. The sign is a git status that lists files from all over your computer. Check with git rev-parse --show-toplevel, and remove the stray .git folder if it has nothing you need.
Creating a repository inside a repository. Running git init in a subfolder of a project that is already a repository gives you two repositories, one inside the other. The outer one can't track the inner one's files properly. One project, one .git folder, at the top.
Running git init after git clone. A cloned folder is already a repository. There is nothing to initialize.
Forgetting cd. After mkdir fee-tracker, you are still outside the new folder. git init would then turn the folder you are standing in into a repository, not fee-tracker.
Thinking git init saves your files. It only creates the empty repository. Nothing is recorded until you add files and commit them.
Under the Hood
git init does very little, which is why it is instant. It creates the .git folder with a few small files and empty folders inside: a place for objects, a place for branch names, a config file for this repository's local settings, and a file named HEAD that says which branch you are on. No commit, no branch file and no objects exist yet. The branch main is only a name written in HEAD until your first commit creates it.
The lesson "Inside the .git Folder" opens the folder and looks at each part.
Interview Corner
What does git init do?
It creates a new, empty Git repository in the current folder (or in the folder you name) by creating the hidden .git folder. It doesn't change, add or commit any of your files. Running it again in an existing repository is safe and keeps the history.
What is the difference between git init and git clone?
git init creates a new repository with an empty history. git clone copies an existing repository: it creates a folder, downloads the full history, checks out the files, and sets up a remote named origin that points to the source.
What is the .git folder?
It is the repository itself: the place where Git stores all commits and other objects, the branches and tags, the staging area and the local configuration. The project files next to it are the working tree. Deleting .git deletes the history and leaves only the current files.
What happens if you run git init inside an existing repository?
In the top folder, Git prints "Reinitialized existing Git repository" and keeps everything. In a subfolder, it creates a second, nested repository, which is almost always a mistake.
How do you turn a repository back into a normal folder?
Delete its .git folder. The files stay as they are, and all history is lost, so it is only right for a repository you created by mistake.
Practice
Challenge: create a repository in one step. You are inside fee-tracker-copy. Move up one folder, create a repository named library-app with a single command (no mkdir), move into it, and check its status.
What does Git print? Before you open the solution, decide what git init library-app reports, and what git status says about a repository that has no files.
Show solution
cd ..
git init library-app
cd library-app
git statusInitialized empty Git repository in /home/navika/projects/library-app/.git/
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)git init library-app created the folder and the repository inside it. You still have to cd into it before you work.
Clean up. Go back up with cd .. and delete the practice folders you don't need with rm -rf fee-tracker-copy library-app. Deleting a folder deletes its repository with it.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git init | Make the current folder a repository |
git init <directory> | Create a folder and make it a repository |
git init -b <name> | Create a repository whose first branch has the given name |
git clone <address> | Copy an existing repository, history included |
git rev-parse --show-toplevel | Print the top folder of the repository you are in |
ls -a | List files, including hidden ones such as .git |
rm -rf .git | Remove the repository and keep the files (the history is lost) |
Was this tutorial helpful?