How to create a Git repository
Why create a new git repo?
Git is a powerful, distributed version control tool that allows users to:
- Track changes to files and folders.
- Maintain an extended history of commits.
- Perform isolated development in local branches.
- Share code with any other networked users.
The core Git artifact that enables these features is the repository. After you install Git, to take advantage of these features, you must either:
- Create a new Git repository of your own, or
- Clone an existing repository from another user.
How to create a new Git repository
Follow these steps to create a new Git repository on your personal computer:
- Create a new folder for your project.
- Open the folder in Git BASH.
- Issue the git init command to create the new Git repo.
- Note the creation of the hidden .git folder in the project.
- Add files and folders to your project.
- Routinely stage files and create commits.
The aforementioned steps will set up your project and lead to your first commit. However, it is specifically the git init command that creates the Git repository.
Git init example
The following image shows the commands needed to accomplish several tasks in Git, including:
- Create a new Git repository with git init.
- Add a new files to the folder.
- Stage the file to the Git index with the git add command.
- Perform a commit with the git commit command.
After these commands are run, the new Git repo will contain a single commit that contains a single file named alpha.html.
Create a Git repo in an existing project
A folder does not need to be empty to issue the git init command and create a new repository.
If a folder already contains files, here’s what you do:
- Simply issue the git init command in the root of the folder.
- Issue the git add . command to stage all files in the project.
- Perform a git commit.
After the first git commit operation executes, a snapshot of all of the project files in their current state will be stored in the newly created Git repo.
Clone a remote repository
One of the most compelling features of Git is the ability to share code with others.
If you want to work on an existing repository that is hosted on a service such as GitHub or GitLab, you simply obtain the unique address of the repository and perform a clone.
For example, each GitHub repository contains a URL that uniquely identifies the repo.
Copy the GitHub URL to your clipboard, and bring the remote repository down to your local machine with the git clone command.