ctrl + f or cmd + f to search what you need instantly

GIT is a version control system (VCS)

3 tier configuration in git

  1. Local Highest priority
  2. Global
  3. etc

Basic commands

# Initialise Local Git Repository
$ git init 
# Add files
$ git add <file> 
$ git add *.<file extension>  // add all files with the .<file extension>
$ git add . // add all files
# Remove files 
$ git rm <file> 
# Check status of working tree
$ git status 
# Commit changes to index
$ git commit 
$ git commit -m "Commit Message"
# Push commits to remote repository
$ git push 
# Pull lastest from remote repository
$ git pull 
# Clone Remote Repository into a new Local directory
$ git clone 
# Branch
$ git branch // Shows the branches of the repository
$ git branch <branch name> // Create <branch name>
$ git branch -D <branch name> // Deletes the branch
$ git checkout // Switch branch
$ git checkout -b <branch name> // Create and switch to <branch name>

Helpful Commands

### (5 commits ago) HEAD~5 || "HEAD^^^^^"
## Anything that can be parsed, a branch, a head, a tag, a hash
# rev-parse
$ git rev-parse <branch> // return current commit hash

Advanced Commands

# Display Commit(s) history of repository
$ git log 
$ git log --stat // Show files
$ git reflog // in order of last referenced
# Show changes between commit
$ git diff 
$ git diff <commit hash 1> <commit hash 2>
## --amend (changes git history) -> only use before pushing
# Change last commit message 
$ git commit --amend -m "New Commit Message"

# Add files to last commit
$ git commit --amend
## cherry-pick (move commits to other branch, don't delete commits)
$ git log // get the commit hash [7 - 8] characters
$ git checkout <branch name> 
$ git cherry-pick <commit hash>
## Delete commit
# --soft :: reset back to commit specified, keep changes made in staging dir
$ git reset --soft <commit hash> 

# (default, mixed) keeps changes in working dir
$ git reset <commit hash> 

# --hard :: reverts all track files, not untracked files
$ git reset --hard <commit hash> 
# Delete untracked files
$ git clean -df // d - directory, f - files
[a32]					[a23]
  |  \ (branch)				  |
[2e2] [d19]				[2e2]
  |     |				  |
[8b3] [e69]				[8b3]
                           		   \(branch)
                            		    [d19']
                              		      |
                            		    [e69']  
# Rebasing (fast forward merge)
$ git rebase master // might mess up reflog

# interactive rebase (can't rebase commits thats shared)
$ git rebase -i HEAD~2 (2 commits ago)
/* vim will pop up */

Unconventional Hipster Cool Commands

*Below shows the hardest way to create a file

How git commit works:

commit -> tree -> blob (file)

## Revert changes (doesn't modify commit history)
# Show changes between commit
$ git revert <commit hash>
## writing into file
# hash-object
$ echo "new file." | git hash-object -w --stdin // it returns a hash
## print from hash
# cat-file
$ git cat-file -p <file hash>
$ git cat-file -t <hash> // t - type
## update staging area
# update-index
$ git update-index --add --cacheinfo <file permission> <file hash> <file name.ext>
## tree object
# write-tree
$ git write-tree // it returns hash of a tree object
$ git cat-file -p <tree hash> // returns directory listings
$ git commit-tree <tree hash> -m "Commmit message" // returns a commit hash
## cat .git/HEAD
# update-ref
$ git update-ref refs/heads/<current branch> <commit hash> // it updates ref
## actual file
# checkout HEAD
/* 
 * -- seperates all option switches and parameters 
 * from a list of files
 * works for diff , checkout
 */
$ git checkout HEAD -- <file.ext> //