Understanding Git, The Easy Way

understand-git-easy-way

Every developer should know the GIT to manage their project easily. If you are working on a collaborative project in which multiple team members are working, it’ll be a good idea to use GIT.

I’ve experienced on several occasions, that when the first time GIT is introduced to new team members or teams who don’t have experience or exposure to it, there’s generally a sense of unnecessary jitters. With this blog post, I’ll try to explain it in as simple way as possible.

Git is a version control system which can track the changes of your files and store the multiple versions of a file based on the changes. Before understanding the GIT, we need to know what was the need of GIT and what problem is it solving?

The Problems

Tracking of revision history of a file was a major issue. If a developer changed the file 100 times, He can’t check the previous code written on the files.

There was no easy way to rollback the previous changes if code becomes buggy.

No way to share code in real time. Writing code with collaboration was difficult. If somehow it is managed, code review becomes painful.

Merging files from different collaborators was difficult. One has to manually check each and every line of code to merge.

The solution for the above problems is Version Control System (VCS). GIT is one of the VCS. Actually GIT is a Distributed Version Control System. (if you’re interested in history of it- It was created by Linus Torvalds in 2005. It was originally created for the development of Linux Kernel. It’s written in C, shell, Perl, Tcl and Python)

Any version control system is a way to manage files and directories. It tracks and manages file changes so that you can recall specific versions later. It shares a codebase to a collaborator without any trouble. It can merge the code with collaborators’ code without any conflict.

There are dozens of VCS available like Mercurial, SubVersioN, Perforce, BitKeeper. You can decide as per your liking but I like GIT better.

Understanding GIT

Before diving deep into GIT, We should know what Git repository is. The repository or GIT repository tracks all changes made to files and directory in your project, building history over the time. Git repository resides in the ‘.git/’ directory inside your project.

Before diving into the GIT, we should set up GIT as per our platform. Click here to know how to install GIT as per your platform.

Creating the Repository

You need to create a directory in your local machine. Open it in the terminal and run the command to create a new git repository.

git init

Existing Repository

If you already have a local or remote repository. Run the below command to checkout the repository

For local repository

git clone /Path-to-repository

For remote respository

git clone username@host:/path-to-repository

Git Flow

Git has three trees, Working Directory, Index and HEAD. Working Directory contains your actual files, Index serves as a staging and HEAD contains the reference of your last commit. Commit is something which you are saying to Git, Hey, I have changed something now it’s the final changes made to the file.

git work flow

Adding files and committing

You can add changes to git Index by below command

git add 

If you want to add all the changes to Index, use below command

git add *

Once all the changes report to Index, you can commit the changes and move them to the HEAD. Always type the message which relates to your changes, It helps you to rollback and see what has changed in a particular commit.

git commit -m "<Your commit message>"

After committing the changes, HEAD holds the changes of files. It stills stays in your local machines. Now it should go to the remote repository.

Push

After you have all the commits in HEAD, push these commits to the remote repository using below command. You can place the branch name in which you want to push the commits however if you do not specify the branch, it will push the commits to your current branch.

git push origin <branch>

Branches

Branch in Git is simply a pointer to your commits. You can separate your development or features using branches. ‘Master’ is the default branch in Git. You can create a new branch for any feature and then can merge it to master branch

For creating a branch

git checkout -b <branch-name>

For switching the branch

git checkout <branch-name>

For deleting the branch

git checkout -d <branch-name>

You need to push the branch which you have created, otherwise collaborators will not know about your branch.

Updating Repository

When you are working in a collaborative environment, you sure want to get updates of the code changes at the remote repository as all are working on the same project.

To get the update execute below command. It will update your local branch to the new commits.

git pull

If you want to merge another branch to your active branch, simply execute below command. It will merge all the changes to your current branch.

git merge <branch>

Git always tries to auto merge changes. But there may be changes you will get conflicts from updates. You need to update code manually in the conflict files. After resolving the conflicts, you need to tell git that all the conflicts has been resolved using below command

git add <filename>

Replace Local Changes

If you want to replace local changes which you have made to a file you can run below command. It can be useful if something went wrong after changing the code in the file.

git checkout -- <filename>

If you want to delete all the changes you have made and point to the latest commit of the remote repository. Execute below command

git fetch origin
git reset --hard origin/master

Useful Commands

//Check the differences between two branches. It can be helpful before merging.
Git diff <source_branch> <target_branch>

//To check the logs
git log

//check log of a particular author
Git log --author=<author_name>

//to check the files which have changed
Git log --name-status

Hope you understand the git with simple series on commands. Please do let me know if you want to know more.

Leave your comment