Guide to git

What is Git:

Git is a software that allows you to version control. Git allows you to have a history of changes made to a file over time. Git was created on 2005 created by Linux development community to track and combine Linux Kernel changes. Since then Git has evolved to support large projects, fully distributed environment, and parallel branches with speed, efficiency and simplicity.

What can Git do?

  • Branch and Merge: Git allows you to do disposable experimentation by creating branches and when you are ready to apply your patch, you can do a git merge.
  • Distributed: Since git was developed to be fully distributed, a git repository can stay on a server or your local computer. You could download history of potentially millions of changes over the lifetime of files using git clone. While you make changes to the files git will record the changes when you run git commit. Once you are ready to show your local changes to the world you would run git push.
  • Data integrity and assurance: For every changes made to a git repository files, it generates a checksum of the repository and records it inside the repository as an ID. Since checksum is recorded in the same repository, the next checksum would depend on the previous checksum. This makes it impossible to change any file, date, commit message without changing ID of every commit after it. This ensures that no one can temper with integrity of a git repository.
  • Fast: Git version control system is a mature software that was optimized to be fast, small and efficient. To be fast git was written in C, which reduces overhead associated with higher level languages. Git compresses all the data when storing and transmitting to a remote repository. Git doesn’t stop at just compressing to make things efficient. When pushing changes to files, it only transmits the part of the file that was changed.

Git commands:

Set your identity

First thing you should do when you install git is to set your identity.

git config --global user.name "FirstName LastName"  
git config --global user.email emailAddress  
git config --global core.editor   

Example:  
git config --global user.name "Zobair Shahadat"  
git config --global user.email zobair@example.com  
git config --global core.editor vim  

Create a local repository

git init

Download a git repository from a remote server

git clone username@host:/path

Example:
Using HTTP
git clone git@ https://github.com/zmsp/zmsp.github.io.git
Using ssh
git clone git@github.com:zmsp/zmsp.github.io.git

Basic

Add files to be tracked

git add <filename>

example:
This adds everything from current directory
git add *

This adds a file called file.txt
git add file.txt

Remove a tracked file

git rm <filename>

Recoard current changes

git commit

Puts changes to the remote location

git push

git push origin master

See the files that were changed since the last commit

git status

Sync changes from remote

git pull

Branches

Branches allows you to break away from original code base (Master branch) while allowing you to track changes without effecting the master branch.

Branch commands

Create a new branch and switch git checkout -b <branchname>
Switch to existing branch git checkout <branchname>
List branches git branch
Delete a branch git branch -d <branchname>
Push to a branch git push origin <branchname>
Delete a remote branch git push origin :<branchname>

/// TODO Following sections needs to be updated///

Merging

git merge <branchname>
git rebase <branchname>

History

git status
git log
git log --author=Zobair
git log --pretty=oneline
git log --graph --oneline --decorate --all
git log --name-status
git log --help
git diff

TAGS

git tag release1 <commitHash>

Save a snapshot of local changes in a temporary place

git stash

Other stash related

# Stash local changes with a custom message
$ git stash save "this is your custom message"

# Re-apply the changes you saved in your latest stash
$ git stash apply

# Re-apply the changes you saved in a given stash number
$ git stash apply stash@{stash_number}

# Drops any stash by its number
$ git stash drop stash@{0}

# Apply the stash and then immediately drop it from your stack
$ git stash pop

# 'Release' a particular stash from your list of stashes
$ git stash pop stash@{stash_number}

# List all stashes
$ git stash list

# Show the latest stash changes
$ git stash show

# See diff details of a given stash number
$ git diff stash@{0}

Revert

git reset --soft origin/master
git reset --hard origin/master
git fetch origin
git checkout -- <filename>

Other commands

  • Help git --help
  • Help with a component of git git <componentName> --help eg: git log --help
  • Git GUI gitk
  • Runs a filesystem check git fsck
  • Garbage collector git gc
  • Run a webserver of local git repo git instaweb
  • colorful git output git config color.ui true
  • use interactive adding git add -i
  • show log on just one line per commit git config format.pretty oneline

Leave a Comment