Skip to content

Instantly share code, notes, and snippets.

@jooeycheng
Last active September 1, 2016 10:59
Show Gist options
  • Save jooeycheng/e4add69c7239ff19aca71e22343521ea to your computer and use it in GitHub Desktop.
Save jooeycheng/e4add69c7239ff19aca71e22343521ea to your computer and use it in GitHub Desktop.
jooeycheng git notes / reference / cheatsheet
git status
git diff                        // diff working_dir vs last_commit

git log
git log --graph                 // visualize graph topology in terminal
gitk                            // visualize graph topology in gitk (GUI)

git add .                       // stage new & modified files, without deleted
git add -u                      // stage modified & deleted files, without new
git add -A                      // stage everything

git commit -m "{}"              // commit staged files with {} message
git commit -am "{}"             // stage and commit modified files with {} message
git checkout {}                 // switch to {} branch
git checkout -b {}              // create new branch and switch to it
git checkout -b {1} {2}         // create new branch {1} based on branch {2}

git branch                      // lists branches
git branch -r                   // list remote branches
git branch -a                   // list all branches (local and remote)
git branch -m <oldname> <newname> // rename
git branch -m <newname>           // rename current branch

git branch -d {}                // delete branch {} after merging
git branch -D {}                // delete branch {} without merging
git push --delete origin {}     // delete branch {} on remote

git merge {}                    // merge {} into current branch
git merge {} --no-ff            // merge with a commit (create commit even if fast-forward)
git rebase {}                   // make current branch based on {}
                                // (warning: rebase local branch only)
                                // (warning: rebase creates new commits, with new commit_id)
                                // (warning: dont rebase a shared remote branch, commit_id will change, de-sync with collaborators local branch)
git remote add heroku git@heroku.com:appname.git
git remote rm heroku            // remove remote branch called heroku
git remote -v                   // see all remote branches

git push heroku master          // push local master branch to heroku's master branch
git push heroku {}:master       // push local {} branch to heroku's master branch
git push heroku master --force  // --force or -f to force push
{} = [ 
      HEAD                      // working dir
      HEAD~ or HEAD~1           // last commit
      HEAD~2                    // 2nd last commit, and so on...
      <commit_id>               // specific commit
     ]

git commit --amend              // add staged files to last commit, edit commit message
git commit --amend --no-edit    // skip edit message (use same message)

git reset HEAD                  // unstage all staged files
git reset HEAD~                 // undo last commit (git reset --mixed HEAD~)

git reset --soft  {}            // changes from {} will be staged. working dir before reset is preserved.
git reset --mixed {}  (default) // changes from {} will be unstaged. working dir before reset is preserved.
                                // unstaged changes from {} will merge with working dir's unstaged changes of the same file.
git reset --hard  {}            // changes from {} stays. working dir is now exactly as {} commit. everything else is nuked.

Atlassian Git Tutorial - Undoing Changes

Thoughtbot - Git Interactive Rebase

Stackoverflow - How to undo/edit last commit

Public GitHub Gist - Git reset guide

Other people's gist git cheatsheet - hofmannsven, betawax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment