Skip to content

Instantly share code, notes, and snippets.

@rayden-alex
Forked from virtualadrian/git_cheat_sheet.md
Created July 22, 2018 16:03
Show Gist options
  • Select an option

  • Save rayden-alex/b2cc1214e7b7e93d8c82b7d926d71ef2 to your computer and use it in GitHub Desktop.

Select an option

Save rayden-alex/b2cc1214e7b7e93d8c82b7d926d71ef2 to your computer and use it in GitHub Desktop.

Revisions

  1. @virtualadrian virtualadrian created this gist Oct 15, 2017.
    189 changes: 189 additions & 0 deletions git_cheat_sheet.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,189 @@
    # GIT CHEAT SHEET

    A humble attempt to create a easy on the eye, skimable git cheat sheet. Please comment with requests, improvement suggestions or corrections.

    ## Starting a Repo init/clone/remote

    **Create or Initialize** a repo from **existing** code or files
    > git init
    **Clone** a current repo (into a folder with same name as repo)
    > git clone (repo_url)
    **Clone** a repo into a **specific folder** name
    > git clone (repo_url) (folder_name)
    **Clone** a repo into **current directory** (should be an **empty directory**)
    > git clone (repo_url) .
    **Create** a remote repo **named origin**
    pointing at your Github repo (after you've already created the repo on Github) (used if you git init since the repo you created locally isn't linked to a remote repo yet)
    > git remote add origin https://github.com/username/(repo_name).git
    **Create a remote** repo named **origin** pointing at your **Github repo** (using SSH url instead of HTTP url)
    > git remote add origin [email protected]:username/(repo_name).git
    **Show** the names of the **remote repositories** you've set up
    > git remote
    Show the names and URLs of the remote repositories
    > git remote -v
    **Remove** a **remote** repository
    > git remote rm (remote_name)
    **Change** the **URL** of the git **remote**
    > git remote set-url origin (git_url)
    **Push** your changes **to** the **origin**
    > git push


    ## Showing Changes status/diff/log/blame
    Show the **files changed**
    > git status
    Show **changes** to files **compared** to **last commit**
    > git diff
    Show **changes** in single **file** compared to **last commit**
    > git diff (filename)
    Show **changes** between two different **commits**.
    > git diff (commit_id)
    Show **history** of changes
    > git log
    Show **who changed each line** of a file and when
    Commit ID: This can be that giant long SHA-1 hash. You can call it many different ways. I usually just use the first 4 characters of the hash.
    > git blame (filename)

    ## Undoing Changes reset/revert

    Go **back to the last commit** (will not delete new unstaged files)
    > git reset --hard
    Undo/revert last commit AND create a new commit
    > git revert HEAD
    Undo/revert a specific commit AND create a new commit
    > git revert (commit_id)

    ## Staging Files add/rm
    Stage **all** files (new, modified, and deleted)
    > git add -A
    Stage **new and modified** files (not deleted)
    > git add .
    Stage **modified and deleted** files (not new)
    > git add -u
    **Remove** a file and **untrack** it
    > git rm (filename)
    **Untrack** a file **only**. It will still exist. Usually you will add this file to .gitignore after rm
    Git Workflow Trees: How adding and committing moves files between the different git trees.
    Working Tree The "tree" that holds all our current files.
    Index (after adding/staging file) The "staging" area that holds files that need to be committed.
    HEAD Tree that represents the last commit.
    > git rm (filename) --cached

    ## Publishing commit/stash/push

    **Commit** the local **changes** that were staged
    > git commit -m "message"
    **Stage files** (modified and deleted, not new) and commit
    > git commit -am "message"
    Take the **uncommitted work** (modified tracked files and staged changes) and **saves it**
    > git stash
    Show **list of stashes**
    > git stash list
    **Reapply** the latest **stashed** contents
    > git stash apply
    **Reapply** a **specific stash**. (stash id = stash@{2})
    > git stash apply (stash_id)
    **Drop** a specific **stash**
    > git stash drop (stash_id)
    **Push** your changes to the origin
    > git push
    **Push** a **branch** to the **origin**
    > git push origin (local_branch_name)
    **Tag** a version (ie v1.0). **Useful** for Github **releases**.
    > git tag (tag_name)

    ## Updating and Getting Code fetch/pull
    Get the **latest changes** from origin (_don't merge_)
    > git fetch
    Get the **latest changes** from origin **AND merge**
    > git pull
    **Checkout** a remote **branch** from origin into a local branch (naming the branch and switching to it)
    > git checkout -b (new_branch_name) origin/(branch_name)

    ## Branching branch/checkout

    **Show all** branches (**local**)
    > git branch
    **Show all** branches (local and **remote**)
    > git branch -a
    **Create** a branch **from HEAD** (latest)
    > git branch (branch_name)
    **Create** a new **branch** and **switch** to it
    > git checkout -b (branch_name)
    **Switch** to an already created **branch**
    > git checkout (branch_name)
    **Push** a **branch** up to the origin (Github)
    > git push origin (branch_name)
    **Get** a **remote** branch from origin into a **local** branch (naming the branch and switching to it)
    > git checkout -b (new_branch_name) origin/(branch_name)
    **Delete** a branch **locally** and **REMOTELY**
    > git push origin --delete (branch_name)

    ## Integrating Branches merge/rebase

    **Checkout** a **branch** by name
    > git checkout master
    **Merge** a specific **branch** into the **current** branch.
    > git merge (branch_name)

    Take **all the changes** in one branch and **replay** them on another **branch**. Usually used in a feature branch. Rebase the master to the feature branch so you are testing your feature on the latest main code base. Then merge to the master.
    > git rebase (branch_name)
    **Merge/Cherry Pick** one specific **commit** from another **branch** to your **current** branch.
    > git cherry-pick (commit_id)

    _


    _Resources:_

    https://scotch.io/bar-talk/git-cheat-sheet