Skip to content

Instantly share code, notes, and snippets.

@encima
Last active December 1, 2016 19:52
Show Gist options
  • Save encima/cb015eb0a7a61bf5d641f2259c811093 to your computer and use it in GitHub Desktop.
Save encima/cb015eb0a7a61bf5d641f2259c811093 to your computer and use it in GitHub Desktop.
Git cheatsheet

Git Cheatsheet

GOLDEN RULE: PULL BEFORE YOU PUSH!

Initial Steps

  1. git init
    • Creates a git repository locally on your machine. (this is basically a .git folder with config and other things inside)
  2. git remote add origin <link>
    • Addss the link of the remote repository (github, gitlab) etc as the origin alias
  3. git add .
    • This adds all the things in your directory to the repository
  4. git commit -m 'initial commit'
    • Commits should happen after changes have been made to a file or files
  5. git pull origin master
    • Pull any other changes down from the remote repository (crucial if you are working with other people)
  6. git push origin <branch>
    • Push your commit changes up to the remote repository

Common Terms

  • Repository - The directory containing your code
  • Remote repository (often called origin) - The link to where your repository is stored online
  • Commit - A checkpoint in your development that signifies a change (i.e. a bugfix or implementing a login page)
  • Pull - Check for changes in the remote repository and download them to your machine
  • Push - Upload your changes to the remote repository
  • Branch - Contains different versions of code. Default is the master branch but this can be anything
  • Merge - Happens when there are conflicts between your code and the remote repository (or vice versa), you must merge your changes and then commit again (git will often try and auto merge)

Useful Commands

  • git checkout -b <branch>
    • Creates a new branch and switches to it.
  • git branch <branch>
    • Create a branch but do not switch to it.
  • git branch
    • List all branches
  • git branch -d <branch>
    • Delete local branch
  • git merge <branch>
    • Merge the named branch into the current branch you have checked out
  • git reset --hard HEAD
    • If you totally mess up, this will revert your local repo to the last committed state
  • git stash
    • Stash your changes but do not commit them
  • git stash apply
    • Apply the stashed changes
  • git log
    • A log of all commits and by whom they were made
  • git tag
    • Tag a point for easy access later (i.e. Versions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment