DEV Community

Cover image for Introduction to Git for version control
Akbar Alam
Akbar Alam

Posted on

Introduction to Git for version control

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

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. "git init" is the command used to create a local repository. After executing this command a .git 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 git add . this command is used to add all files. git add file_name is used to add individual files by calling file name. To check if our files are tracked or untracked we use a command "git status" 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. git commit -m 'some changes about file' 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 ('Local to remote repository push') all those changes to central server. git remote add origin remote_repo_url 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 git push -u origin master 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. git fetch 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 git merge command. Their also another command which is git pull 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.

Top comments (3)

Collapse
 
usvma profile image
Usama Munir

Brilliant job in explaining the command of git. However, it is actually more inclined toward the audience who are somewhat familiar with the git already. Overall great job with your first attempt!

Collapse
 
akbaralam profile image
Akbar Alam

Thank you usama means a lot.

Collapse
 
_yourabbu profile image
Abutalib Haider

Was looking for a basic explanation, found exactly that.