How to create a simple Git repository on your local machine

What is Git?

It's a file version control system that helps manage and track changes made to stored files.  Software developers use it primarily. But it can be used to keep track of changes in other scenarios.

Say you have a Word document.  Saving it to disk is a good habit to get into when you are working on a document.  Source control adds another layer of history.  It's like a time machine of sorts.  At the start of the week you can make changes to the document.  Save it and record the changes in Git.  Then make new changes on another day.  After a few days worth of changes stored in source control using Git, you can compare changes from today to those made at any point previously.

This is a powerful and useful tool for software developers.  We've been using Git and other source control tools for decades.  Git, however, has become a global standard out of the sea of competition.  Without question, Git is used by most software developers.

Git is a distributed source control system.  Which simply means that each computer with access to a source repository will have it's own copy.  The internet is nothing if not a great way to share files.  So, Git can be hosted on the web and anyone with access can clone, or get a copy of the source repository.  There are many ways of hosting Git repositories.  The variety and competition for the ultimate hosting platform continues while Git is pretty much decided as the engine that underpins them.  GitHub, Azure DevOps, Bitbucket are a few of these hosting providers for Git.  Those are available on the internet.  But what if you want to have your own "host" on your laptop or home network?  It turns out that is very easy to set up.

Here are the steps to create a local Git repository and a few starting commands to edit and manage files with it.

Step 1: Installation

  • Navigate up to https://git-scm.com/downloads and install from the recommended "Latest source release" download link for your operating system.  (For simplicity, the rest of this post will cover a Windows installation.)
  • Next, validate the installation.  Open a command prompt and type the text below.  It should report the version of Git that was installed.
git --version

Step 2: Create a local repository on your computer

  • Create a folder at the root of your C drive named, "localRepository.git".
  • Open a command window and change directory to the new folder.
cd c:\localRepository.git
  • Then create and initialize an empty repostory with the following command.
git init --bare --shared=all

Step 3: Clone your new local repository into a working folder

  • Now create a different folder at the root of your C drive named, "repos".  This is going to be the working folder where the repository created above is copied, or cloned to.
  • From the command prompt, change the current directory to your new "repos" directory.
cd c:\repos
  • Type the following to clone your repository.
git clone "C:\localRepository.git"

Step 4: Create a new file to add to source control

  • Change the directory in your command prompt to your new cloned folder.
cd c:\repos\localRepository
  • Create a file with the name and extension "ReadMe.md" file in the cloned folder.
  • Edit the file content with a text editor and type in some text like, "This is a readme file." Then save the file normally.
  • Now add and commit the file to the repository using the following commands.
git add --all
git commit -m "First commit"

Step 5: Store all your new committed changes to your repository

  • At this point, your file is saved and a commit in a local database on your local computer is ready to be saved in the central repository. To "push" the commit with changes to the main repository, use the following command.
git push

Now what?

At this point, you can make changes to the ReadMe.md file using any text editor.  As long as that file stays in the cloned Git repository folder, any editing to it can be committed and pushed in order to track changes.  While it does seem like extra work and using a normal save is enough, the extra file management features are very powerful.  Git provides the ability to store a copy with change history in another location away from the files you are working with.  In addition, it gives a way for more than one person to work on a single file and merge everyone's changes back together. (Something not covered in this post).  Also useful is the ability to compare versions of one or more files at any two points.  The best part of using Git in software development is peace of mind.  When used correctly, it is difficult to lose work, and easy to go back to any older version that works better if a mistake is made.

This was meant to be an over simplified introduction for a non-developer audience.  These are some easy steps to get set up for more complex concepts and to practice using Git safely on your own computer.  I've shared these basic steps over the past few years among seasoned developers who are new to Git but who have experience with other source control tools.  Hopefully it will help someone get started.  (See what I did there?)