Skip to content

Instantly share code, notes, and snippets.

@aikalima
Last active December 31, 2015 21:09
Show Gist options
  • Save aikalima/8044630 to your computer and use it in GitHub Desktop.
Save aikalima/8044630 to your computer and use it in GitHub Desktop.

A short intro to git and github

Goals

  • Understanding git and github
  • Understanding the basics of managing files
  • Learn how to collaborate with others

Motivation

Imagine being a fiction writer, a fairly solitary occupation. You sit at your desk typing away in MS Word (?!). You are smart and make back-up copies of your work once in a while, perhaps to an external drive or flash memory stick. When a chapter is finished, you want your editor to review and annotate it. What do you do? Perhaps you save a copy and name it "KlingonWars_Chapter1_V_1-12-19-2013.doc", zip it up, attach it to an email and send to the editor. She opens it up, turns on 'track changes' and starts editing. When done, editor saves a copy KlingonWars_Chapter1_V_1-12-15-2013_reviewed_12-2-2013.doc and sends it back to you etc.

That's a simple workflow for two people collaborating on a single document. I'm sure many of you have experienced this in some form, perhaps writing a paper in school with a classmate? What are some of the problems?

  • What if you forget making backup copies?
  • What if your hard drive crashes and the most recent backup was from two days ago?
  • How to revert to a previous version, or view the version history?
  • After a while, you have many copies of different versions of the same content. Document management becomes a challenge (what's the latest version?)
  • What if a third person enters the picture - the workflow breaks down
  • How to merge different versions into a single, master document?
  • What about permissions to make changes, how to enforce?

Now enter software programming. Programming is not a solitary activity, it's a highly team-based activity. For example, over 600 engineers collaborate on on Mac OS, 40 engineers work on "Angry Birds".

So how do you collaborate in software projects, what's a useful workflow? Enter git and github.

git

The purpose of git is to manage a project, or a set of files, as they change over time (version control).

github

It's a social network build around git. I has has completely changed the way we, as programmers, work. Started as a developer's collaborative platform, GitHub is now the largest online storage space of collaborative works.

###Git basics

Check that git is installed

git --version

Let's put some code into git. Use ruby code that you worked on earlier today. cd into the code folder.

cd my_code

create a local git repository:

git init

The current folder becomes your workspace (a.k.a 'branch'). By default, git refers to it as 'master'. You do this only once per project.

Add files to the repository, specify the file you want to add or '.' for everything in the current folder:

git add test.rb

Commit file changes to the repository, the option '-m' allows you to attach a submit comment:

git commit -m 'my first commit' test.rb

Check git log - it shows the file history: version numbers, dates, submit comments

git log test.rb

Now mess with the file and save changes, or delete the file. How do you revert file, or recover it?:

git checkout -f test.rb

Now make some changes that you want to keep. After saving the file, check the file status:

git status test.rb

You can also check differences between latest commit (or any version) and what is currently in the workspace:

git diff test.rb	

Commit first real change. Always provide a descriptive comment that helps you and others to understand the nature of the change:

git commit -m 'added reverse() method' test.rb

My personal workflow usually looks something like

Do some programming.
git status to see what files I changed.
git diff [file] to see exactly what I modified.
git commit -m 'a message' to commit.

Now, everything is still local, on your computer. What if the hard drive crashes, or your laptop is stolen? Or you want to share files with you fellow students/project mates? Enter github

###github

Got to github.com and explore. Some interesting repositories:

https://github.com/ruby - ruby source code
https://github.com/facebook - facebook on github
https://github.com/google - google on github

If not already done, create an account and login

Configure your environment to work with your github account:

git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

Check configuration:

git config --global user.name
git config --global user.email

Ok, good to go.

Create a repository on github Find the corresponding function in the github UI, call the repo 'first_project'

Now, in your shell, link the newly created gihub reporitory to the local workspace on your computer. The following line reads link the remote repository first_project.git which shall be known as 'origin' to my local workspace

git remote add origin git@github.com:aikalima/first_project.git

Push changes to remote - you may have to authenticate with your github credentials. The following line reads upload what is in my local workspace which is called 'master' to github, which is referred to as origin

git push origin master

Verify that changes made it to github. Now make a local change, commit and push to github:

make change
git commit -m'my comment' .
git push origin master

Did change make it on to github? Now delete your local directory. Panic, it's all gone. No worries, to recover:

git clone git@github.com:aikalima/first_project.git

Collaborating

Assumption: by now, you all have a repo on git hub. The real power of Git comes out when you are collaborating with others on a project. If you own a repo, you can ask others to collaborate with you as contributors.

###Lab

Get in pairs of 2, designate person A and B. Ask B to make a change to your source code file, for example add a ruby comment.

What are the steps?

Make B collaborator - Under the repo, go to Settings > Collaborators and add using their github username or email.
B clones A's repo on their local machine
B makes a change and commits
B pushes change to git hub
A picks up changes - (can you find the right git command?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment