Git What,Why and How ?

git What is git ?

Git is a open source distributed Version Control System(VCS) originally developed in 2005 by Linus Torvalds the creator of the linux kernel and is still being actively mantained.


Why use it ?

While working on a project git tracks the changes you make to files, thus keeping record of what has been done, so you can easily revert your project to some specific point or change should you ever need to. Git also makes collaboration easier, allowing changes by multiple people to all be merged into one source and maintaining projects efficiently. It does so by storing your changes in commits.


How to use git ?

First you need to install git. For windows visit the link and download and install it, others just open the terminal and type the commands.

Windows : https://git-scm/download/win
MacOs : $ brew install git
Debian/Ubuntu : $ sudo apt install git
Arch based : $ sudo pacman -S git
NixOS : $ nix-env -iA nixos.git

  • Configure git
    $ git config --global user.name "username" #set git username globally
    
    $ git config --global user.email <usermail@example.com> #set git usermail
    
    $ git config --global core.editor <editor> #set preferred editor for git
    
    $ git config --list 	#check git config
    


  • General commands
    $ git init 		#initialize the repo.
    
    $ git add . 		#stage all changes to be commited
    
    $ git commit 		#commit changes with a commit msg + summary
    
    $ git commit -m "msg" 	#commit changes with a commit msg.
    
    $ git commit --amend 	#amend last commit locally.
    
    $ git remote add origin <url.git>   #add url where to host the repo
    
    $ git merge <b1>    #merge changes from 'b1' branch to current branch
    
    $ git mergetool 	#handy tool for solving merge conflicts
    
    $ git cherry-pick <commit-id> 	#add commit with given id from a remote(added to the project) to current branch
    
    $ git pull 	#pulls latest changes from default branch(branch/remote can be specified)
    
    $ git push 	#push latest changes to cloud host default branch(-u,branch,remote can be specified)
    
    $ git log 	#show commit history
    
    $ git checkout #by default checks out to default branch(branch/commit-id can be specified)
    
    $ git clone <url.git> #clones 'url' current location(location,depth,recursive can also be specified). 
    
    $ git status 	#shows the current status of the git repo.
    
    $ git commit -m "msg" 	#commit changes with a commit msg.
    
    $ git revert  	#revert last commit(commit-id can be specified).
    
    $ git commit -m "msg" 	#commit changes with a commit msg.
    
    $ git commit --amend --date="Wed Feb 16 14:00 2011 +0100" #commit date+time can be changed
    
    $ git commit --signoff	#for signed commits
    
    $ git branch   #show all branches for the remote
    
    $ git checkout -b "branch-name"   #switch to new branch 
    
    $ git branch -M "branch-name"     #switch current branch to specified branch.
    
    $ git clone <url> --depth=value    #value=1 fetches the latest repo with only the latest changes or shallow clones it.
    
    $ git fetch --unshallow         #converts the shallow repo clone to full clone
    
    $ git reset --hard HEAD~1  #removes the head/top commit
    

There are various cloud based git clients to help save your projects in cloud. Some popular of them are:

  1. Github
  2. GitLab
  3. SourceForge
  4. Gerrit
  5. Bitbucket
  6. Codebase
  7. AWS CodeCommit
updated_at 06-01-2021