Introduction to Git for version control 
Tags: beginners, tutorial, git
Published: 2020-05-04 10:08:42

There are many version control systems available but what we are going to discuss is “git” a distributed version control system but first what is distributed version control system(DVSC)? So, in DVSC files or code is distributed among each developer working on the project. They all have their copy in their working directory. The master copy is placed on a central server where each developer will request to the central server to update the code base or will request to update his local folder, if the central sever is updated already. 


#1.Local and Remote repository

These two terms are very important part of git local and remote repository. Repository is a directory or a folder where your version control system stores all the versions and information related to the project. Local Repository is the one which will be in your personal computer in your working directory. And remote repository is the one which will be on the central server or any remote server.  

#2.Basic git work-flow
A simple workflow for git is drawn in the below diagram.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/u63zk3wbdlruxs5ktl20.jpg)

Here we have developers working on a same project, each have their own distributed working copy of the project. They all have their own local repository and a remote repository where they all push changes or pull changes from remote repository to local. Each developer make changes to files, add those files to git to track (staging area), commit all changes to local repository and push those changes to remote repository and this cycle repeats but if your remote repository is ahead of the your changes. which mean another developer have make changes to remote repository now it won't allow you to push local changes to remote repository.In this case we have to first update our local repository with the new changes that has being push to remote repository by some other developer working on the same project. so we will first pull changes, update our local repository and the push our changes to remote repository. 



#3.Git basic operations
these are some of the basic git commands to get us started in the journey of versioning of our code. we will use command line to perform these operations..
1. Initialize:
this word means "start of an operation". this is the first step towards getting to know git. We will initialize (create a local repository ) to start versioning our project. <mark>"git init"</mark> is the command used to create a local repository. After executing this command a <mark>.git</mark> hidden folder is created in our working directory which will store every information related to versioning and all metadata of our project.

2. Add: 
after creating a local repo now we add all files to staging area where we add file to git and git will start tracking the files. To add files to track we use <mark>git add .</mark> this command is used to add all files. <mark> git add file_name</mark> is used to add individual files by calling file name. To check if our files are tracked or untracked we use a command <mark>"git status"</mark> to check if our file is track or untracked in staging area so, we can proceed to next step.

3. Commit:
as of now we have added all changes to our git and git has started to track down the changes now its time to submit(commit) those changes to our local repository. <mark>git commit -m 'some changes about file'</mark> this command is used to commit every changes in file with a customize message.

4. Push:
after completely everything locally, now its time to push (<mark>'Local to remote repository push'</mark>) all those changes to central server. <mark> git remote add origin remote_repo_url</mark> after executing this command our local repo will connect to remote repo but our changes are still not published on the remote server.so, to publish changes we use <mark> git push -u origin master </mark> now this will push all changes to central server. Not be confused by the term 'origin' and 'master', origin is the name of remote repository and the master is  default git branch. Branching is also a feature of git but it is not relevant to this for now as we are discussing basic git operations. 

5. Pull:
lets say you and your friend is working on the same remote repository.He push few changes to your remote repository and now you need those changes push by your friend to your local repository. <mark> git fetch </mark> this command will fetch those changes but won't add those files to local repo until you merge those changes to your local repo by executing <mark> git merge </mark> command. Their also another command which is <mark> git pull </mark> this will fetch and merge changes all at once. 


This was completely a very basic introduction to git, Will publish more about git and its advance features.