git init & git clone
A repository is a project folder that Git tracks. There are only two ways to get one: create a new, empty repository for a project you are starting (git init), or copy an existing repository from somewhere else, usually GitHub (git clone).
In this lesson you will do both, look inside the hidden .git folder that makes a folder a repository, and learn the options you will use most often.
Creating a repository with git init
Priya is building a website for her family's sweet shop in Jaipur. She makes a project folder, moves into it and runs git init:
mkdir sweet-shop
cd sweet-shop
git initInitialized empty Git repository in C:/Users/priya/projects/sweet-shop/.git/
That is all. The folder looks the same in File Explorer, but Git has created a hidden folder called .git inside it. Ask Git about the state of the project with git status:
git statusOn branch main No commits yet nothing to commit (create/copy files and use "git add" to track)
Git says three useful things: you are on a branch called main (because you set init.defaultBranch in the previous lesson), there are no commits yet, and there is nothing to save because the folder is empty.
You can run git init in a folder that already has files, too. Git does not touch your files; it only starts watching the folder. You can also pass a folder name, and Git creates the folder for you: git init sweet-shop.
What is inside .git?
The .git folder is the repository. It holds every commit, every branch and all the settings for this project. Your project files next to it are only the current working copy. List it with ls -A (show hidden files) in Git Bash, macOS or Linux:
ls -A .gitHEAD config description hooks info objects refs
Here is what the important parts do:
objectsstores the content of every saved version of every file, compressed.refsstores branches and tags: small files that point at commits.HEADrecords which branch you are on right now.configholds settings for this repository only (the local level).hooksholds sample scripts that can run automatically, for example before each commit.
You will look at objects and refs in detail in How Git Stores Data. For now, the rule is simple: never edit files inside .git by hand, and never delete it, because deleting .git deletes the entire history and leaves only the current files.
Copying a repository with git clone
When a project already exists on GitHub, you clone it: Git downloads the whole repository, including its complete history, and sets up a link back to where it came from. GitHub keeps a tiny practice repository called Hello-World under its mascot account, octocat, which is perfect for trying this:
git clone https://github.com/octocat/Hello-World.gitCloning into 'Hello-World'... remote: Enumerating objects: 13, done. remote: Total 13 (delta 0), reused 0 (delta 0), pack-reused 13 (from 1) Receiving objects: 100% (13/13), done.
Git created a folder named after the repository, downloaded 13 objects (commits, folders and file contents), and checked out the newest files. Move into it and look around:
cd Hello-World
git log --oneline
git remote -v7fd1a60 (HEAD -> master, origin/master, origin/HEAD) Merge pull request #6 from Spaceghost/patch-1 7629413 New line at end of file. --Signed off by Spaceghost 553c207 first commit origin https://github.com/octocat/Hello-World.git (fetch) origin https://github.com/octocat/Hello-World.git (push)
You have the full history on your computer. git remote -v shows that Git saved the address you cloned from under the short name origin; later you will push and pull through it. This old repository still uses master as its main branch, which is common in projects created before 2020.
Useful clone options
Give a second argument to choose the folder name:
git clone https://github.com/octocat/Hello-World.git helloCloning into 'hello'...
For a very large project where you only need the latest files (for example to build it once), a shallow clone downloads only the most recent commits:
git clone --depth 1 https://github.com/octocat/Hello-World.git shallow
cd shallow
git log --oneline7fd1a60 (grafted, HEAD -> master, origin/master, origin/HEAD) Merge pull request #6 from Spaceghost/patch-1
Only one commit came down. Shallow clones are fast, but you cannot see or search older history, so use a normal clone for anything you plan to work on.
Two more options you will meet later: --branch <name> checks out a branch other than the default right after cloning, and --recurse-submodules also clones any submodules the project uses.
HTTPS or SSH addresses
GitHub shows two kinds of clone address for every repository:
| Kind | Example | Logs in with |
|---|---|---|
| HTTPS | https://github.com/priya-sharma/sweet-shop.git | Browser login through Git Credential Manager, or a personal access token |
| SSH | git@github.com:priya-sharma/sweet-shop.git | An SSH key you add to your GitHub account |
Public repositories can be cloned over HTTPS with no login at all. To push, you must prove who you are; you will set that up in GitHub Basics.
Common mistakes
- Running Git commands outside a repository.
fatal: not a git repository (or any of the parent directories): .gitmeans you are in the wrong folder.cdinto the project first. - Cloning inside another repository. Cloning into a folder that is already tracked creates a confusing "repository inside a repository". Clone into a normal folder such as
C:/Users/priya/projects. - Running
git initafter cloning. A cloned folder is already a repository with history. Runninggit initagain is unnecessary. - Deleting
.gitto "start fresh". That throws away all history. If you really mean it, make sure you have no work you need first.
Key points
git initturns the current folder (or a new named folder) into an empty repository.- The hidden
.gitfolder holds all history and settings; never edit or delete it by hand. git clone <url>downloads a full copy of an existing repository and names the sourceorigin.- Add a folder name to rename the clone;
--depth 1makes a fast shallow clone. - GitHub offers HTTPS and SSH addresses; either works for cloning public projects.
Exercise
Create a folder called cricket-scores and turn it into a Git repository. Check its status. Then, in a different folder, clone https://github.com/octocat/Hello-World.git into a folder named practice and list its commits on one line each.
Show solution
mkdir cricket-scores
cd cricket-scores
git init
git status
cd ..
git clone https://github.com/octocat/Hello-World.git practice
cd practice
git log --onelinegit status in the new repository reports "No commits yet". The clone lands in practice because you passed that name as the second argument, and git log --oneline shows the three commits.
Quick Quiz
1. What does git init create?
git init only creates the .git folder; nothing is committed until you add and commit files.
2. After git clone, what name does Git give to the repository you cloned from?
Clone saves the source address as a remote called origin.
3. What does git clone --depth 1 <url> do?
A shallow clone with depth 1 is fast but contains no older history.
4. What happens if you delete the .git folder?
All commits, branches and settings live in .git; only the current files remain.
Learn Git with a mentor
Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
