Master Git (Version Control) in One Video From Scratch

Master Git (Version Control) in One Video From Scratch

Video Tutorial

Getting Started

Git is a Distributed Version Control System Free and Open Source Fully Designed to Handle Project Collaboration across people (Usually Developers), it is very flexible and easy to use alongside big Git hosting services (Github, Bitbucket) some of them are free for open source only projects and others are completely free.

First, Make sure that Git is installed on your machine (Linux Distros and Mac come with Git preinstalled) and for windows install it from Here also you gonna need a terminal to work with you can use CMD or Integrated VsCode Terminal.

Why Learn Git

if you still confused about why you should learn Git and master Version Control System Well since your vision is about teamwork either with companies or your own team or even working as a freelancer require you to learn Git for making the work simple as well as very easy to deploy, also Git is not only used for development process rather than this it is very important for deploying Web Apps which makes it super fun to deploy and maintain your application on web hostings.

Git Repository

So let’s say that you are working on a simple PHP Blog project and you want to integrate Git into your it, well the very first thing is you need to initialize a repository.

#Make sure to cd under your project's root directory
git init

Check your git Repo status

git status

This will show you the files and directories that exist under your working directory and since it is showing with the Red color that means that the file(s) changed (have modification) and since this is our first time looking into this it is pretty obvious.

Git keeps track of any changes on the files that you add to the staged area (Tracking System) so for any file, you want to add to the Git repository use the add command and specify which file(s)

#Specify the file name
git add "index.php"
#Or add all the available files/Directories
git add .

Ignore Files in Git

Ignoring Git files under the current working directory is very important since sometime you would have configuration files for your project (.env file holds localhost information) that you don’t want to deploy (upload) to the remote repository, for that you can add new file to your current working directory name it .gitignore and under it add the files/directories you want to ignore

//Git Ignore file
.gitignore
.env
node_modules

if you try to run the Git status command after adding the gitignore into your project you wouldn’t see the files you have specified as ignorable.

Git Commits

Commits on Version Control Systems are the Most important thing related to your project, it is the way to save your changes (either added a feature or fixed a bug).

Commits have a unique hash code that references a single commit and a message describes the main role of the commit, first, you need to add changes to the stage area and then commit them

#Commit changes (Save Changes)
git commit -m "Initial Commit"

Now if you would want to take a look at the commit history

#Full Commits History
git log

Also, it shows you the currently commit being pointed by the HEAD, the HEAD actually means your current working commit.

Git Branches

Branches are Commit Containers (Holds Commits) by default when you commit for the first time and no branch is preset Git create a master branch that by default holds your commits, you can create multiple branches to organize your work and code (for ex: create a separate branch for fixing a bug then merge the fixed bug code with your master branch which holds your main code project).

when you create a branch by default it points to the latest commit which means that all the commit available from the first commit till the last one (when the branch is created) after that if you tried to commit under the new branch the other branches won’t know about your commits and changes you have done so far on the branch.

If everything goes fine and you have been able to fix the bug, merge your work and delete the temporary branch.

#Create a new Branch (Points to the Latest Commit and holds all the previous changes)
git branch bugFix

To switch from the master branch to any other branch

#Use the Checkout Command to switch among branches 
git checkout bugFix

Also, the checkout command can be used to detach the HEAD from branches to commits or other branches (The HEAD pointer is your main push and fetch point)

Now if you try to commit under the branch and fix the bug you can merge back your progress with the main branch (master Branch). First, make sure to switch to the branch you want to merge to in this case it is the master branch

#Switch to master branch
git checkout master

Then specify which branch to merge from (Merging basically means grabbing all the commits and add them to the current branch)

git merge bugFix

Now you can Delete the bugFix branch since you don’t need it anymore (its Job is Done)

#-d option tells it to delete branch 
git branch -d bugFix

Git References 

Let’s say that you have a lot of commits and branches, so managing all of those together can be taught sometimes but using references helps you a lot to move on the git history tree (Tree of Commits).

So what we usually do to Detach the HEAD from one branch and move it another branch or to a commit is we either use the branch name or the commit Hash code in order to refer to them, references allow you to move under the tree depending on branches pointing position.

You have the master branch points to the latest commit this is being made by default as well as the HEAD and you have another branch called newFeature pointing to the previous commit (last but one commit) you can move the newFeature branch to the latest commit using Refs since the HEAD and master branch are pointing to it (you only need one thing in order to move along the history tree)

git checkout newFeature HEAD^

This will move the newFeature branch to the latest commit.

Git Remote (Push and Fetch)

So the main purpose of using Git is to be able to take your local repository into the cloud (Remote Repository) well, for this you need to setup a remote repository in this case we are going to create an empty repo on Github since it is probably the most famous Version Control hosting among devs also it is very is to create an account and a repository on.

So go to Github and open up a new account and create an empty repository after creating it you will get Repository URL make sure to copy since you gonna need it for pushing changes from your local repository.

Under your Git repository, you can add multiple Remote repositories (For ex: one repository for development the other for production)

git remote add origin "REPO_URL"

The above command will add new origin repository, now you can push your changes to it depending on your commits if you have committed new changes since the last push Git going to be smart about this and only push the newly committed changes or all commits depending on your remote repo.

git push -u origin master

Using GIt push you can push your changes (Commits) to the remote repo you specify using the -u option alongside the branch you want to push in this case is the master branch (Main Branch), pretty Simple.

If this is your the first time pushing to Git repository Git will ask you about your Github Username(Email) and password in order to be able to push to the Remote Repository.

If you are collaborating with someone else or a group of developers there might be a newly committed change since your last commits (Where you don’t know about this new changes) so for grabbing the changes from the remote Repo into your local Repo use the Fetch command or Pull Command to Fetch and Merge at the same time

#Only Grab changes from remote repo
git fetch REPO_URL

Or Fetch and Merge to a local branch

#Fetch and Merge changes from Remote repo into local branch 
git pull REPO_URL master

This is the simple commands that you need to know in order to start collaborating on projects using Git.

What’s Next

You still need to practice what you have learned, I advise you to set up a test repo and start playing around with commits, pushes, and branches that will make you understand how actually things are being done by Git

Hopefully, you could learn some Git Commands from this Tutorial.