Create Your First Git Repository
A Git Repository is a project which is tracked and managed by Git. Git tracks the changes by storing snapshots of our project versions in a .git file. In this tutorial, we will learn how to create a Git Repository and also learn how to clone or copy other repositories. Let's open Git Bash and start working with Git.
Before creating our first repository we should configure our name and email address. This helps Git to keep track of which user made commits to a certain project if multiple developers are working on the same project. We only need to do this once after installing Git.
$ git config --global user.name "Your Name"
$ git config --global user.email "firstname.lastname@example.org"
Git init is going to be the very first command that we will always use when working with Git. It is used to initialize a new Git Repository and tell Git to track any changes that we make in that repository. We only need to do this once while creating a new Git Repository. To convert an existing directory into a Git repository we need to move to that directory and just run the git init command directly. Let's take a look at how to use the git init command.
- To initialize the current working directory as a Git repository:
$ git init
- We can also use this command to directly create a new Git repository instead of navigating to an existing one. Just pass the name of the new directory that you want to create and that directory will be created and initialized as a Git repository.
$ git init <directory-name>
- We can also use --bare flag along with git init to create a new bare repository. A bare repository is a repository that does not contain the working directory and can only be used as a Remote Repository. We cannot edit or make commits to a bare repository and it is to be used only as a central directory. The use of such a repository is to avoid the possibility of overwriting changes that exist in non-bare repositories.
$ git init --bare <directory-name>
After using the git init command a hidden .git subdirectory is created in our project and this .git subdirectory is what turns our local project into a Git repository. This file stores all the data that is required to track the changes that were made to our project. To maintain a reasonable size of this file, Git uses complex compression mechanisms to store the changes in small chunks. We can see this hidden .git file by running ls -a command in our terminal. Let's understand the git init command with the following example.
- First, we navigate to the directory that we want to initialize:
- We use the git init command to initialize it as a Git Repository:
- Now, lets use the ls and ls -a commands to see the hidden .git file:
- As we can see, the ls command does not return any files as the .git file is hidden but the ls -a command which is used to the only view the hidden files returns the .git file as output. This folder is kept hidden from view to avoid accidental deletion or modification of this subdirectory which can corrupt the version control data that is stored there and we may also lose our project. We can also navigate to .git folder and see what all is stored there.
The next command that we will be using after initializing a Git repository is git add command. The git init command lets us create and edit files in the working directory but it is the git add command that allows Git to track a particular file and check for any modifications to it. The changes made to the file are not recorded by Git yet. This command is a way of telling Git that we want these changes in our files to be recorded permanently in our next commit and so Git starts tracking these changes. The following illustration tries to explain these three stages.
The staging area is an intermediate stage between modifying files and committing those modifications permanently. The presence of the Staging Area helps developers to segregate and group related files that must be committed together. This also makes sure that the commits are atomic in nature and easier to understand and allows us to rollback only a part of our project to the previous checkpoints instead of reverting back our entire project.
Using the Git Add command
Before starting to use this command we need to understand that the git add command is somewhat redundant in the sense that whenever we alter a file we need to add that file to the staging area again even if it was already added previously. But at the same time, this makes the entire process more robust and avoids confusion. With the git add command we have the choice to add a single file, multiple files, or the entire content of the directory to the staging area.
$ git add <file-name>
- To add multiple files with a single command:
$ git add <file-1> <file-2> <file-3>
- To add all the files in the project directory:
$ git add .
One of the major use cases of Git is to let other people collaborate with you on your projects or build something on top of an existing project. For this to happen we often store our local Git Repositories on the cloud using Git hosting services offered by sites like GitHub and BitBucket. The is where git clone command comes into action. This command helps us to copy or clone an existing remote repository into our local repository and start contributing to that project. Whenever we clone a repository we get the current version of it along with all the previous commits that were made by someone else. This is possible because of the distributed model on which Git works.
To clone a remote repository to our local system, we need the URL to that remote repository. Let's take a look at how to use git clone command.
- To clone a remote repository to the current working directory:
$ git clone <remote-repository-url>
- We can also tell Git where to clone these files bypassing the directory name.
$ git clone <remote-repository-url> <directory-name>
- As discussed above, whenever we clone a remote repository we get all the previous commits. We can limit the number of previous commits that are cloned by specifying the "depth" in the clone command. The example shown below will clone only the last 4 commits that were made to the project.
$ git clone -depth=4 <remote-repository-url>
Let's see an example of cloning a remote directory from GitHub. Let's clone the repository present at https://github.com/codebreaker003/Bubble-Sort.
- First navigate to the repository where you want to clone and use the git clone command with the given repository URL.
- The above code gives the following output:
- We can navigate to the cloned repository and see the files that were cloned.
- Using the git log command we can see the history of all the commits that were made to that repository.
In this tutorial, we learned how to configure Git when you first start using it by using the git config command. We learned how to create a Git repository using the git init command. We also saw how to use the git add command to add files to the staging area and prepare them for committing. We learned how to use the git clone command to clone or copy projects from remote repositories to our local system. I hope you found this tutorial helpful and learned something new. In the next tutorials, we will learn about more Git commands and how to use them.