GitHub is a Web-based repository hosting service for git
. You can upload your repositories to it and then access/manipulate them with a nice Web-based interface for many of the most common git
commands. You can create as many public repositories as you want; you have to pay if you want to keep your repositories private. GitHub then adds new services on top of git
that are designed for collaboration:
I will not attempt to present a comprehensive introduction to GitHub. Features that I don't cover can become the topic of YOUR presentations. Take a look at <https://github.com/features> for more info.
Instead, I plan to cover:
git pull
and git push
commandsI assume that you:
Set Up Gitinstructions at GitHub Bootcamp
git clone
, git fetch
, git pull
, and git push
requests, GitHub will ask you for your password or passphraseThe first thing that needs to happen to work with Github is linking a repository on your computer to one that exists on Github. There are two primary ways to do this.
git clone
command.Let's explore both of these scenarios.
git clone
commandIf the repo exists on GitHub already, then go to the repository's page on GitHub and look for the SSH clone URL
Once you have located that URI, perform the following steps:
cd
to a directory where you want the repository to be located (~/Projects
is one option.)git clone
command: git clone <URI>
where <URI>
is the link you copied from GitHub in step 1.Here is a graphical depiction of what the git clone
command does.
git clone
configures the repository to remember where it came from. You can verify this with the git remote
command. For one of my GitHub-based repos, the git remote -v
command lists:
origin git@github.com:kenbod/icse2016.git (fetch)
origin git@github.com:kenbod/icse2016.git (push)
This output indicates that my local repository is associated with a remote copy called origin
(the first remote associated with a repository is called origin
by convention) that is located on GitHub. A repository can be associated with any number of other repositories and can pull and push from any of them.
On a repository that has no remotes, the git remote -v
command produces no output.
If a repo exists on your computer but not on GitHub, then to create a copy on GitHub that can be linked to your local copy, perform the following steps:
Fill out the dialog below; enter a name for the repo and skip the initialization steps that GitHub can perform.
After the empty repository has been created, GitHub provides helpful instructions on how to establish the link:
As an example, I took the (very) simple repo I created during the Git lecture and uploaded it to GitHub. I named my new empty repo on GitHub example_project
to match what I called the repo on my laptop. Here's what happened:
Things to note:
git remote
command is first used to add a remote repository, then used to list our local repository’s remotes.git push
is used to push the contents of our master branch to the remote repo AND to configure the local branch to trackthe remote
git branch -a
is used to list all branches including the ones on our remotebug-fix
that did NOT get copied to the remotegit clone
tracking relationshipbetween them.
tracksa remote branch will allow
git pull
commands to copy commits from the remote branch that were added in some other way (typically by being pushed to that branch by one of your collaborators)git push -u <repo> <refspec>
git push
We are just scraping the surface of the git push
command. The generic form of the command is:
git push <options> <repo> <refspec>
The <repo>
argument ALWAYS refers to a remote repository. If you don’t specify it, then origin
is assumed.
The <refspec>
argument is a placeholder for this beast: (+)<src_ref>:<dst_ref>
src_ref
is a refspec that references your local repo; dst_ref
is for the remote<src_branch>:<dst_branch>
or, in our case, master:master
<refspec>
, your current branch is assumedPutting it all together
git push -u origin master
This command means that we want to push the contents of the master
branch of MY repository to the master
branch of the remote repository (creating the branch, if needed) AND set up a tracking relationship between them.
After we set-up the tracking relationship, we can push new commits to the remote copy of the master branch using any of these commands
git push origin master:master
git push origin master
git push
The last command assumes that your current branch is master
.
If you have new commits that you want to push to the remote but someone else already pushed THEIR new commits to the remote, you will need to pull them in first using ANY of these commands:
git pull origin master
git pull
git pull
is a short cut
in that it does the following:
As a result, if you just want to pull changes from the remote repository but not modify any of your branches just yet, you can just issue a git fetch
command and then do any merging into branches yourself.
Regardless, once you have pulled, you can resolve any conflicts that might have popped up and then you can push your own commits to the remote branch
Finally, going back to our example, if we want our bug-fix
branch from our local repository to be stored on the remote, then we can just push it using the same command that we did before. Here's how:
git checkout bug-fix
git push <== fails, because remote branch doesn’t exist yet
git push -u origin bug-fix <== works!
That's it for our review of GitHub.
Next up, head to the material on Strategies for GitHub Presentations or head back to the Table of Contents.