Header Ads Widget

All about Git - Cheat Sheet - Basic Part - I


WHAT IS GIT?
In today's world, the most widely used modern version control system is Git. Git is a mature, actively maintained open source project originally developed in 2005 by Linus Torvalds, the famous creator of the Linux operating system kernel.

SETTING UP A REPOSITORY:
Git uses the below command to create a new Git repository.
git init
Executing this command will create a .git subdirectory in the project root and transforms the current folder into a git repository.

The below command will create a new folder named directory and initialize the repository by creating .git subdirectory
git init

BARE REPOSITORY:
The below command will create the bare repository.
git init --bare
The --bare flag will create a repository which is having no active working directory. This is very important. You should always consider creating your central directory as a bare directory and from there create all your branches (non-bare directory) where the developers will work. Once the work is done, developers will merge their code from branches (non-bare directory) to central repository(bare directory). Since the central repository is bare directory and does not have any active working directory, there is no chance of editing the files and committing them.

CLONING AN EXISTING REPOSITORY:
The git clone command copies an existing Git Repository. Cloning always creates a remote connection called origin pointing back to the original repository. This makes it very easy to work with the central repository.
The below command will clone the repository "https://github.com/suddeb/TestRepository.git" to the folder TestRepo in my local machine.

git clone https://github.com/suddeb/TestRepository.git TestRepo

GIT CONFIGURE:
The git config command will configure the Git installation from the command line.
Below command will configure the Author name to be used for all commits in the current repository by the current user.
git config --global user.name "Sudipta Deb"
Below command will configure the Author email to be used for all commits in the current repository by the current user.
git config --global user.email "sudipta.deb@gmail.com"

SAVING CHANGES:
The git add command adds a change in the working directory to the staging area. Basically, it will inform git that you want to include this change in your next commit.
Below command will add the file "test.txt" from working directory to staging area making it available for next commit.
git add test.txt
Here is the screenshot of my command prompt:

So if you see once I changed the test.txt file, git status command is telling me that I have changes not staged for commit. After that, I have used the command git add test.txt to add the file to staging area. Once done, issues the command git status is telling me something different that I have now changes ready to be committed.

GIT COMMIT:
The git commit command commits the staged snapshot to the project history. Below is the command which will commit the previously staged file test.txt into local repository. Don't be confused? I will explain what is local repository very soon.
git commit -m "My First Commit"

So once your commit is done, if you issue git status it will tell you that you have nothing in the staging environment to be committed.

WHAT IS LOCAL REPOSITORY?
In Git, snapshots are always committed to local repository. This is one of the fundamental difference between Git and SVN/CVS. In SVN/CVS, you will always commit against the central repository, whereas in Git you don't need to interact with central repository until and unless you are ready to do that. All your commits are stored in the local repository. Consider staging area as the buffer between your workspace and project history, similarly each developer's local repository is the buffer between their contribution and central repository.

There are some other fundamental differences between Git and SVN. Where SVN tracks the differences of a file, Git's version control mechanism is based on snapshots. Below picture will help you to understand the differences between SVN/CVS(Centralised Version Control System) and GIT(Distributed Version Control System)
In my next post, I will talk about Git Stash. One of my favorite command and you can play with this command. Till then, enjoy version controlling. Provide me your feedback. Thanks.

Post a Comment

0 Comments