How to create a new git project

Zhao Li
Zhao Li
Sep 5, 2018 · 3 min read

Problem

Before you can start using git, you need to create a git repository first.

A repository is where all of your project code is stored and accessed.

Solution

There are two common ways of creating a git repository. The method you choose depends if you are creating a brand new repository or if you are creating a clone of an existing repository.

Initializing

To initialize a brand new repository on your local development machine, you use the command git init.

$ mkdir my_project # create the directory for your project
$ cd my_project # the git command is executed within the project directory
my_project$ git init # initialize a git repository
Initialized empty Git repository in .../my_project/.git/

You have just created your git repository on your local development machine.

Cloning

To clone an existing repository on to your local development machine, you use the command git clone.

For this example, we will be cloning this git repository: https://github.com/django/django

$ git clone https://github.com/django/django.git
Cloning into 'django'...
remote: Counting objects: 420546, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 420546 (delta 0), reused 1 (delta 0), pack-reused 420540
Receiving objects: 100% (420546/420546), 176.36 MiB | 4.58 MiB/s, done.
Resolving deltas: 100% (304991/304991), done.
Checking out files: 100% (6005/6005), done.

You have just cloned django’s git repository (that was hosted on GitHub) on to your local development machine.

Verifying Git Repository

No matter which method you chose to create your git repository, you should have a .git subfolder within your project folder.

# if you used git init
$ ls -la my_project
total 0
drwxr-xr-x 3 user staff 96 Sep 4 17:27 .
drwxr-xr-x 12 user staff 384 Sep 4 17:27 ..
drwxr-xr-x 9 user staff 288 Sep 4 17:27 .git # your entire repository is stored in this .git folder
# if you used git clone
$ ls -la django
total 240
drwxr-xr-x 28 user staff 896 Sep 4 17:37 .
drwxr-xr-x 13 user staff 416 Sep 4 17:36 ..
...
drwxr-xr-x 12 user staff 384 Sep 4 17:37 .git # your entire django repository is stored in this .git folder
...

This also means that if you want to blow away git but keep the code in its current form, then you just need to get rid of the .git folder.

You can also use git status to verify your git repository.

# if you used git init
my_project$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)
# if you used git clone
django$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
# if you are not in a git repository (i.e. no .git folder in current directory
$ git status
fatal: not a git repository (or any of the parent directories): .git

Notes

After creating your new git project, you will want start saving and loading code.

It is helpful to use a graphical user interface to follow to see the affects of each action as you’re following along: https://medium.com/@zhao.li/how-to-ease-gits-learning-curve-67e3ae5f5f58

The last thing to note is that all of your git repository’s configurations are stored in .git/config.

# if you used git init
$ cat my_project/.git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
# if you used git clone
$ cat django/.git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/django/django.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master

References

Zhao Li

Written by

Zhao Li

In case it helps others…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade