Introduction to Git
What is GIT ? and why use it ?
GIT is an open source distributed version control system.Nowadays Industry life as well as a college life projects have working multiple developers.Therefore GIT is imporatnt to maintain what changes have made by developers and also using GIT we can create multiple branches and do parelel coding with team members and merge into one project.In Additionaly GIT provide feature like developer can go back to the older version of the project.
Let’s try to use GIT
I’m going to explain GIT using an examples .I think it is the easiest way to understand this.
Download GIT to your PC
Please follow below link to install GIT.
After installing the Git You can verify that whether GIT correctly install or not using following command in CMD :
git --version
Create local GIT repositary
First create folder called GIT-Example
in your machine as your project folder.Then open the cmd by giving folder location and give command as follows :
git init
git init
command means adds a local git repository to the project.
create a text file called example.txt
and write on it somthing you like.I give follows :
My First GIT Experience
then save file.
Steps for commiting the code
First you have to add the file into git
git add example.txt
Then use below command to commit the file :
git commit -m "Intial Commit"
you can give any name as commit name.
Status
It shows information about what files are there to commit .Use below command to see the status :
git status
GIT Branches
By default GIT commits are going to the master branch.When many developers are working on the same project that is very hard to maintain .In that case we need multiple branches to parallel developments.
Create Branch
Create new branch called “demo” using the below commad :
git branch demo
Then you have to switch to the demo branch .Use the below command:
git checkout demo
you can lisout all branches using the following command:
git branch
Commits into new Branch
Now modify your example.txt file adding somthing .I add following
Mr First GIT Experience Add Somthing Using Branch
Then add and commit the code. Use the following commands:
git add example.txtgit commit -m "Demo Branch commit"
This commit goes to the demo branch.So that master branch has one commit and demo branch has one commit.
You can see commit history by using below command:
git log
Merging to master
Now we have to merge demo branch in to the main branch of master.first of all we have to go back to the master branch.Try follows:
git checkout master
then run the below command to merge :
git merge test
still we have working on local repository.Now we have to push code into remote repository so that we have to go to github .Use followig link :
Still, If you don’t have an account please simply create it.After creating the acoount system navigates the home page.
Then you click on New .
Then give repository name as GIT_Example and click “Create Repository”
Them it will create the remote repository and open page like this .
You have to copy the highlighted URL and go to the cmd prompt and type the following command:
git remote add origin [repository url]
Push to remote repository
Push command from local repository to remote repository,you have to give following command :
git push -u origin master
after entering this command it will ask your GIT username and password .You have to provide that credentials.Then code will push into the remote repository.
You can refer following :This is my cmd prompt output
These are the basic commands about GIT.I hope this will help for the beginners.Thank you for reading this article.!!