So now that you have a git repository on your computer, and you can take snapshots of your project at any given time, lets talk about Github.
Github is a place to publicly store your repo on a server. When you do this, you have a backup of all of your commits, and other people can view/collaborate on your project. So how do you get your git repo from your computer onto github?
First, check your git user.name and user.email by opening the terminal and typing:
git config --get user.name
You should see your name show up. Now type
git config --get user.email
you should see your email show up. If you don’t see what you expect, do this:
git config --global user.name "Your Name"
git config --global user.email "firstname.lastname@example.org"
You’ll definitely need a github account. Go make one (or log in). Make sure you use the same email address for github that have configured for git! You also need to set up an SSH key, which you can do by following this link
Alright, you’re ready to put your local repo onto github.
First, open your terminal and prove to github that you are in fact who you say you are by typing:
ssh -T email@example.com
You should see a message like the following:
Sweet, Github now trusts you!
Ok, go to github and create a new repository (click your the + icon next your your username)
Give it a name and a description. Preferably the name should match the name of your directory on your computer. I’m calling mine homework_assignments. You probably don’t want to change any of the default options. Leave them as public and DO NOT check the box that says “initialize this repository with a README”
You should see a screen that contains this:
You’ll want to follow the prompts for “Push an existing repo…”
From then on, whenever you make a commit from your terminal, can can simply type
in the terminal from your git directory, and your commit will now be on github.
One of the things I love about programming is that I feel like I get to see behind the scenes of things that most people take for granted. Not only that, but I am gaining the ability to change and contribute to the inner workings of the technology that affects millions of people.
My journey in programming so far has been a journey of self teaching and writing simple (and sometimes not-so-simple) programs that run in a browser or on my computer that do something. I think I became a fairly competent beginning programmer pretty quickly, but there was always a missing piece. I knew how to write code, but not how real programmers and developers actually worked. How do a bunch of lines of code in several different files, possibly on multiple computers, all come together to make something work?
After two weeks of the DaVinci Coders class, I think I have a decent understanding of how that works. Git and Github are two tools that programmers use for revision control and collaboration on projects. I’m going to walk through the steps of setting up and using git. In another post, I’ll discuss more about github.
What is Git?
Git is a distributed version control system. It is a powerful tool to ensure that as you work on, change, and update a project, you aren’t prone to lose something or break something that worked before. There are many ways to install git, and I’ll let you google to find them. Once git is installed, here’s how you use it to create a repository in a directory called git_practice.
create the directory:
change into that directory:
You now have a .git/ directory that contains the inner workings of git, and you are ready to start modifying/adding files, staging, and committing.
1) The Workspace
The workspace is where all of your current project files live. You can add new ones, change existing ones…etc. Lets create a file:
echo 'Hello World' > README
You should see something like this:
This will list any and all files that have not been staged or committed.
When you have a file (or many) that are updated with, say, a new release of your project, you can stage those files by typing:
git add filename1 filename2 filename3...
Then you can type
git status again to see that those files have been moved to the staging area. This means they are ready to commit.
When you commit, you are saying that all the staged files are ready to be saved as a permanent record of your project at that point in time. The more often you commit, the more versions of your project you have saved, so you can always go back and see what your project looked like at a given point in time. You commit by typing:
git commit -m 'description of commit goes here'
If you forget the -m with the comment, it will take you into your default text editor (probably Vim), and ask you to type your comment to go along with this commit. If you get into vim, type (but without the ) to change to insert mode (so you can type), then press and type to write out and quit the editor. You have now made your commit! All staged files, and previously committed files, are saved in that commit, and you can return to the workspace and make more updates/changes.
So thats Git in a nutshell. Keep in mind that if your hard drive crashes, you will lose all of your projects including all of your commits. To eliminate this risk, and to allow for collaboration on projects, you’ll want to use Github, which I will discuss in my next post.
An update from my last post, and a couple more tips and questions for guard-rubocop:
In the directory where you install guard-rubocop, you also want to use rvm to specify a version of ruby and specific gemset for that directory. DO THIS BEFORE INSTALLING GUARD-RUBOCOP. So, go to whatever directory you want guard-rubocop installed in, and type:
echo '2.1.2' > .ruby-version
echo 'name_of_directory' > .ruby-gemset
Now to make these files take effect, change out of this directory and back in, like so:
This will generate wrappers for the direcotry and, you can now install guard-rubocop as per my last post, and it should fix the problem I faced before of needing to initialize guard every time i wanted to run it.
A few things I learned:
You can specify a directory or file for rubocop to inspect, instead of all files in that directory. Just run:
Rubocop can also autocorrect some offenses like so:
In addition, I finally figured out how to configure guard-rubocop, so I can specify which conventions (called cops) I want rubocop to notify me about, and how to modify some of the rules. After several failed attempts at following the guide at https://github.com/bbatsov/rubocop, I finally found the sought after /config/default.yml file. Alright, so here is how to configure guard-rubocop:
1) First follow all steps in my previous blog post and the beginning of this post.
2) locate the config/default.yml file. I did this by opening terminal and searching from my home directory by typing
$ find ~ -name "default.yml"
This gave me the following list:
These are all the locations where I installed guard-rubocop, and where the default.yml file is. I suppose you could use any of these files, because I assume they are all the same. I chose the one in the directory ruby-2.1.2, rather than any of the ruby-2.1.2@some_other_directory, so I copied the full path to the file on my clipboard (you need to copy your own path to that file, not mine):
If your terminal did not return any results for the search, go back and try installing guard-rubocop again. Phew!
3) Ok, now use the terminal to go to the directory where you want to install the modifications to guard-rubocop, and create a .rubocop.yml file. My preferred method of this to to type
This will create the file, and open a built in text editor where you can add text to the file. Add the following text (after the inherit_from: part, you can paste the path you copied before):
you can then quit the text editor by typing ctrl+x, pressing ‘y’ to save the changes, and pressing enter. You’ve now created the .rubocop.yml file, which list the modifications, or deviations from the default. Any lines added in this file will override the ones in default.yml.
The cops in the example above do the following:
This changes the maximum line length that rubocop allows. So I can now have lines up to 100 characters, instead of 80.
This stops rubocop from complaining about double quotes (which it does if you don’t use string interpolation or special characters).
This changes the prefered style of string formatting to ‘percent’ (it is format by ‘default’). Before I included this, rubocop would complain whenever I used string formatting like this:
Now it doesn’t complain (although I imagine it will now complain when you use sprintf or format)
You can peruse the default.yml file here to find other modification you might want to try. You can always go back to the .rubocop.yml file by typing
nano .rubocop.yml and editing the cops listed.
A question I still have is: What is “guard” vs. what is “rubocop”? I’m still new to the whole gem thing…
We’ve been using this great tool in the Da Vinci Coders class called guard-rubocop. It’s a gem for ruby that checks your code for conventions and good code grammar (is that a legit term?). Anyway, I’ll walk through how to install rubocop for a particular directory on your computer.
First, in your terminal, navigate to the directory where you want rubocop to patrol. I wanted to install it in my homework_assignments folder, so for me, this looks like:
$ cd workspace/davinci_coders_t2_2014/homework_assignments
Now, I like to do as much as I can from the terminal, so i typed
This creates a file called Gemfile, which is where your computer will look to find specific gems that you want to install. open the Gemfile in whatever text editor you use (I use Ruby Mine) and type the following:
Save the file and go back to your terminal. In the terminal type
This will look for the Gemfile, and install the gems listed in it.
One last step, which is to initialize guard by typing
You can then run
guard in the terminal from the directory in which it is installed, and it will detect any offensive violations in any of your ruby files within that directory.
The problem that I have encountered is that sometimes when I go back to that directory and try to run guard, it seems to not know what I’m talking about. I don’t know why this happens, but I think I’ve found a fix to this, which is to type
Any insight on why guard seems to forget that it’s there would be appreciated. Thanks!
Every time I learn a new shortcut, I get a little giddy. I don’t quite know why I’m so excited about them, but I am. Some (or all) of these I’m sure you already know, but I thought I’d post some of the recent shortcuts (and other helpful tips) I’ve learned recently.
General Keyboard Shortcuts (for mac)
- command + shift + [left or right arrow] (highlights from the cursor to the end (or beginning, depending on which arrow) of the row)
- command + ~ (switches between windows within the same application)
A note one that last one: I have used command + tab forever to switch between applications, but was always frustrated that I could not go between windows of the same application (chrome, for example). And now I can! Woo Hoo!
Ruby tips (works in Ruby Mine and Sublime Text. untested elsewhere)
- highlight some text, and press ‘ or ” and it will wrap the highlighted text in quotes. (if you already have a string, but want to change from single to double quotes, or vice versa, highlight the string, quotes included, and press ‘ or ” and it will replace the single(double) quotes with double(single) quotes) Awesome!
Command Line Shortcuts
- when changing directories (cd), start typing the directory, and press tab to autofill the matching directory
- cd .. (goes up one directory)
- touch file_name (this will create a file in the current directory. It can be any file type. examples below:
Welcome to my blog! My name is Aaron Apollo Glasenapp. I’m an aspiring ruby on rails web developer, and a hard core recreational mathematician. I’ll be posting anything from fun math/computer science problems to bug fixes to who knows what else.
I’m currently taking the Da Vinci Coders rails boot camp, and will be posting many things related to the class, including solutions to some problems I come across, and perhaps problems that I have not yet solved.