Git CheatSheet

Cherry-pick

# pick specified commit
git cherry-pick 3ea6207fe84

Stash

# Stash modified/staged files
git stash

# Stash modified/staged/untracked files
git stash -u

# Stash modified/staged/untracked/ignored files
git stash -a

# Stash modified/staged files with message
git stash save "stash message"

# Show all available stashes
git stash list

# Get stash to working gir & delete the stash (last or specified)
git stash pop
git stash pop [email protected]{1}

# Get stash to working gir, but not delete the stash  (last or specified)
git stash apply
git stash apply [email protected]{1}

# Recover dropped stash (you need to know it's hash)
git stash apply [stash-hash]

# Delete stash (last or specified)
git stash drop
git stash drop [email protected]{1}

# Show stash diff (last or specified)
git stash show
git stash show [email protected]{1}

# Create branch from stash & delete stash (last or specified)
git stash branch [branch-name]
git stash branch [branch-name] [email protected]{1}

By default git SKIP untracked or ignored files for stash.

Remote, Fetch, Pull, Push

Retrieving updates from another repository and updating local repos

# add a git URL as an alias
git remote add [alias] [URL]

# Fetch down all branches from remote repo
git fetch [alias]

# Push local branch commits to remote branch
git push [alias] [branch]

# Force push (with changing history)
git push [alias] [branch] --forse   =   git push [alias] +[branch]

# Fetch and merge any commits from the tracking remote branch
git pull

# Pull only if it can be “fast-forwarded” (without creating new commits)
git pull --ff-only

Merge, Rebase

Merge

# merge the specified branch’s history into the current one
git merge [branch]

# merge a remote branch into your current branch to bring it up to date
git merge [alias]/[branch]

Rebase

# apply any commits of current branch ahead of specified one
git rebase [branch]

Branch, Checkout

Branch

# List all local branches
git branch

# List all local branches & show last commit message
git branch -v

# Delete local branch only if it was fully merged
git branch -d [branch-name]

# Force delete local branch
git branch -D [branch-name]

# Rename branch
git branch --move [old-branch-name] [new-branch-name]

# Add local branch to remote repo
git push --set-upstream origin [branch-name]

# Remove branch from remote repo
git push origin --delete [branch-name]

# Create a new branch at the current commit
git branch [branch-name]

# List local & remote branches
git branch --all

# List of branches that already was merged with current
git branch --merged

# List of branches that not merged with current
git branch --no-merged

Checkout

# Switch to another branch and check it out into your working directory
git checkout [branch-name]

# Create branch and checkout to it
git checkout -b [new-branch-name]

Status, Stage, Commit, Reset

Commit

# commit your staged content as a new commit snapshot
git commit -m "your commit message"

# add & commit
git commit -am "your commit message"

# Append current changes to last commit
git commit --amend

Status (diff show)

# show modified files in working directory, staged for your next commit
git status

# diff of what is changed but not staged
git diff

# diff of what is staged but not yet commited
git diff --staged

# show the diff of what is in branchA that is not in branchB
git diff branchB...branchA

# show any object in Git in human-readable format
git show [SHA]

Log

# show all commits in the current branch’s history
git log

# show the commits on branchA that are not on branchB
git log branchB..branchA

# show the commits that changed file, even across renames
git log --follow [file]

# Show compact
git log --oneline

# Show tree
git log --graph
git log --graph --oneline

Stage (add)

# Stage all repo files (not from .gitignore)
git add .

# Stage all changes in <directory> for the next commit.
git add [directory]

# Stage all changes in <file> for the next commit.
git add [file]

Reset

git reset   =   git reset --mixed HEAD

git reset HEAD
git reset [hash]

# reset all: commit history, stage snapshot, working dir
git reset --hard [hash]

# reset: commit history, stage snapshot
git reset --mixed [hash]

# reset: commit history
git reset --soft [hash]

# unstage a file, but retaining the changes in working dir
git reset [file]

Create Repo

# Create Git repo in current folder
git init
git init [project name]

# Create remote repo in current folder by URL
git clone [url]

Config (settings, setup, option)

# Show all settings
git config --list
# Or see files: `~/.gitconfig` (user wide) `.git/config` (repo wide)

# Show where that setting is defined (global, user, repo, etc...)
git config --list --show-origin

# Show single option value
git config --get user.name
git config --show-origin --get user.name

# Set exact setting
git config --global user.name "[Kama]"
git config --global user.email "[[email protected]]"

# Set automatic command line coloring for Git for easy reviewing
git config --global color.ui auto

# Set system wide .gitignore patern for all repos
git config --global core.excludesfile [file]

Tracking

# delete the file from project and stage the removal for commit
git rm [file]

# change an existing file path and stage the move
git mv [existing-path] [new-path]

Revert

# Creates new commit in which all changes of another commit will be reverted
git revert

# Revert merge and leave only 1 parent
git revert -m 1 HEAD   =   git revert --mainline 1 HEAD

-

See: https://education.github.com/git-cheat-sheet-education.pdf