Rys Git Tutorial
Rys Git Tutorial
Preliminaries
We will be using GitHub in this class for assignments.
Everyone needs to create a GitHub account if you don’t
already have one. Do this at:
https://github.com/
After you set up your Github account you will need to inform
us what your GitHub username is. Their will be instructions for
how to do that in the near future.
Preliminaries
● We will be using the command line version of git.
○ If you are using Cygwin you may need to install it. (Run
the installer again and search for the git program.)
○ To see if you have git installed, type the command
which git
If it gives you a location of git, then you have it.
Resources
● There is a man page for the top-level git command but there are
man pages for each Git operation, too. For example, these all
work:
% man git
% man git-clone
% man git-checkout
● A free online copy of the book Pro Git by Scott Chacon and Ben
Straub is available at
1. It’s clunky
2. It takes up a lot of space.
a. You are saving copies of files that haven’t changed
b. You are saving complete files even if only a small
change was made.
c. You may be saving files you don’t care about (e.g.
test files, object files, compiled code)
3. It’s slow. (You’re copying all that extra stuff mention in
2.)
4. It’s hard to navigate. (Which version had what change?)
Local Version Control Systems
● A Version Control System VCS automates the process.
● It saves the changes (say in a database) and the user “checks out” a
version of the project.
● The user only has one version of the project she has to deal with.
● This process took place on a programmer’s local computer. There was
no good way to share code amongst several developers.
Centralized VCS
In a Centralized VCS instead of storing things locally, the project was
saved on a server and multiple users could check out files to work on.
CVS was an example of such a VCS.
Distributed VCS
Git is a distributed version control system. Every developer on a project
will have on their machine a copy of a repository for that project.
GitHub
Git provides ways to transfer groups of changes between repositories.
Git is typically used with a web-hosted service that maintains the "official"
repository for a project. We'll be using GitHub.
git
Obviously you should use your own name and email. The global
flag tells git to save these values in your home directory and thus
you use them for every git project you work on.
When you save something to the repository (commit a change) and you
don’t give a description of the change, git will invoke an editor for you to
enter a description. By default that editor is vim (if it’s installed). You can
set which editor is used via:
Lastly, when you interact with github is will ask you for
your user id and email. To have it cache your credentials
for a day you can set:
% git config --global credential.helper "cache --timeout 86400"
using git
Your files for any project will be organized inside a directory.
Usually in this class that directory will be created from a
repository on github. If you are working on a private project, you
can use git locally:
% mkdir myProject
% cd myProject
% git init
% mkdir myProject
% cd myProject
% git init
% ls -a
. .. .git
% git status
You will get a result like something shown on the following slide:
using git
% git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what
will be committed)
README
prog1.py
Tells git that the next time you take a snapshot, you want to include
the file README. To also include prog1.py you can follow with:
Git add will also take multiple arguments so we could have just
typed:
% git commit
The git commit command saves the staged changes along with a
description of what those changes are. With no options, git commit
opens a default editor for you to write a description of this snapshot.
To avoid this step you can use the -m option to include the
description with the command:
% git status
Will tell you which files are changed and what is staged.
git log
To see a history of your commits you can use the command:
% git log
This will list out all the commits made, who made them, and the
description written for each one. It is pretty wordy. Most often you
will want to use the option --oneline to condense the output:
This shows only a hash identifier for the commit and the description
of the change. In this case there were two commits to this repository.
So far
Notice you use the hash number to identify the snapshot you want
information on.
git checkout
You can use the git show checkout command to view what the
project looked like at a particular snapshot
Changes all tracked files to match the most recent commit. This
change cannot be undone, so use it with care. Without the --hard
option the reset command clears the staged snapshot.
The command:
% git clean -f
deletes all untracked files from the directory. This is another change
that cannot be undone. The clean command does have options that
check what will be deleted that you might want to use.
working with GitHub
Most of the time in this class you will not create a repository with
the git init command, but instead copy a repository from the web.
GitHub lets you create a repository using a web browser, and then
copy it to your local machine using:
For example
% git push
The command:
% git pull
Hitting the link will take you to a page where you can "Accept this
assignment". Accepting it creates a repository with a URL like
https://github.com/csc210f17/aNUMBER-GITHUB_ID
Do a git clone:
git clone https://github.com/csc210f17/a2-jsmith a2
Use git add and git commit commands to commit your work to the
assignment-specific repo on your machine and then git push to copy
those commits into your assignment-specific repo on GitHub.
Conclusion
These slides only scratch the surface of all you can
do with git. I encourage you to read some of the
documentation, look at man pages to see different
options, talk to peers about git, and most importantly
EXPERIMENT.