Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Practical Git: Create local repos with git init

    Trevor MillerTrevor Miller

    Getting started with Git requires you to add a .git folder to your project on your machine and then set up that folder to point to a remote repository. In this lesson, we walk through using git init and git remote to do this; we use GitHub for this remote repository as an example, but keep in mind that any Git repo hosting service will work.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Here, we're inside of a folder called Utility Functions. If we take a look at what's inside, we have a couple of files. We want to make this folder into a Git repo. The first thing we need to do is to run git init. This will initialize an empty Git repository, as it says here.

    If we output what's in this directory, we can see that there is a new .git folder at the root. If we take a look at this .git repo, we can see that there's a lot of folders and files in here. Although we're not going to get into the details of this in this lesson, the important thing to know is that all of the Git settings and information is located in this .git repo at the root of our project.

    Since all of our Git information is in this .git folder, if we ever want to remove Git from our project, all we have to do is delete that folder from our project. If we were to remove .git, and take a look at our project again, we can see that Git is no longer a part of our project, including all of the information that it's gathered about our project.

    If we try to run a Git command, like git add for example, it says that this is not a Git repository. Once we run git init, then we can run the command, and it no longer throws an error. Now that we have a Git repo set up locally, it won't be much use to us unless we can sync it with a repo on the Internet somewhere. That could be on GitHub, or Bitbucket, or any number of Git repo hosting services.

    In my case, I'm going to use GitHub. What I've done here is, I've logged into my GitHub account. I will go to the new repository button. It's asking for our repository name. I usually use the folder name of my project. In this case, it's Utility Functions.

    I will copy that and paste it here in the repo name. We'll give it a description. We can go down and say Create Repository. This takes us to a new repo page, which gives us a URL that has a .git extension. We're going to copy that URL because this is the location of our remote repo.

    Back in our command line, we're going to say Git Remote Add, and then the name of our remote, which will be Origin. We'll paste in the URL that we copied. If we run that, we now have a remote set up on a repo. To verify that it was added correctly, we can type git remote.v. We can see that the URLs were added correctly.