Git all in one cheat sheet

initialize an existing directory as a Git repository
$ git init

add username and email commits for the current repo
$ git config user.name "Your Name Here"
$ git config user.email your@email.example

add name that will be attached to your commits and tags globally.
$ git config --global user.name “Your Name”

add e-mail address that will be attached to your commits globally
and tags.
$ git config --global user.email “you@example.com”

to add remote origin
$ git remote add origin git@github.com:user/repo.git

to change the url of an existing remote repository
$ git remote set-url origin git@github.com:User/UserRepo.git

retrieve an entire repository 
$ git clone [url]

working directory status
$ git status

add file to staging
$ git add [file]

show difference b/w directory and staging area
$ git diff [file]

show any changes between the staging area and the repository
$ git diff --staged [file]

remove all changes in working directory
$ git checkout -- .

remove changes from file in working directory
$ git checkout -- [file]

remove untracked files from working directory
$ git clean -df

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]

fetch all branches from git remote
$ git fetch [alias]

merge a remote branch into your current branch
$ git merge [alias]/[branch]

push local branch commits to remote
$ git push [alias] [branch]

fetch and merge any commits from the tracking remote branch
$ git pull

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

clear staging area, rewrite working tree from specified commit
$ git reset --hard [commit]

save modified and staged changes
$ git stash
$ git stash save "stash name optional"

stash staged, unstaged and untracked files
$ git stash save -u
$ git stash save "stash name optional" -u

stash unstaged files
$ git stash save --keep-index

stash staged files
$ git stash -- $(git diff --staged --name-only)

list stack-order of stashed file changes
$ git stash list

write working from top of stash stack
$ git stash pop

discard the changes from top of stash stack
$ git stash drop

apply latest stash
$ git stash apply

apply nth stash 
$ git stash apply n

undo last commit, commited files will be added to changes, commited code will not be lost
$ git reset HEAD~

undo last commit, commited files will be added to staging, commited code will not be lost
$ git reset --soft HEAD~1

delete last commit and commited code will be lost
$ git reset --hard HEAD~1

revert changes, a new commit will be added
$ git revert HEAD
$ git revert [commit]
skip editing commit message
$ git revert [commit] --no-edit


add commit, this will open vim ui to add comment 
$ git commit

add commit with message
$ git commit -m 'commit message'

add staged changes to previous commit
$ git commit --amend --no-edit

add staged changes to previous commit with new commit message
$ git commit --amend -m 'commit message'