What is Github?
So far, everything we did in Git was done locally, on our computers. What happens when we want to collaborate with others, or save a back up of all our work. We can push (upload) our work to repositories hosted by companies like GitHub. GitHub is essentially a service that allows us to store our work on their servers and let us share and collaborate with others.
Why is it so good for collaborations? Because GitHub takes care of user permissions. It has interfaces for you to comment on code, places to raise and keep track of issues of your project. And it runs the server that stores your work, so you don't have to take care of any of that when you are working in a team.
When you push your work onto GitHub, it's similar to syncing your files with Dropbox. We mostly use Github for collaboration and hosting our code in the cloud.
Sign Up
Lets experience first hand the wonders of GitHub, first go to https://github.com/ and sign up for an account.
New Remote Repository
Once you have signed up, lets create a new remote repository on GitHub. Go to the Home Page, at the top right hand corner, you should see a +
sign, click it and choose "New repository".
Give your new repo a name, I named mine "myDogsDiary". Then click "Create repository" at the bottom.
Now you are on a Quick setup page. With a few options to choose from. We are going to clone our new repository onto our computer. Make sure your link is HTTPS.
Go to your terminal, navigate back to your home directory ~
and type in the following, except replace your link with mine.
git clone https://github.com/xiaopow/myDogsDiary.git
This will clone your new remote repository onto your local computer. You should have a folder in your home directory with the same name as your remote repo name. Navigate into your new repo.
Create a new text file $ touch dog_diary.txt
, open it with Atom and add today's entry "1-1-2017: A new year, a new me, no more biting the sofa.". Save the file. Add the file to staging and make a commit. Now, enter $ git remote --v
and you will see details of your remote repositories. origin is the name of the remote, this is where your GitHub remote is stored. We can push our local changes up to our remote repository.
$ git push origin master
git push
is a git command where we are pushing our local repo to another place. origin
is the name of the remote, it contains the URL of your GitHub repository, and master
is the local branch that we want to push. When it's done, your local changes is on GitHub.
If you go back to GitHub and navigate to your repository page, you should see your new file "dog_diary.txt" show up.
What if you are working with a team mate and he made some changes to the master branch and pushed the updates to the shared GitHub repository. You want to keep your local repository's master branch in sync with the latest changes. To download updates from github, you use git pull
, git pull origin master
will pull down the master branch from the repo on GitHub and update your local repo's master branch. If we try it now, it will say Already up-to-date
because nothing has changed on origin master.
Forking
Another neat thing about GitHub is that you can copy other people's repositories into your own account and work on it as your own. This copy action is called forking. Like splitting from one time line into two. If you go to the Ruby Koan repository created by Neal https://github.com/neall/ruby_koans. You can see the Fork button on the top right below your profile picture.
This GitHub repository has been forked by 1615 users already. Lets fork it once more. Click the Fork button and choose your account to fork to. You should see that you have the ruby_koans repository under your account name, and it's forked from neall/ruby_koans.
This repository is essentially what you downloaded and worked on in the first couple weeks of the course. You can clone this repo to your local machine or you can just leave it as is.
Adding a GitHub remote repository to your existing local repo
Sometimes, we might start a new project locally and later on decide to make a backup of our project on GitHub. In this case
- We can create a repository on GitHub
- Add it's address to our local repo
- Then push our repo to GitHub.
I want you to
- Create a new folder somewhere in your home directory. Name it myCatsDiary this time.
- Init it as a git repository.
- Add a file called README.md
- Open this file with Atom and type something inside then save it.
- git add your file and make a commit with a commit message.
- Now go to GitHub and create a new repository called myCatsDiary.
You should see the following after creating the remote repository.
We can add the address of our GitHub repository to our local repository and then push our work to GitHub. You do this using the command git remote add
-> $ git remote add origin <url>
, where origin
will become a shorthand for the url
. The url in my case is 'https://github.com/xiaopow/myCatsDiary.git', yours will have your account name instead. Run that command in your terminal then run $ git remote --v
, you should see your GitHub repo address successfully added as the origin remote. Now push your work to GitHub with $ git push -u origin master
. -u
is a flag for setting your upstream remote for the first time. So that's it, you have learned the basic operations of git and GitHub.
Below is a cheat sheet for git you can use as reference.
Git Command
Key/Command | Description |
---|---|
git init | have git start tracking all code changes in the current folder and all current and future sub-folders |
git status | get useful information on your git repository: what files you've added, etc |
git add [-options] | stages your changes |
staging - records all of your code changes to a stage file, also called an index file | |
git log | The history of commits |
git add -A | stages the changes from All files |
git commit [-options] | takes a snapshot of all your changes and generates a unique identifier for this commit called a HEAD that you can use later to roll back to if needed |
For example, git commit -m "Completed form validations." will commit your changes with a short message |
|
git stash | saves the state of your half-done work |
git stash apply | re-apply the state of your half-done work |
git checkout -b <branch-name> |
creates a new branch and puts you in it |
For example, git checkout -b user-login will create a new branch called "user-login" and any future code will be committed to this branch |
|
Will not allow creating unless current code has either been committed or stashed | |
git checkout <branch-name> |
moves you into a new branch |
For example, git checkout master will move you into the branch called "master" |
|
Will not allow creating unless current code has either been committed or stashed | |
git merge [origin-branch-name] | merges code changes from the origin branch into the branch that you are currently in |
For example, If you are currently in the master branch: | |
git merge user-login will merge all code from your "user-login" branch into your master branch |
|
git reset --hard HEAD | resets your code to a previous commit |
WARNING: this command throws away all of your un-committed changes | |
For example, git reset --hard 820f417 |
Git Command for working with GitHub remotes
Key/Command | Description |
---|---|
git fetch | imports commits from a remote repository into your local repo |
git fetch <remote> <branch> |
|
For example, git fetch origin user-login |
|
git merge | merges branches after a fetch |
git pull | fetch and merge in one command |
For example, git pull origin user-login |
|
fetches code from your remote origin "user-login" branch and merges it with your local code | |
git push <remote> <branch> |
transfers commits from a local repo to a remote repo |
For example, git push origin user-login |
|
pushes the local code in your "user-login" branch to your remote origin "user-login" branch |