Skip to content

Instantly share code, notes, and snippets.

@DannyCrews
Last active May 9, 2016 00:48
Show Gist options
  • Save DannyCrews/5924cc187625217f48a8d2d378dd3a98 to your computer and use it in GitHub Desktop.
Save DannyCrews/5924cc187625217f48a8d2d378dd3a98 to your computer and use it in GitHub Desktop.
Git command cheatsheet from Michael Hartl's Learn Enough Git to be Dangerous

Getting started

Command Description Example
git help Get help on a command $ git help push
git config Configure Git $ git config --global …
source <file> Activate Bash changes $ source ~/.bash_profile
mkdir -p Make intermediate directories as necessary $ mkdir -p repos/website
git status Show the status of the repository $ git status
touch <name> Create empty file $ touch foo
git add -A Add all files or directories to staging area $ git add -A
git add <name> Add given file or directory to staging area $ git add foo
git commit -m Commit staged changes with a message $ git commit -m "Add thing"
git commit -am Stage and commit changes with a message $ git commit -am "Add thing"
git diff Show diffs between commits, branches, etc. $ git diff
git commit --amend Amend the last commit $ git commit --amend
git show <SHA> Show diff vs. the SHA $ git show fb738e…

Backing up and sharing

Command Description Example
git remote add Add remote repo $ git remote add origin
git push -u <loc> <br> Push to branch to remote $ git push -u origin master
git push Push to default remote $ git push

Intermediate workflow

Command Description Example
.gitignore Tell Git which things to ignore $ echo .DS_store >> .gitignore
git checkout <br> Check out a branch $ git checkout master
git checkout -b <br> Check out & create a branch $ git checkout -b about-page
git branch Display local branches $ git branch
git merge <br> Merge in a branch $ git merge about-page
git branch -d <br> Delete branch (if merged) $ git branch -d about-page
git branch -D <br> Delete branch (even if unmerged) (dangerous) $ git branch -D other-branch
git checkout -f Force checkout, discarding changes (dangerous) $ git add -A && git checkout -f

Collaborating

Command Description Example
git clone <URL> Copy repo (incl. full history) to local disk $ git clone https://ex.co/repo.git
git pull Pull in changes from remote repository $ git pull
git branch -a List all branches $ git branch -a
git checkout <br> gh-pages Branch name for production website $ git push -u origin gh-pages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment