Skip to content

git init#

When you have a directory with code in it, or perhaps you're starting from nothing, you can make that directory a valid Git repository. Remember that a Git repository is simply any directory with a .git directory inside of it.

Let's try this now on an empty directory:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$ ls -la
total 0
drwxr-xr-x  2 michaelc  wheel   64 23 Apr 12:27 .
drwxrwxrwt  8 root      wheel  256 23 Apr 12:27 ..

$ git init .
Initialized empty Git repository in ~/git-stuff/.git/

$ ls -la
total 0
drwxr-xr-x  3 michaelc  wheel   96 23 Apr 12:27 .
drwxrwxrwt  8 root      wheel  256 23 Apr 12:27 ..
drwxr-xr-x  9 michaelc  wheel  288 23 Apr 12:27 .git

One moment we had an empty directory, and after a simple git init ., we have a .git directory. Now we can add files to this directory. Let's add a text file:

1
2
3
4
$ nano hello.txt

$ cat hello.txt
Hello, world!

I just used nano to edit a new file and put Hello, world! inside of it. You can write whatever you like inside of the file, however.

How does Git now see this file? That's where the git status command comes in, which we'll look at next.