The Git Handbook

The Git Handbook

Understanding the Basics

Git is a Version Control System, which means it basically tracks changes a project/ file goes through over time, so a team can work on different parts of the project knowing that if they mess up they can always go back to their previously saved state.

dvcs.jpg The 2 types of Version Control Systems are Centralized and Distributed.

In a Centralized Version Control System, you have to be connected online, so any commits made were directly updated on the main server. In a Distributed Version Control System, everyone has a local copy of the file system that's on the server. The benefit is they can make commits offline and then push it online of their own choosing, which means you don't have to be online to make changes and it helps developers make changes asynchronously from any time zone.

Once changes are made for a given version we create a snapshot of this new modified version, this snapshot is called commit i.e saving a particular file in time, so each commit is an updated version of what was changed plus the older context that was not modified.

3 areas of git.png The process of making changes to a file and committing it occurs in 3 stages

  1. Untracked Area/Working directory: A file is stored here when modifications occur on a file, it may be creating, updating, or even deleting a given file

  2. Staging Area: Here we track all changes made to the previous versions. If any changes are done to a file in the staging area we move it back to the untracked area.

  3. Committed Stage: here we basically create a snapshot of the entire filesystem i.e the parts that were updated as well as the unmodified parts. (A,B,C) -> (A1,B,C); where A is the changed file.

The Git Commandments

git --version   
#To check if git is present
git config --global user.name "your username"
git config --global user.email "your email"
git config --global -l 
#To check configurations
git init 
#if you want to add version control to a filesystem you currently are in.
#this command also creates a hidden file called .git
#which tracks all changes of a  given file.
git status
#checks which files are In which stages
git add .
#add all modified files from untracked area to staged area
git commit -m "message"
#move staged file to commit and make a snapshot of modified files from the
# unstaged area and untouched files
git restore --staged filename
#move files from the staged area but leave changes untouched
git log
# log of all changes made in a file
git log --oneline --decorate --graph -all
#use the above to get a better visual understanding of the commits
git branch <branchname>
#create a branch of the existing filesystem
git branch -a
#lists all the branch this filesystem has
git checkout <branchname>
#move head to a given branch you wish to modify
git merge <branchname>
#move head to the given branch you want to merge the branch name with
git branch -d <branchname>
#delete the given branch
git checkout -b <branchname>
#creates and switches to a new branch
git checkout -b hotfix-branchname 
#make quick changes
git merge hotfix-branchname
#makes a hotfix to the given branch called branchname
git clone https://github.com/handle
#clones the given repo input correct Github id and password
#a good practice is to make a branch as soon as you clone a repo
git pull origin master
#it fetches the files and updates if any changes were done on the main repo.
#It basically syncs the files
git push
# pushes changes done in the local repository to the remote repository.