Skip to content

Instantly share code, notes, and snippets.

@madysondesigns
Created April 4, 2014 20:56
Show Gist options
  • Save madysondesigns/9982968 to your computer and use it in GitHub Desktop.
Save madysondesigns/9982968 to your computer and use it in GitHub Desktop.
Git Cheatsheet
Checking in/out:
$ git clone [project url] - get a copy of repo in a folder
$ git checkout [file] - check out last committed version of a file
$ git add --patch [file] - Step through each section of changes in a file with option to stage or not stage
$ git remote rm origin - remove origin
$ git remote add origin [url] - add origin
Working with Branches:
$ git branch [foo] - create branch named foo
$ git branch - show all branches
$ git branch -r - show remote (origin) branches
$ git branch -d -r origin/abtest - delete remote branch with no local reference 
$ git checkout [foo] - check out branch foo
$ git push -u origin [foo] - push foo branch to remote origin, create if doesn't exist, and set as upstream
$ git branch -D [foo] - delete local branch foo
$ git push origin :[foo] - delete remote branch foo
$ git --version - check version
$ git log oldbranch ^newbranch --no-merges - show commits on oldbranch that are not in newbranch (works with git l)
$ git diff-tree --no-commit-id --name-only -r [hash] - show files committed in given hash
$ git remote prune origin - remove unused branches from origin list (use --dry-run for list first) 
$ git fsck --full --no-reflogs | grep commit - Grab dangling commits, show each to recover a deleted branch
tagging:
$ git tag -a [version] -m'[message]'
Search through commit messages:
$ git l | grep [foo] - gives you list of commits containing foo and their hashes
$ git checkout [hash]^ - checks out the commit before the hash into a no branch
$ git checkout -b [foo] - convert the no branch into an actual branch
[do stuff]
$ git checkout master - return to current master branch
Comparing:
$ git show [hash] - Show diff of all files in particular commit
$ git diff --stat master..[foo] - show what has changed between master and foo branch
$ git diff [hash or HEAD^] [file] - Show what was changed in a particular file
$ git l - show pretty log for current branch, la for all branches
$ git ra - show last 20 lines of pretty log for all branches, r for current branch
$ git l origin/master..HEAD - show unpushed commits (doesn’t show un/staged changes)
Bisecting/Binary Search:
http://git-scm.com/book/en/Git-Tools-Debugging-with-Git#Binary-Search
Resets:
$ git reset - unstage everything that was staged and not committed
$ git reset --hard HEAD - trash uncommitted changes, ignores untracked files
$ git reset --hard origin/master - reset to github- i.e. when undesired changes are committed to master
$ git clean - cleanup untracked changes (-n or -f required, notify what would be cleaned up, then force cleanup - also -d to include directories)
$ git reset --soft HEAD^ - 'undo' commit, reset it and move all files back to how they were prior to commit- staged or unstaged
$ git checkout [hash]^ [file] - grab specific files from the commit before the noted hash
$ git checkout [hash/branch] -- [file/folder] - check out files from another branch or commit seq
Stashing:
$ git stash - temporarily stash uncommited/untracked changes
$ git stash list - show stashes you've stored
$ git stash apply - apply the most recent stash
$ git stash apply [stash ID] - apply a particular stash
$ git stash drop [stash ID] - remove a particular stash
$ git stash pop - apply and drop the most recent stash
$ git stash show -p - show diff of stash
Edit old commit message:
$ git rebase --interactive [hash]^ - opens commit editor, change typed commit to ‘edit’ and save
$ git commit --amend - opens commit editor to edit message
$ git rebase --continue - finishes the rebase
Misc: 
$ git update-index --assume-unchanged [file] - assumes changes you've made shouldn't be committed. VERY DANGEROUS!! (undo with --no-assume-unchanged) Git also bitches when you try to switch branches since changes will be blown away. 
$ git clean -f -f -d [dir] - remove submodule directories
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment