Are you a newbie, thinking of where to start learning git!! This is a good start for you.

Head First into Git

Shriram Navaratnalingam
Loops Blog
Published in
6 min readFeb 24, 2019

--

Basically Git is a tool that is used to manage changes to files and directories. Technically it’s called as a Version control system .

Git is different from other similar tools and following are the things I feel which makes it stand out…
1. It always allow rollback to see which results were produced in which versions.
2. It has a mechanism of implicitly notifying things when any of your work conflicts with another, so it’s almost impossible to overwrite another work.
3. It can synchronise work done on different machines by different team members.

Git information storing mechanism..How it works?

Normally projects controlled by Git has two sections.One is the files that we directly manipulate , and the other one is the additional details that Git records regarding the project. The combination of both these sections form the repository.

As I mentioned earlier all the additional details recorded by Git is stored in .git which is stored in the main parent/root directory of the repository. Since information recorded are maintained in a very precise way, we should not manipulate anything in .git

Just for your better understanding,if you have a directory called/user/home and assume that there is a repository called sliit, which also has a sub-directory called data. So here where do you thing information recorded by Git is stored?
Yes you’re right; It’s stored in /user/home/sliit/.git

Checking the state of the repository…

Normally when we are using the Git we want to check the list of files that have been modified since we last saved the changes made. To do so, we need to run the command git status.

How to know that I have made the change?

In Git we have a logical concept called staging area. In a very high level placing files in the staging area is like keeping things in a box, while committing refers to the putting that box that contains files we already placed into it in the mail. We can add or take things out of the box often as we need until we mail that box with files, once mailed we can not make further changes.

We can also compare a file to what it currently holds and what it holds we it last saved. To do so we can utilise the command git diff filename.When we run the command git diff without any filenames it’ll show all the changes made in our repository, Similarly we can use the same command to compare files in the same directory using git diff directory.

How to begin saving changes?

There are two steps in making changes in Git repository:

  1. First as mentioned in the above diagram we need to add files to the staging area.
To all a particular file to staging area
> git add filename
To add all the files to staging area
> git add -A
or
> git add .

How do I commit changes?

After all the changes are made to the files in staging area now we can commit it,for that we can utilise the command git commit. It saves all the files that are put into staging area as one unit

As we make commit changes we must provide a log message to specify why we made that change.To keep things simple, we can use single line log message as we make our commit to local repository,like this:

git commit -m "Any commit message"

There are some instances we we mistype a commit message, in such instance we change it using the --amend flag.

git commit --amend - m "new message"

Viewing entire repositories history

We can use command git log to view the log of entire project's history. Log entries specifies the most recent manipulation on top.

commit 0430703687386195993bac9c21j5fv2ccfb5j1056d
Author: ShriLingam23 <xxxx@gmail.com>
Date: Sun Feb 24 13:42:26 2019 +1023

Each commit has a unique ID for the commit called a hash. It also specify who made the change, when it’s done and log message for the commit made.

As we run git log, Git automatically show single screen of output at a time. We can use space bar to go down a page and 'q' key to quit.

Viewing a specific files history

Sometimes we want to inspect the changes only in particular files or directories. We can inspect it by using
git log path, path indicates the location to the specific file or directory.

The log includes the information such as changes made to that file, date and time files were manipulated.

Proper Standards that need to be followed for log messages

We can follow a simple and most usual way of giving a commit message when committing for a smaller change as follows
git commit -m "message"

But it’s a good practice as well as improve the readability of the change you made when a third party inspect your work, even it’ll be more convenient to our own future use as well.

There is also an another way of specifying the log message, when we perform a commit. That is if we run git commit without -m "any commit message", Git immediately launches a text editor with a template as follows:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Your branch is up-to-date with 'origin/master'.
#
# Changes to be committed:
# modified: sliit_git.txt
#

All the lines that starts with # are comments won’t be saved into the local repository. They give an info of what we’re supposed to do and what files we’ve changed. And your message should be specified above those comments and it can be as much as we want.

I would also like to share a set of summarised basic set of commands that are used in git

Check git version installed in computer

> git --version

Personalise setting, so that git knows who’s making changes

> git config — global user.name ‘ShriLingam23’
> git config — global user.email ‘xxxxx@gmail.com

Before proceed ,make git pointing to our working directory

right click -> select ‘git bash’

Create local repository in our working directory

> git init

Additional — to create a file from command line

> touch “fileName.extension”

check for any changes made

> git status

Add the changed files to staging area


> git add fileName.extension
// or to all files that are changed
> git add .
> git add -A

Commit all files in staging area to local repository

> git commit -m ‘commit message’

Restore all files from local repository to our working directory

> git checkout — .

Clone a project(repository) from GitHub to computer

> git clone repositoryURL

Note : This will just copy the files in remote server to local repository(created automatically)
Therefore we don’t need to create the local repository for folder
ie: git init

Since we clone from a repository; when we push, it will be pushed to repository from where we clone

To check the URL/address of the remote repository from git command

> git remote -v

Note : Here ‘origin’ keyword refers to the remote repository address
To set the remote repository URL/address -> ‘set-url’

> git remote set-url origin remoteRepositoryAddress

When we first connect to remote repository 1st time( ie : we didn’t clone), we need to ‘add’ remote repository url

> git remote add origin remoteRepositoryAddress

Push the files in master branch of local repository to master branch of remote repository,if we don’t specify master all branches in local repository with matching branches in remote repository will be pushed

> git push origin master

To get the latest code from remote repository to local repository and also sync it with working directory

> git pull origin master

Fetch vs Pull
Pull will download the specified branch from origin(remote repository) and also sync our working directory accordingly
But Fetch will just download the entire remote repository,then using merge we can merge specific branch in download repository to our current branch

Fetch from remote repository,that is only repository will be downloaded and not won’t be placed to local repository, placing in local repository happens when we merge

> git fetch origin

Merge the master branch in download repository to our current branch, this will sync our working directory as well

> git merge origin/master

This bring a conclusion to the learning of Git basic flow!!!

Thanks for reading my blog….See you soon in the next blog

--

--