Skip to content

Instantly share code, notes, and snippets.

@johnnyjung
Created January 21, 2013 20:57
Show Gist options
  • Select an option

  • Save johnnyjung/4589307 to your computer and use it in GitHub Desktop.

Select an option

Save johnnyjung/4589307 to your computer and use it in GitHub Desktop.

Revisions

  1. johnnyjung created this gist Jan 21, 2013.
    85 changes: 85 additions & 0 deletions git_cheat_sheet
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,85 @@
    Workflow

    1) Create private branch off public (remote) branch
    2) Regularly commit to this private branch
    3) Clean up its history (squash merge, rebase, or merge into a separate temp branch and then merge to master)
    4) Merge to origin public (remote) branch

    git init (Create a new local repo)
    git remote add repo_name ~~~~~~.git (Add a remote repo as a new remote "repo_name")
    git clone ~~~~~.git repo_name (Clone a remote repo as "repo_name")
    git clone existing new (Clone the local repo "existing" as "new")
    git clone --bare existing bare.git (Clone the local repo "existing" as bare repo "new")

    git add filename (add a file)
    git add * (add current dir)
    git add -A (all all)
    git commit -m "" (commit msg)

    git checkout filename (undo unstaged changes to a file)
    git reset HEAD filename (undo staged changes to a file)

    git commit --amend (ammends to the previous commit)

    git diff <hash> (see unstaged differences with 'hash' -- default HEAD)
    git diff --staged <hash> (see staged differences with 'hash' -- default HEAD)

    git reset --hard hash (reset HEAD to a given commit and reset local changes)
    git reset hash (reset HEAD to a given commit and leave local changes)
    git reset --hard remote/branch (reset to a given remote branch HEAD)

    git lg --all (see all commits)
    git lg -p (see diffs for each commit)
    git lg --stat (see the file based stats for each commit)

    git stash save (save local working directory to stash)
    git stash apply [name] (apply stash of name -- default latest)
    git stash pop (apply latest stash)
    git stash list (list all in stash)
    git shash show [name] (show diff of stash of name -- default latest)
    git stash drop [name] (drop stash of name -- default latest)
    git stash clear (clear the stash)

    git branch (see local branches)
    git branch -a (see all branches)
    git branch new_branch (creates new_branch from current branch)
    git checkout -b new_branch (checkout new_branch from current branch)
    git checkout -b new_branch other_branch (checkout new_branch from other_branch)
    git checkout master (switch to master)
    git checkout other_branch (switch to other branch)
    git branch -d new_branch (delete new_branch)

    git remote (show remotes repos)
    git remote show remotename (show info about 'remote_name')

    git checkout -b name origin/name (co local branch 'name' that tracks remote branch 'name')
    git checkout --track origin/experimental (same as above)
    git branch --track experimental origin/expimerimental (same as above with no checkout)

    git merge other_branch (merge other branch into current branch)
    git merge --no-ff other_branch (merge other branch into current branch with no ff)
    git merge --squash other_branch (squash merge other branch into current branch)
    -> do this directly into parent branch
    git rebase --interactive other_branch (rebase other branch into current branch)
    -> rebase in the child branch, then ff merge
    -> rebase only local branches!
    -> pick for no change
    -> change to squash to squash the commit to above

    git fetch (pulls commits from origin, but doesn't merge into the local branches)
    git merge origin/master (then actually merge into local branches)
    git pull (fetch and merge)

    git push origin master (push current branch to remote origin, branch master)
    -> create if not exist
    git push remote_name branch_name
    git push origin :other_branch (delete other_branch from remote origin)

    git tag tagname <hash> (tag commit as tagname)
    git tag tagname (tags current current as tagname)
    git checkout tagname (to checkout a tag)
    git push origin tagname (push tag tagname to origin)
    git push origin --tags (push all tags to origin)
    git tag -d tagname (delete local tag tagname)
    git push origin :refs/tags/tagname (delete remote tag tagname)