Master Git in 30 Days

Day 03: Creating a Repository

To start using Git, the most important thing is to have a Git repository first. But where does this repository come from? This article will introduce various ways to create a repository.

To create a repository, in fact, you have many options, for example:

  • Create a local repository on your local machine
  • Create a shared repository on your local machine
  • Create a remote repository on GitHub or other Git platforms

Regardless, you always need a repository. We will explain each as follows:

Creating a Local Repository on Your Local Machine

Let’s first open the Git Shell tool of GitHub for Windows. This tool is actually a Windows PowerShell interface but with some Git-related environment variables and command prompt settings:

image

After opening, it will default to the %USERPROFILE%\Documents\GitHub folder. This is the default project root directory of GitHub for Windows, which means all projects cloned from GitHub through GitHub for Windows will be placed in this directory by default.

So we can directly create a new directory here to serve as our “working directory”. We can enter the command mkdir git-demo to create the directory, then use cd git-demo to enter that directory.

Since this is an empty directory and doesn’t contain any Git repository, we can use the git init command to create the repository. By default, the repository will be placed in the .git directory under the working directory.

The complete operation steps are shown in the following figure:

image

After creation, you should notice something different in the command prompt of Git Shell - it adds a highlighted [master] after the current path. This prompt will display various states of your current working directory. If you can understand it, it’s a very useful source of information that can help you avoid typing many git status commands to query the current state of the working directory when using command-line tools to operate Git. This part will be specifically explained in the next article.

Creating a Shared Repository on Your Local Machine

A shared repository refers to creating a Git repository without a working directory. This situation is more common on Linux operating systems because Linux operating systems are usually used by multiple people on the same Linux host. Although this can also be used on Windows operating systems, our development environment is mostly in the Windows Client environment, and it’s rare for multiple people to share a computer.

To create a shared repository, you can use the git init --bare command to create it, as shown in the following figure. You can see that when git init is used with the --bare parameter, it creates all Git repository-related files and folders in the current directory. You must pay special attention that this folder cannot be used directly for development purposes. It can only be used to store Git-related information. In most cases, you should not manually edit any files in this folder. It’s best to operate through git commands.

image

Since this is a “pure repository without a working directory,” the shared repository also has an alias called “bare repository.”

I must emphasize again that Git is “distributed version control,” and everyone has a complete repository. That is to say, when you want to create a “working directory,” you must first get the content of this “bare repository” back. At this time, you must use the git clone [REPO_URI] command to “clone” a copy back. Through the git clone process, not only will a working directory be automatically created, but this “bare repository” will also be completely cloned back. This cloning process is like a “complete backup,” copying all version records in the Git repository, all versions of files, etc., all data completely back. The complete command operation process can be referred to in the following figure:

image

In practice, there may be several ways to use “shared repository” or “bare repository”:

  • Collaborative development on a machine used by multiple people, where most people can be given read-only permission to this “bare repository folder,” and only one person or a few people have write permission.
  • Some people put bare repositories on Dropbox to sync this bare repository with their multiple computers.

Please note: Although the .git directory under the “working directory” is also a “repository,” the repository under the working directory also contains some index information of the working directory, recording state information of the working directory. This state information will not appear in the “shared repository,” which only contains version information (i.e., Git object information).

Creating a Remote Repository on GitHub or Other Git Platforms

In fact, “remote repository” and “shared repository” are almost the same. The difference is only that “shared repository” mostly uses direct file access, while “remote repository” usually uses protocols such as SSH, Git protocol, HTTP, etc. to “remotely” access the Git repository. Other usage methods are basically the same.

The following demonstrates creating a repository through GitHub and cloning the repository back to local:

  • First log in to GitHub, then create a new repository

image

  • Set the relevant information for the GitHub project and create the repository

image

  • After creation, it will prompt you on how to get the project or import your local existing project to GitHub

image

  • As shown above, we can click “Set up in Desktop” in the Quick setup section. Your computer will automatically open the GitHub for Windows tool and automatically clone this repository back and create a working directory.

  • You can also copy the URL of the remote Git repository, then execute git clone [REPO_URL] to clone a copy back.

image

Today’s Summary

The above are three ways to create a Git repository. Let me reorganize the Git commands and parameters learned today:

  • git init
  • git init –bare
  • git clone [REPOSITORY_URI]