Skip to content
ellun edited this page Jun 21, 2017 · 2 revisions

Basic Git Commands!

This guide is to help you understand basic commands used with git/p>

So what is git? Git is version control to track changes you make to your files. But this isn't just for a singular version, with git you can create "branches" which essentially clones your current working branch.

Why would we want to clone a working branch? Lets say you wanna add that cool new feature to stay young and hip with the kids. If you tried to implement a new feature on your main branch right away then you're looking for a bad time. Branching allows you to test features and fix bugs before you merge it back into the main branch. This helps ensure the quality of your product.

Getting an existing project

When working with an existing project that's been deployed to a git repo, we can simply the command git clone to get a copy of that repo!

Example. This class-notes repo is located at https://github.com/bootcamp-s17/class-notes.wiki.git. If we wanted to clone all these files locally, we would simply run

$ git clone https://github.com/bootcamp-s17/class-notes.wiki.git

This copies our cloned git repo in our current directory. So make sure you're cloning it to where you want locally.

By default, we'll be starting off in the master branch. You can consider the master branch like the mother branch. The branch where you would run your working product. We want to always avoid working in the master branch. As specified above, we want to work in feature branches and merge those into master when we are satisfied with the quality of our feature!

Create a feature branch

$ git checkout -b newBranchName

The checkout command allows us to switch around to existing branches.

-b allows us to create a branch that doesn't currently exist.

If a branch already exists, you can simply switch to it with

$ git checkout existingBranchName

You can use the following command to see all existing branches.

$ git branch -v

Updating your branches

Repos allow you to work on multiple machines and also work with others. When ever a branch gets updated on one machine, you'll want to make sure everyone instance of the project is up to date.

Lets say you're working on your laptop on a feature branch, you decide you want to jump onto your desktop instead and continue working. Well, how would you get the changes you just made on your laptop onto your desktop? This is where git repos come in. You can save your current work and send up your changes for other people to grab.

Example:

# you're in the readme branch
# this branch is just for updating the readme
# you've made changes on your laptop and want your desktop to see the changes.
# run git status to see what changes have been made.

$ git status
On branch readme
Your branch is up-to-date with 'origin/readme'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

# git status is letting now that we've made changes to the README.md file.
# We need to add these changes.

$ git add  README.md

# adding essentially says, we want to save these changes and get them ready to send to our git repo, but before we can do this, we need to give our save a commit message. A commit message is a brief description to what changes have been added.

$ git commit -m "Updated documentation on the README.md"

Now that our changes have been added and committed, we can send our changes to our online git repo.

$ git push origin readme

This sends our changes to a readme branch on our git repo. Now, anyone with access to that git repo can pull down our changes.

# you are now on your desktop.
# you are on your readme branch

$ git pull origin readme

This will pull the new changes made from your laptop onto your desktop. You should now be up to date and ready to work again!

Summary

$ git clone gitUrl

Copies a project locally.

$ git checkout branchName

Switches branches

$ git add
$ git commit

Saves local changes and allows you to deploy your project back to the online repo.

$ git push origin branchName

Allows you to push your changes to an online branch.

$ git pull origin branchName

Allows you to pull and changes made to a branch.

Clone this wiki locally