2 ~ Github

One of the most important things that a developer can learn, is how to make their process more flexible and to be protected against accidents.

The way this happens in the development world, is through software called “Source Control”. A source control application will keep copies of your files over time, allowing you to see the changes, and even go back to old versions. So, if you save a copy, then make some changes and realize you broke something, you can click a button and get the old version back, like nothing ever happened, no matter how long it has been, or even if you turned off your computer. I find myself doing things like, “I wonder what would happen if I… Oh, that is bad, let’s undo that whole file and pretend I didn’t do that.”

Usually, source control systems interact with a server, keeping a backup of all your work.

Modern source control software also helps with teams of developers working together. The software helps someone put together some changes and submit them to the group for review without actually changing anything. If the suggested changes are accepted, then the software moves forward.

As with all things, this has evolved over the years. A very popular source control software these days is called “git”. An easy way to have git without setting up your own server, is to use the online service, Github. Many projects use Github to coordinate teams of developers. This service is completely free, as long as you aren’t keeping anything private. (if you want to keep things out of the public view, they charge a small monthly fee)

Let’s get rolling with some hands on!

Step 0, Create an Account:

Please create an account in Github: https://github.com/

 

Step 1, Install:

Download and install the software: Github Desktop

This software will give you a graphical way to interact with Repositories. A repository is a collection of files that go together. Github and git can handle many file types, however, it is most efficient with text files (the most common type of source code file).

Step 2, Fork and Clone:

One of the big advantages of git, is that you are always working on copies of the files. You are never directly touching the main files. It also gives you a more free hand to try things, because if you do anything truly disastrous, you can always clone from the server again.

Once you have the software installed, go to the following link: https://github.com/DeveloperGym/DevGym_Project2_Github

On the upper right hand side, select “Fork”. Thinking about a “fork in the road”, a fork of a repository takes a complete copy as of this moment, and puts it in your account. Now, click on the green button on the right, “Clone or download” -> “Open in Desktop”. That should pop up the Github Desktop software.

This will prompt you to select a local folder on your computer to make a copy of the fun test project I created. Click “Clone” to finish the process.

 

Step 3, Branching:

We are going to create a branch to play around in. A branch is another separate version of the files.

In the Github Desktop software, click on the small button to the left of the word “master”. If you hover over the button, it will say “Create new branch”. Branch names are usually short, and don’t contain spaces. Dashes and underscores if you need a separator. For this, you can name your branch something like: “YourNameLearning”

Step 4, Making a Change:

Now that we have cloned the repository, and made our own branch, go ahead and open the folder where you copied the files. If you don’t remember, open the “Repository” menu item and choose, “Show in Explorer”.

Lets make a change to the file “SuperImportantSourceCode.txt”. Change anything you like. Add something, remove something, whatever. Then save the file and close it.

Go back to Github Desktop, and you should see a small circle next to the Changes button. This tells you that Github has noticed a file changed. Click on the “SuperImportantSourceCode.txt”, and you’ll see line by line, exactly what changes you made. Red lines are removed from the file, and green lines are added.

Step 5, Made a Mistake:

Oops, we made a mistake, and never want to see it again! Right click on the file name in Github Desktop, and select “Discard Changes…”, then “Discard Changes” to confirm what you are about to do. Now, if you open the file again, you’ll see anything you changed has been put back the way it was.

 

Step 6, Saving a Change for Good:

Okay, make another change, and save the file again. Once again, you’ll see the change appear in Github Desktop. This time, we are going to save it.

Inside of Github, click on the file, and verify that it is showing you the change you just saved. Also make sure that there is a checkmark in the box to the left of the file name. This is called “Staging” the file. Meaning that we want to keep the changes in the file.

Down below, there is a “Summary” and “Description” boxes. At the moment, you’ll notice that there is a disabled button at the bottom “Commit to (branch name)”, this is because you aren’t allowed to save the file to the repository without a short explanation. Please put something like “Learning how to use Github” in the summary box. You may also add a more detailed Description if you feel like it.

Now that the button has been enabled, you can click on “Commit to (branch name)”

After you Commit your changes, click on the “History” button in the upper left. That will show you all the changes that have been made to this project (repository), including yours now!

Step 7, Pull Request:

Click on the “Publish branch” button. This will save your changes up to the server. Now, if your computer crashed, or you moved to a different computer, your changes are safe and accessible from any internet connected device.

Click on the menu item “Branch”, and select “Create pull request”. A pull request tells the main server, that you have a change you would like to have integrated back to the main repository you forked from that everyone else is using. The Github Desktop software opens a web page, with the Pull Request ready for you to finalize. Once you have updated the Summary and Description the way you like it, finish up by clicking “Create pull request” on the web page.

In a real project, other team members would then look at the changes, make comments, or “Approve” the changes. Once the repository rules are met, then your changes will be “merged” or brought in to the main branch.

You can see your Pull Request under the tab, “Pull Requests”:

Step Infinity, Success (and limitations):

This was meant as a small introduction to the power of git, but is a great, safe way to learn about backing up your working files, and sharing them with the development world.

There is a lot more to git and Github than what you’ve seen here. Even the desktop app you’ve been using only has a very tiny amount of the features that git supports.

Future posts will cover more advanced topics… Like Gitflow and common git commands.

Further reading can be found via Tattered Cover, a great independent book store, here is the online search for Github books: http://www.tatteredcover.com/search/site/github

Leave a Reply

Your email address will not be published. Required fields are marked *