Skip to content

Instantly share code, notes, and snippets.

@arjunvenkat
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arjunvenkat/09e2c3c1b7de44663248 to your computer and use it in GitHub Desktop.
Save arjunvenkat/09e2c3c1b7de44663248 to your computer and use it in GitHub Desktop.
Git Guide

Git Guide

Cloning down a project

In your web browser, navigate to the project page that you want to clone down and copy the clone URL on the middle-right side of the project page. It should look something like https://github.com/tsl-su-15/ruby_logic.git

Then in terminal, navigate to the parent folder that you want to download the project into and and use the following command, replacing [project_url] with your project's URL:

git clone [project_url]

It should look something like:

git clone https://github.com/tsl-su-15/ruby_logic.git

Then run these commands to get your cloned app setup:

bundle install

rake db:migrate

Committing your changes

Check the status of your repository with:

git status

Add any new changes with:

git add -A

Commit your changes with:

git commit -m "some message"

Make sure you don't forget the message at the end, or else you'll run into some probems.

You might get pulled into a text editor called Vim at this point. You'll know if the top of your terminal window says "vim" instead of "bash" or your normal terminal program. To save your changes and exit Vim, use the command :wq. If that doesn't work, first hit the esc key then type :wq and hit enter.

Once you've commited your changes, use git status again and you should see the message:

nothing to commit, working directory clean

Pushing to Github

If this is your first time pushing your repo to Github, initialize the project as a Git repository with:

git init

This command tells Git to keep track of your project.

Next, commit your changes according to the directions above. Now you're ready to connect to Github and push up. If you haven't already, create a new repository in Github and follow their directions to connect your local project to your new Github repo. The directions should look something like this:

git remote add origin https://github.com/your_account/your_repo.git
git push -u origin master

The first line creates the connection and the second line pushes your code up, as long as all your changes have been committed.

Any time you need to push in the future, just commit your change and run:

git push origin master

Pulling down changes from Github

There are two ways to accomplish this. I'm assuming you're pulling down changes into a repo that you've cloned down from Github.

1. Overwrite your local changes

If you're fine with overwriting any work that you've done, add and stash your changes inside your project folder:

git add -A
git stash

git stash takes all your changes and stores them in some mysterious part of your hard drive. It's possible to recover those changes, but for now use the command as if you're just throwing away your changes.

Then use the command:

git pull

to pull changes from the original repo on Github

2. or Merge your local changes

If you don't want to lose any changes you've made, use this method. Be prepared - it's a bit trickier.

First, make sure all your local changes have been committed. Then run the command:

git pull

You'll probably get a merge conflict. That's ok, this is totally expected. This happens because the histories of your local repo and the repo your pulling from have diverged, and Git doesn't know which pieces to keep. If you do get a merge conflict, you'll need to go into each file with a merge conflict and edit the file so only the appropriate code remains. If you don't get a merge conflict, you might be pulled into Vim to commit your changes. You'll know this because the top of your terminal window will say 'Vim'. You can save and exit Vim by using the command :wq

Travelling to old points in a project's history

The easiest way to do this is to find the commit hash in Github, then use it to reset your repo back to that commit.

First, navigate to a Github project page, then clicking on the 'commits' link:

git-hist-1

Then find the commit hash by clicking on the 'copy' button for the appropriate commit to copy it to your clipboard:

git-hist-2

Once the commit hash has been copied to your clipboard, run the following command inside terminal:

git reset --hard [your commit hash]

it should look something like:

git reset --hard 7a789c747dd389cc1e51566a2f18da8c5916a3bb

Now, the project will be reset to that earlier point in its history.

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