Skip to content

Instantly share code, notes, and snippets.

@jpvelez
Created July 25, 2012 21:05
Show Gist options
  • Save jpvelez/3178673 to your computer and use it in GitHub Desktop.
Save jpvelez/3178673 to your computer and use it in GitHub Desktop.
Git Cheat Sheet
COMMANDS
git init - initiates local git repo
git add file.txt - add file
git add . - add new or changed files. doesn't add removed files.
git add -A - add new, changed, and removed files
git commit -m "added juan to hello message" - commit files that you added above
git commit -am "msg" - add and commit simultaneously
git status - what's changed since my last commit (compares files in wd with repo)
git diff <file> - shows you how a file has change
git log - shows you a list of previous commits with their messages
git checkout <sha> . - sha is a unique string that represents a particular commit. they're the long strings you see when you type git status. this cmd replaces the files in your working with those in that commit. later commits still available in repo. by default it will checkout latest commit. make sure to add '.' add the end so that you checkout the commit to the current branch you're in... if you don't you'll change the file in the working dir but get off the branch, so if you make more changes and then commit them you won't be committing them to the master branch.
git checkout master - get back on master branch
git stash - take all the stuff that i've changed and stash it away
git stash apply - puts stashed code on top of working directory.
git clone <git address> - pull down files and create clone of origin repo in local dir. this is how you creates copies of public github repos.
git pull origin master - pull new commits from the master branch of the origin repo. always do this first when working with others on a repo.
git push - push new commit to the 'origin' repo i.e. github
first create new github repo on github
git remote add origin git@github.com:jpvelez/social_nav.git - associate local repo with github repo
git push -u origin master - first push from local to origin, the master branch of the remote github repo
HOW TO USE THESE COMMANDS
When coding, make some changes, check status, maybe check diff, add, commit, check status to make sure it worked
With github, first pull latest commit to master. Make changes. When you get to a good stopping point, commit them. Make sure you don't commit broken code.
If I forget to pull before I start coding and make some changes. I can stash those changes, pull down latest commit from github to local repo & working directory. Then, I can apply my stash to those now-up-to-date working files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment