Creating And Using GIT Repository

A GIT repository is where GIT stores all our project files. A GIT repository can be created in two ways. First, directly create a new GIT repository and store project files in it. Second, clone an existing GIT repository to create a new repository.

Creating a New GIT Repository

We can create a new repository by using the following command in the GIT Bash:

git init

Steps to create a new repository:

  1. Open GIT Bash
  2. Navigate to the directory where you want to create the new GIT Repository
  3. Use command 'git init' to create a new repository.

Shown below is an image where we have created a GIT repository inside 'local repo' directory. When a repository is created, GIT adds a '.git' folder where it stores all the important internal files.


Adding/Committing Files to GIT Repository

We have successfully created an empty GIT repository. Now we can start using it by adding files to it. The files and folders we added are said to reside in the working directory. Now to commit the files into the repository we first need to move it to the staging area.

We can use the 'git add' command to move the files from working directory to the staging area. To commit the files we can use the 'git commit' command.

git add *
git commit -m 'Initial Commit'

The image shown below adds a folder called 'myproject' and two files 'Doc1.txt' and 'Doc2.txt' inside the folder, to the repository. We can use the -m option with the git commit command to add a commit message. In our case the commit message is 'Initial Commit'.


Note: We can use the '-a' option with git commit command to skip the staging part. Git will automatically stage the files. This is only applicable to files already tracked.

Check Status of Files in Repository

GIT provides the 'git status' command which can be used to determine the state of files in the repository.

git status

The image below shows 'git status' command's output. The first time we run the command the working directory is clean. We modify a file and run the command again, hence the output shows that there are changes that are not yet staged for commit. 


We can use the git diff command to see the changes that are not staged.

git diff

Removing Committed Files From Repository

To remove a committed file from the repository we need to delete the file from the working directory and then run the 'git rm' command which will stage the removed file. Later we can run the git commit command to delete the file from the repository.

Alternativley, we can directly run the git rm command which will directly remove the file from the working directory and stage the removed file.

git rm <filename>
git commit -m <message>

The image below shows how to remove a file and commit it to the Git repository.


View Commit History

Git provide the 'git log' tool to help users to view the commit history of a file. It is a very powerful tool which helps to keep track of the users who have made changes to the files in the repository.

git log

The image below shows how to use 'git log' command. Notice the '-3' option passed with the command, it is used to limit the output to last 3 commits. Using the 'git log' alone will output the entire commit history.


Remote Repository

Git is a distributed VCS, thus users maintain their own copy of the central repository. The central repository can be placed in a remote machine and each user can access it when needed.

The remote repository is preferably created as a bare repository. It is different from a regular repository; it doesn't have a working directory and hence cannot contain a checkout copy of the source files. It is primary meant to be used as a centralized repository where all users can push there changes.

To create a bare repository we can use the following comand:

git init --bare

We have created a bare repository in a folder "remote" as shown in the figure below:


Pushing Files to the Remote Repository

To push changes to the remote repository we should use the 'git push' command. But the local repository must be associated to a remote repository before we can push changes to it. To associate a local repository to a remote repository we can use the 'git remote' command.

git push
git remote

The image below shows how to associate a local repository to a remote repository and push changes to the remote repository.

Steps followed:

  1. We use 'git remote' to check if the local repository is associated to a remote repository.
  2. We add a remote repository to it using the command 'git remote add <name> <remote_location>'
  3. Now the 'git remote' command shows a remote repository associate to our local repository.
  4. We use 'git push' to push the files to the remote repository. We need to use 'set' option to set the upstream branch in the remote repository where to push the files. This has to be done only for the first push. In this case we have set the upstream branch to master.
  5. Now we can use 'git push' without set option. However, nothing gets pushed this time since there are no changes.

Cloning a Repository

To clone any repository we need to use the 'git clone' command.

git clone

The image below shows how to clone a repository:


Notice that after the clone is created we have changed a file, committed and pushed to the remote repository. Since this repository is created by cloning a remote repository, it is already associated with a remote repository. Hence we do not need to explicitly set the remote repository and the branch to push the files.

As we have pushed some changes to the remote repository we can now pull the changes to all the local repositories that are associated with it. To pull the files from a remote repository to a local repository we should use the 'git pull' command.

git pull

The image below shows that the changes are pulled to our local repository. This is the same repository to which we earlier associated the remote.


To unassociate a remote repository from the repository we can use the below command:

git remote remove <remote_repository_name>


git remote remove myremote


Git Branching Support

Branching is a major part of any modern version control system. Git provides a unique branching feature which sets it apart from other version control systems. This article covers the various aspec...

View Article

Git Overview

GIT is an open source distributed version control system that emphasizes on speed and efficiency. This article covers some of the basics of the GIT VCS and also discuss about the GIT config tool.

View Article

Tagging in GIT

Tagging allows us to mark a specific point in the commit history or snapshot. A tag is typically used to mark a project release. This article shows how to create tags in Git.

View Article