GIT

What every developers should know

Git Basics

:)

How git works

Unlike other VCS, git take a snapshot of your code, not difference

Git doesn’t store a file again, if it has not changed

Link to the previous identical file it has already stored

What is a commit?

Commit is a snapshot of your code

Each commit has unique hash (using SHA-1)

The commit hash is check-sum of all files

Each commit may have 0 (only initial commit) or more parent

git int

git clone

git config

git add

--patch (-p)

git commit

--all (-a)

--patch (-p)

git status

Staged, Unstaged, Untracked

Untracked

Unstaged

New files and directory

Tracked files (committed before) but has changd

Staged

Changes that are ready for commit

git log

--oneline

--pach (-p)

git log <since>..<until>

git log <file>

git log --graph --decorate --oneline

--author

--grep

What is HEAD?

HEAD is a reference to current active commit

Travel through commits

:D

Move HEAD to a branch

git checkout <branch-name>

Move HEAD to a specific commit

git checkout <commit-hash>

* Detached HEAD

Change a file to what it is in a commit or branch

git checkout <commit-hash> <file>

git checkout <branch> <file>

Relative addressing

^ modifier

~ modifier

Examples

A =      = A^0
B = A^   = A^1     = A~1
C = A^2  = A^2
D = A^^  = A^1^1   = A~2
E = B^2  = A^^2
F = B^3  = A^^3
G = A^^^ = A^1^1^1 = A~3
H = D^2  = B^^2    = A^^^2  = A~2^2
I = F^   = B^3^    = A^^3^
J = F^2  = B^3^2   = A^^3^2

Undoing Changes

:|

revert

project $ git revert <commit-hash>

[<branch> <short-commit-hash>] Revert <commit-message>

n file changed, m insertion(+), t deletion(-)

project 

reset

project $ git reset <commit-hash>

Unstaged changes after reset:

M        index.php

M        app/routes.php

M        app/controllers/AdminController.php

project 

reset

Never reset public commits

reset

--soft

--hard

Reset Vs Revert

Clean

-n

-d

-f

-x

Rewriting History

B)

amend

project $ git commit -am "Add new hello.php"

project git add required.php

project git commit --amend --no-edit

Don’t Amend Public Commits

rebase

Don’t Rebase Public History

Syncing

(O.o)

remotes

git remote add <name> <url>

git remote rename <old> <new>

git remote remove <name>

git remote

git fetch

git pull

git push

Branches

(@_@)

branch

git brach

git branch <new-branch-name>

git branch -d <branch-name>

git branch -D <branch-name>

git branch -m <branch-new-name>

merge

git merge <branch-name>

git merge --no-ff <branch-name>

Workflows

:)

Centralized Workflow

Feature Branch

Gitflow

Forking

Base

Fork 1

Fork 2

Thank You

^_^

Git

By Behzad Shabani