Learn git

In my previous blog, I explained about how I setup dev environment (git-bash) in my system. In this blog, I am going to explain about basics commands in git.

Version Control System

Version Control System (VCS) is a software system that allows multiple software developers to share the documents and work together and maintain a complete history of their work.

Distributed Version Control System

Distributed Version Control System or Centralized Version Control System (CVCS) uses a central server to store all files and enables team collaboration. But the major drawback of CVCS is its single point of failure (central server).

In a DVCS, each user has a complete local copy of the repository on his local computer. This copying process is typically called cloning and the resulting repository can be referred to as a clone.

Git

Git is a distributed Version Control System (DVCS) for tracking changes in computer files and coordinating work on those files among multiple people.

Why is git useful?

  1. easily store, retrieve and update source codes
  2. security of files using SSH keys and passphrases
  3. DVCS
  4. sharing of files and work
  5. Being distributed, you don’t need to be connected to a server all the time, just work locally and push when you need to.

Basic Commands

  1. git-init

git-init command is used to create an empty git repository or to reinitialize an existing one.

From the above example, you can see that a .git repository is created inside a directory intern. If no directory is specified, then by default, a .git repository will be created in the current directory itself.

2. git status

The git status command is used to obtain a summary of tracked files, untracked files, staged files, updated files, etc.

3. git add

git add command is used to store a snapshot of the specified files temporarily in the staging area. It allows you to modify files and stage them. This can be repeated till you are satisfied with your changes. To add everything in your directory, use git add . command.

Let first create a new file named git_learn.txt

Now, check the status of the file.

From the status obtained, we find that git_learn.txt is an untracked file.

Now, add the file to the git repository using git add command.

Now, again check the status of the file.

When we check the status of the file, we find that git_learn.txt becomes a staged file.

4. git commit

git commit command is used to store a snapshot of those files (which are temporarily stored in the staging area) permanently in the Git repository. These files are immutable (i.e., they cannot be changed). A new commit object is created, when you commit changes to your git repository.

From the screenshot, we find that git_learn.txt is permanently stored. If you leave off the -m option, then you will be put into an editor to write the message yourself.

Now, again check the status of the file.

The file is not displayed, which indicates that git_learn.txt is permanently moved to the git repository.

5. git remote

git remote add command is used to add a new remote. This command should be executed in the directory where your repository is stored.

It takes two arguments:

  • A remote name
  • A remote URL

A remote named origin is created at the specified remote URL.

Using git remote -v command, we can find the remote name, URL, and the type of operation to be performed.

6. git push

git push command is used to push the commits that is made in the local repository to the remote repository.

It takes two arguments:

  • A remote name, for example, origin
  • A branch name, for example, master

Login using github account when a prompt window appears.

The remote repository will be updated like the following image.

7. git pull

git pull command is used to fetch the changes made in the remote repository, and then merges it with the local work. The syntax for git pull is similar to that git push.

It takes two arguments:

  • A remote name, for example, origin
  • A branch name, for example, master

Now, create a new file named all_about_git.txt in the remote repository.

Now, use git pull command.

If the local repository is already updated with the remote repository, then the output will look like

8. git clone

git clone command is used to copy the repository of another user to your local computer. Once cloned,

  • A new local repository is created
  • A remote named origin is created, pointing to the URL you cloned from
  • All of the repository’s files and commits are downloaded there
  • The default branch (usually called master) is checked out

I have cloned a repository named local-development from hasura github account.

9. github account with a configured SSH key

SSH key is used to protect you from unauthorized access. With SSH keys, if someone gains access to your computer, they also gain access to every system that uses that key. To add an extra layer of security, you can add a passphrase to your SSH key. The syntax to generate a SSH key is

ssh-keygen -t rsa -b 4096 -C “email_id”

I have referred the following sites:

For git add & commit

  1. http://www.vogella.com/tutorials/Git/article.html#gitterminlogy
  2. http://classic.scottr.org/presentations/git-in-5-minutes/

For git remote

For git push

For git pull & clone

For github account with a configured SSH key

In my next blog, I will explain how I setup Hasura local development in my system.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.