Get started Bring yourself up to speed with our introductory content.

How to create a local repository with the git init command

The first thing to do when you start to work with Git is either clone a remote source code repository or create your own new local repository.

Regardless of the method you choose, a repository will serve as your home base for any future projects.

In this git init command tutorial, we will demonstrate the latter and how to successfully create a new local repository.

Git init example prerequisite

Before we begin, make sure you have Git installed on your local machine.

For this tutorial, I created a new folder named "my-local-repo." Within that folder, I created three files that will be added into the new Git repository when we create it.

You can accomplish this easily, by issuing the touch command three times with the Git BASH shell inside the newly created folder.

[email protected]: ~my-local-repo$ touch index.html
[email protected]: ~my-local-repo$ touch
[email protected]: ~my-local-repo$ touch style.css
Git init files
You will need files to add to the repo in this git init command example.

Issue the git init command

After you create these files, the next step is to create the source code repository through the invocation of the git init command.

[email protected]:~my-local-repo$ git init
Initialized empty Git repository in /home/cuttlefish/my-local-repo/.git/

Note that after you call the git init command, you'll need to inform the tool of your name and email address.

[email protected]:~my-local-repo$ git config --global "Cameron"
[email protected]:~my-local-repo$ git config --global "[email protected]"

Add files to the Git index

With these steps completed, you can now add the three files created earlier to the Git tracking system -- also known as the Git index -- with the git add . command. Note that a space and period after the add command is required here. It's a commonly overlooked switch needed to run the command.

[email protected]:~my-local-repo$ git add .

You can choose to individually add files to the Git index by name calls, but with a period after the git add . command, Git knows to add every new or changed file it can find to its tracking system.

Perform a git commit

Finally, once you have added the files to the index, you can perform your first commit.

[email protected]:~my-local-repo$ git commit -m "My first commit"
Git init success
A commit proves that the git init command was issued successfully.

Once the commit is registered in the DVCS, you can confirm that the git init command successfully created the repository. Then, you can continue to add files to the index and subsequently create branches, issue commits, perform reverts and reset the HEAD on your local Git repository.

By the way, note that this git commit isn't particularly well worded. Read up on some git commit message rules to learn about these practices early so they becomes habitual.

Furthermore, I created the three files in this example before I issued the git init command. I could have invoked the git init command first and then used the touch command to the files, but the order isn't important. You can choose the preferred route in your repository creation.

View All Videos