Git is a Command Line Interfacetool for version control of source code for software.

img

Data model

Git models the history of a collection of files and folders within some top-level directory as a series of snapshots. In Git terminology, a file is called a “blob”, and it’s just a bunch of bytes. A directory is called a “tree”, and it maps names to blobs or trees (so directories can contain other directories). A snapshot is the top-level tree that is being tracked. For example, we might have a tree as follows:

<root> (tree)
|
+- foo (tree)
|  |
|  + bar.txt (blob, contents = "hello world")
|
+- baz.txt (blob, contents = "git is wonderful")

The top-level tree contains two elements, a tree “foo” (that itself contains one element, a blob “bar.txt”), and a blob “baz.txt”.

Initialise Git Project

git init

Add Files

git add .

Show files in a commit

git show —pretty=“” —name-only <commit-id>

Branching

Checkout an existing branch

git checkout <branch-name>

Create a branch

git branch <branch-name>

Merge a branch

git checkout <branch-name> # Checkout branch to merge “to”
git merge <branch-name> # Choose branch to merge back

Delete a branch

git branch -d <branch-name>

Diff

Show changes to unstaged files

git diff

Show changes to staged files

git diff —cached

Show changes to staged and unstaged files

git diff HEAD

Show diff between two commits

git diff <commit-id-1> <commit-id-2>

Log

Show log of commits

git log 

Search log of commits

# Tell grep to also include 5 lines up, so we get commit id
git log | grep “<my search term>” -B 5