Skip to content

Instantly share code, notes, and snippets.

@mlg-
Forked from EliseFitz15/git-cheat-sheet.md
Last active March 10, 2016 17:50
Show Gist options
  • Save mlg-/65f699da63c0b0743072 to your computer and use it in GitHub Desktop.
Save mlg-/65f699da63c0b0743072 to your computer and use it in GitHub Desktop.
git-cheat-sheet

##Intro to Git

##What is Git?

  • Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency

##What is GitHub?

  • GitHub is a web-based Git repository hosting service, which offers all of the revision control and source code management (SCM) functionality of Git.

##Initializing a Project with Git Make sure you are in the project's root directory! You can run pwd to see where you are if you're unsure.

  • git init This should only be run once - this initializes a new git repository in this directory.
  • git status This command allows you to check the current status of our project at anytime. It shows what files are being tracked or need to be added to the staging area.
  • git add -A This adds all files onto staging. You can also add files one at a time, simply sub out -A for a file's relative path and name. You can see for sure that you've staged these files by running git status again.
  • git commit -m "Initial commit" This is the standard message for the first commit of a project.

##CHOOSE YOUR OWN ADVENTURE! You can use the hub gem or set your repo up on Github manually.

##Hub Gem Repo Setup on Github

  • Install hub with gem install hub
  • Run hub create repo-name in your directory (name it whatever you like, just remember this will appear on your public GitHub profile!
  • Now you should have a repo set up on GitHub that's connected to your local git repository, and you can skip down to pushing to Github.
  • NOTE: If you get a access denied (publickey) error, you need to add an SSH key to your GitHub account. Follow these directions and then these exactly.

##Manually set up your repo on GitHub. On GitHub click "New Repository" and give the repo a name. It's a good idea to make it the same as your root directory/project name.

Once you click "Create Repository" you can go back to your terminal and run

  • git remote add origin <your-remote-repo-url>

To check that the project is connected to GitHub you can run git remote and you should see "origin" this is what the master branch is called on GitHub.

  • git push origin master

This command pushes any commits you've made up to GitHub and is the last step for getting a project set up with Git and GitHub.

##On-going git workflow You'll want to continue making git commits when you get to a good stopping place in your project. Usually you'll want to commit code that is working, or make a commit when you complete a feature or are at a natural stopping point when creating a new feature.

  • git status
  • git add <file-names>
  • git commit -m "descriptive message on what the commit accomplishes here if you haven't hit a working stoping point, a work in progress (WIP) commit works too."
  • git push origin master

##Helpful Git commands

  • git status Check status of files modified, added, deleted, etc.
  • git logs A journal that remembers all the changes we've committed so far, in the order we committed them.
  • git stash Allows you to discard changed files instead of adding them to staging, you can also get them back if you want to come back to them later with git stash pop
  • git remote Checks to see what remotes have access to this repository. You should see origin for github and heroku for if you've connected it there for deployement.

##Git clone other repos and set up your own local copy!

  • git clone <repo-link>
  • git remote rm origin
  • go to Github and set up your own repo
  • git remote add origin <your-remote-repo-url>
  • on-going git workflow

##Branching and Pull Request Workflow When collaborating it's common to create branches for features so that git can track who is working on a set of files or changes. This also preserves your master branch as a definitive, working copy of the code and avoids contaminating it with experimental or not-yet-functional features.

New branch

  • git checkout -b <branch-name-descriptive-of-feature>
  • git pull origin master This command pulls down the latest code on the master branch from GitHub.

Once you are on a branch you would use the on-going git workflow and then push to the branch name.

  • git push origin <branch-name>

To switch branches

  • git checkout <branch-name>

Once you have working code that you want to merge into the master branch, you can open a pull request on GitHub. Go to the repo and click on the branch link. You'll see a button "New Pull Request". Once on that page you can "open" a new pull request.

Code review occurs at this stage and additional refactoring and changes may occur. You can make additional commits on that branch and push them up to GitHub to resolve any comments that are made if someone else looks at your code. When the code is ready to be merged into master, it's time to click "Merge Pull Request" button at the bottom of the pull request page. BE CAREFUL! This means whatever is in the branch will be put on your master branch, the official "good and working" copy of your project.

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