Welcome to tutorial on Git. Here you will learn how to Create a Repository, and how to clone or copy other repositories. A Repository is a project that is tracked and managed by Git. Thus, Git tracks changes by storing snapshots of project versions in a .git file.
Git Config
There is a need to configure our name and email address before creating our first repository. This assists Git to keep track of which users made commits to a certain project, in the case where multiple developers are working on the same project. Check out the example below, as we only do this once after installing Git.
$ git config --global user.name "Your Name" $ git config --global user.email "[email protected]"
Git Init
The Git init will be our first command which we will use whenever we are working with Git. As it is used to initialize a new Git Repository and instruct Git to track any changes that we make in that repository. Therefore, we can do this while creating a new Git Repository.
In other to convert an existing directory into a Git repository, there is a need to move to that directory, then run the git init command directly. Check out the example.
To initialize the current working directory as a Git repository, as shown below.
$ git init
This command can also be used use this command to directly create a new Git repository, rather than navigating to an existing one. Thus, we can simply pass the name of the new directory that we want to create, as the directory will be created and initialized as a Git repository.
$ git init <directory-name>
In addition, we can use the –bare flag along with git init to create a new bare repository. A bare repository is referred to as a repository that does not contain the working directory, but can only be used as a Remote Repository. However, we cannot edit or make commits to a bare repository, because it is to be used only as a central directory. Check out the example below.
$ git init --bare <directory-name>
In addition, the use of such a repository is to prevent the possibility of overwriting changes that exist in non-bare repositories.
.git File
Now, after making use of the .git init command a hidden .git subdirectory is created in our project and this .git subdirectory is responsible for turning our local project into a Git repository.
Therefore, this file stores all the data that is required to track the changes that were made to our project. In other to maintain an appreciable size of this file, Git makes use of complex compression mechanisms to store the changes in small chunks. You can see this hidden .git file by running the Is-a command in our terminal. Check out the following example on the git init command for a better understanding.
First, we will navigate to the directory which we want to initialize:
$ cd DIRECTRORY
Then we will use the git init command to initialize it as a Git Repository:
$ git init
Then we will use the ls and ls -a commands to see the hidden .git file:
$ ls -a
From the above example, the ls command does not return any files as the .git file is hidden but the ls -a command which is used to only view the hidden files returns the .git file as an output. As such the folder is kept hidden from view to avoid accidental deletion or modification of this subdirectory that could corrupt the version control data that is stored there and we may also lose our project. check out the example below, as we can also navigate to the .git folder and see what all is stored there.
$ cd .git $ls
Git Add
The git add is another command used after initializing a Git repository. Although the git init command allows us to create and edit files in the working directory, the git adds command allows Git to track a particular file and check for any modifications on it. However, the changes made to the file are not recorded by Git until now. This command is a way of instructing Git that these changes are needed to our files are to be recorded permanently in our next commit and thus, Git starts tracking these changes. The diagram below illustrates these three stages.
Staging Area
In the modification of files and committing of these modifications permanently, the Staging Area is an intermediate stage between that. The existence of the Staging Area helps developers to segregate and group related files that must be committed altogether. It also ensures that the commits are atomic and simple or easier to understand, as it allows us to roll back only a part of the project to the previous checkpoints instead of reverting our entire project.
Using the Git Add command
In using the Git add command, we need to first understand it, and why it is redundant, that is whenever we alter a file we need to add that file to the staging area again, even if it was added previously already. However, at the same time, the makes the entire process more robust and avoids confusion. As such if the git adds command is used we have the choice to add multiple files, simple files, or the whole content of the directory to the staging area.
To add a single file, do this:
$ git add <file-name>
To add multiple files with a single command, do this:
$ git add <file-1> <file-2> <file-3>
To add all the files in the project directory, do this:
$ git add .
Git Clone
Amongst other important uses of Git is to let other people collaborate with you on your projects or build something on top of an existing project. in order to make this happen, we mostly store our local Git Repositories on the cloud by making use of Git hosting services offered by sites like GitHub and BitBucket. Thus, at this point, the git clone command comes into action, as it helps us to copy or clone an existing remote repository into our local repository and start contributing to that project.
Therefore, if we clone a repository we will get the current version of it along with all the previous commits that were made by someone else. However, this is quite possible, because of the distributed model on which Git works on.
Check out the following examples on how to use the git clone command. Note that to clone a remote repository to our local system, we need the URL to that remote repository.
To clone a remote repository to the current working directory, do this:
$ git clone <remote-repository-url>
We can also tell Git where to clone these files by bypassing the directory name, do this:
$ git clone <remote-repository-url> <directory-name>
Just as discussed above, if we clone a remote repository we get all the previous commits. But, we can as well limit the number of previous commits that are cloned by specifying the “depth” in the clone command.
Check out the example below as it shows how only the last 4 commits were made to the project.
$ git clone -depth=4 <remote-repository-url>
Now, take the example of cloning a remote directory from GitHub. Let’s clone the repository present at https://github.com/msg2gk/Codeignitercoremodel. It is summarized below:
First, navigate to the repository that will be cloned and then use the git clone command with the given repository URL.
$ git clone https://github.com/msg2gk/Codeignitercoremodel
The code above gives the output below:
cloning into codeignitermodel..... .................................. ..................................
Also, we can navigate to the cloned repository, to see the files that were cloned.
Now, by using the git log command we can see the history of all the commits that were made to that repository.
$ git log