Learn Git tool in 15 mins - Tutorials with examples


Git Tutorials Basics

git basics with examples

Git is open source and distributed version control tool to track changes of a code or documents. Nowadays it is very popular among opensource code base. During application development, we have several questions.

How do you track changes of a code based?

How multiple people work on the same code or module?

How to do you maintain several versions of the code base?

How do you rollback code changes to the previous version if any issue in production?

How do multiple people merge the code?

How code released to production?

Answers for the above questions are the features of GIT.

In development, Developers writes a code base in their local system. Once everything is ready, Commit code changes to a remote repository. Then other developers can able to get the latest code changes

Git Features

1. Opensource - It is free to use under GPL license. You can download and install client and server track your code

2. Performance is good - Developer does changes on the local environment. No need for internet to work. when you are done with local changes, You will commit changes to remote repository.

3. Distributed tool - Multiple teams can work on the same code base.

There are three repositories

the repository is set of files and directory and their history of each file.

Remote repository:- It is a code on central repository or server where multiple users are able to work by cloning into the local repository

Local repository;- It is code on your local system which cloned from a remote repository. Each user has a copy of the code base.

user work on local changes for his work.

Staging area:-

It is a temporary area where the user adds the files to the repository. Files can be of different states files can be of tracked, untracked, committed, staged types

git tutorials with examples

Install and Setup

on windows First, download window installer from here.click on the installer and follow the steps. Once done successfully, You re able to access git tool.
On Ubuntu/Debian
Installation is very easy using apt-get package. It will install the latest git command

apt-get install git  

on Linux
Use yum command to install git tool

  
yum install git  

once git installed on your respective operating system. you can check installation by using version as below.
 To check Version of a git tool

C:\>git --version  
git version 2.15.0.windows.1  

One time Setup
Once git is installed successfully, You need to do some global settings and one time step, Please do this via command line as below

git config --global user.name "Kiran"  
git config --global user.email "code@cloudhadoop.com"  

repository git commands

We will see various examples of the git command.

Initialize empty repository

 This is starting command for a new project creation in git. git init command initializes the git empty directory. It creates the .git folder in the empty directory.

  
C:\gitcommands>git  init  
Initialized empty Git repository in C:/gitcommands/.git/  

Add files to the empty repository 

This will add files to staging area/working tree. This is a temporary area for keeping files before commit changes to the remote repository

git add {list of files or directory by space}  

Commit changes to the repository 

Once files are added, you need to use this command to commit changes to the local repository. Provides user message with -m option

  
git commit -m "Message"  

repository status

Using status command, You can get each of the file statuses, files status are added, Modified etc..

  
git status  

Branch example

Branch is a separate repository for the main repository which multiple teams work on.

  
git branch branchname // Create new branch  
git branch -a  list of all branches  
git branch -d delete branch  

Cloning a repository

the clone is used to get the copy of the remote repository into your local repository. It just a clone of your remote copy

  
git clone {gitrepourl} {folder}  

gitrepourl is git repository URL folder is optional if the folder is not provided.

configuration example 

To find a list of available configurations

  
  
C:\gitcommands>git config --list  
core.symlinks=false  
core.autocrlf=true  
core.fscache=true  
color.diff=auto  
color.status=auto  
color.branch=auto  
color.interactive=true  
help.format=html  
http.sslcainfo=C:/Program Files (x86)/Git/mingw32/ssl/certs/ca-bundle.crt  
diff.astextplain.textconv=astextplain  
rebase.autosquash=true  
http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt  
http.sslbackend=openssl  
diff.astextplain.textconv=astextplain  
credential.helper=manager  
user.email=abc@abc.com  
core.repositoryformatversion=0  
core.filemode=false  
core.bare=false  
core.logallrefupdates=true  
core.symlinks=false  
core.ignorecase=true  

get global settings

 git config --global --list

Commit history usage 

Every commit tasks you do in local will record changes .git folder. You can see history using below command

  
git log  

gitignore file 

In your project, It is good practice to have the .gitignore file. In code, the code compiles and generate temporary folders like a target in maven java project, dist folder for javascript applications. These folders are not required to commit to the repository In that case, Please add this folder to a .gitignore file. This folder changes will not be committed as well as not shown with git status command each line in this file represents

  
cat .gitignore  
/target/*  

checkout example

checkout is used to work with multiple branches or other developer committed code.

  
git checkout revisionid - check the changes of commit id  
git checkout branchname - checkout existing branch  
git checkout -b branchanem - Create and checkout new branch  

remote example

git remote command is used to connect between remote repository with the local repository for names reflecting origin is the name of the original branch

  
git remote add origin {remote-name} {git-url}  
B:\Workspace\angularspree>git remote -v  
origin  https://github.com/aviabird/angularspree.git (fetch)  
origin  https://github.com/aviabird/angularspree.git (push)  

pull command usage

pull is used to get the latest changes of a remote repository. Once this command is fired, This gets the content from the remote repository to the local repository

  
git pull branchname remote-url  
git pull origin master  

push command usage

This is reverse of git pull command. This push changes from local repository to remote repository. This is final step once you added/committed changes

  
git push origin master  

Similar Posts