artimys
4/14/2017 - 6:28 PM

Git Cheatsheet

Basic and advance git commands.

The Basics

git add -A                        # to stage untracked files
git commit -am "message here"     # save work in a commit
git push origin master            # push to remote repo (master branch)

git pull                          # runs 'git fetch' then 'git merge'

git commit, reset, unstage, clean(todo)

# unstage a file
unstage <filename>
git reset HEAD <filename>

# remove already tracked file from history
git rm --cached <filename>          # delete tracked file but not deleted locally

git reset --hard <hash-or-ref>      # https://stackabuse.com/git-revert-to-a-previous-commit/
git reset --hard HEAD               # [goto]undo changes to unstaged/staged files
git reset HEAD^                     # unstage changes after reset, doesn't delete changes
git reset --soft HEAD^              # undo last commit, put changes into staging
git reset --hard HEAD^              # undo last commit and all changes
git reset --hard HEAD^^             # undo last 2 commits and all changes
git reset --hard HEAD@{1}           # go back a specific commit, use `git reflog` to get number

git revert <hash-or-ref>            # Deleting Published Commits

git clean -f      # remove untracked files
git clean -fd     # remove untracked files and directories


# Reset to HEAD: To hard reset a single file to HEAD.
# Note that @ is short for HEAD. An older version of git may not support the short form.
git checkout @ -- myfile.ext

# Reset to index:
# To hard reset a single file to the index, assuming the index is non-empty, otherwise to HEAD.
git checkout -- myfile.ext

git log, blame

git log
git log --pretty=oneline
git log --oneline --graph

git reflog

git blame <filename> --date short

git remote

git remote -v                                 # list all remotes
git remote add origin <git_path_to_repo>      # add remote
git remote rm origin <git_path_to_repo>       # remove remote

git remote show origin    # show some info about remote (branches, up-to-date, etc)
git remote prune origin   # to clean up deleted remote branches
git remote prune origin --dry-run   # previews which local branches it will delete

git branch, checkout, merge

git branch                    # show local branches
git branch -r                 # show remote-tracing branches
git branch -a                 # show local/remote-tracking branches

git branch <branch-name>      # create branch
git checkout <branch-name>    # switch to different branch
git checkout -b <branch-name> # create/switch branch with `-b` option

git branch -d <branch-name>   # delete branch
git branch -D <branch-name>   # delete branch regardless of unstaged files

# Delete REMOTE REPO branch
git push origin --delete <branch-name>
git push origin :<branch-name>



# This will list out your local branches with more information 
# including what each branch is tracking and if your local branch is ahead, behind or both.
git branch -vv


# merge feature-branch with master branch
git checkout master
git merge <branch-name>

git stash

# ---------------------------------
# IMPORTANT: STAGE YOUR FILES FIRST
# ---------------------------------

# create stash
git stash
git stash save "message here"   # or save with optional message

# view stashes
git stash list

# remove stash (choose the correct number)
git stash drop stash@{0}      

# apply stash to staging area
git stash pop stash@{0}      # pop - applies stash and removes from stash list
git stash apply stash@{0}    # apply - applies stash but leaves a copy in stash list

git amend

Useful when you forget to change a line of code or rename a commit message.

# ---------------------------------------------------------------
# NOTE: DO NOT AMEND IF COMMIT WAS ALREADY PUSHED TO REMOTE REPO
# you'll fuck up HEAD (out of sync),
# to fix HEAD, use git push origin master --force
# ---------------------------------------------------------------

git commit --amend -m "new message here"