PreliminariesWe will be using GitHub in this class for assignments.Everyone needs to create a GitHub account if you don’talready have one. Do this at:https://github.com/After you set up your Github account you will need to informus what your GitHub username is. Their will be instructions forhow 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. (Runthe installer again and search for the git program.) To see if you have git installed, type the commandwhich gitIf 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 areman pages for each Git operation, too. For example, these allwork:% man git% man git-clone% man git-checkout Ry's Git Tutorial by Ryan Hodson is available in several forms. PeepCode Git Internals by Scott Chacon reveals some of Git'sinnards.A free online copy of the book Pro Git by Scott Chacon and BenStraub is available at
What is Git?Git is a version control system The essential job of a version control system is to maintaina history of changes to a collection of files.
What is Git?Git is a version control system The essential job of a version control system is to maintain ahistory of changes to a collection of files. This is more than just a text list of what has changed in aproject, but a sort of “time machine” that can recreate exactlywhat the files in your project looked like at particular points intime
What is Git?Git is a version control system The essential job of a version control system is to maintain ahistory of changes to a collection of files. This is more than just a text list of what has changed in aproject, but a sort of “time machine” that can recreate exactlywhat the files in your project looked like at particular points intime In association with github git can also add in coordinating aproject between programmers and in working on a project onseveral machines.
MotivationSuppose you were working on a project in a directory calledmyGreatProj that contained many source files, you haveno tool like git, but you want to maintain some sort ofversion control. What could you do?
MotivationSuppose you were working on a project in a directory calledmyGreatProj that contained many source files, you haveno tool like git, but you want to maintain some sort ofversion control. What could you do?You might make multiple copies of the directory manually:% cp -R myGreatProj myGreatProjv1and after you do more work you do it again to a new name:% cp -R myGreatProj myGreatProjv2What are some disadvantages of this system?
MotivationWhat are some disadvantages of this system?1. It’s clunky2. It takes up a lot of space.a. You are saving copies of files that haven’t changedb. You are saving complete files even if only a smallchange 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 in2.)4. It’s hard to navigate. (Which version had what change?)
Local Version Control Systems A Version Control System VCS automates the process. The user only has one version of the project she has to deal with.It saves the changes (say in a database) and the user “checks out” aversion of the project.This process took place on a programmer’s local computer. There wasno good way to share code amongst several developers.
Centralized VCSIn a Centralized VCS instead of storing things locally, the project wassaved on a server and multiple users could check out files to work on.CVS was an example of such a VCS.
Distributed VCSGit is a distributed version control system. Every developer on a projectwill have on their machine a copy of a repository for that project.
GitHubGit 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 git is a distributed VCSdeveloped in 2005 for use by the Linux communityBeing used to manage the Linux kernel forcedseveral requirements on git including: ReliabilityEfficient management of large projectsSupport for distributed developmentSupport for non-linear development
git configBefore you start using git you might want to do someconfiguration. First set your name and email address:% git config --global user.name "Eric Anson"% git config --global user.email [email protected] you should use your own name and email. The globalflag tells git to save these values in your home directory and thusyou use them for every git project you work on.When you save something to the repository (commit a change) and youdon’t give a description of the change, git will invoke an editor for you toenter a description. By default that editor is vim (if it’s installed). You canset which editor is used via:% git config --global core.editor your editor choice
git configLastly, when you interact with github is will ask you foryour user id and email. To have it cache your credentialsfor a day you can set:% git config --global credential.helper "cache --timeout 86400"
using gitYour files for any project will be organized inside a directory.Usually in this class that directory will be created from arepository on github. If you are working on a private project, youcan use git locally:% mkdir myProject% cd myProject% git initThe commands above create a directory called myProject,move into it, and then and then the git init command willchange that directory into a repository.
using gitCreating a git repository creates a hidden directory called .gitwhich contains all the tracking information.%%%%.mkdir myProjectcd myProjectgit initls -a. .gitThe only difference between a normal folder and a Gitrepository is the .git directory. You do not want to manuallychange anything in it.
using gitYou can add files, edit files, and delete files from this directoryand these changes will not be saved by git until you tell it to. Asan example, suppose you add the files README and prog1.pyto your directory. Now invoke the command:% git statusYou will get a result like something shown on the following slide:
using git% git statusOn branch masterNo commits yetUntracked files:(use "git add file ." to include in whatwill be committed)READMEprog1.pynothing added to commit but untracked filespresent (use "git add" to track)
git addThis indicates that the files are not under version control(untracked). You need to tell git to add these files to therepository if you want to track them. To do this you must firststage a snapshot. The command:% git add READMETells git that the next time you take a snapshot, you want to includethe file README. To also include prog1.py you can follow with:% git add prog1.pyGit add will also take multiple arguments so we could have justtyped:% git add README prog1.py
git commitNote staging a snapshot does not save the state of your files, itjust indicates what will be saved. To actually do the saves youuse the command:% git commitThe git commit command saves the staged changes along with adescription of what those changes are. With no options, git commitopens a default editor for you to write a description of this snapshot.To avoid this step you can use the -m option to include thedescription with the command:% git commit -m “Files README & proj1 added”
git so farSo we’ve seen three stages of working on our project:1. Edit the files in your directory2. Stage the changes to save in next commit3. Commit the changes% git statusWill tell you which files are changed and what is staged.
git logTo see a history of your commits you can use the command:% git logThis will list out all the commits made, who made them, and thedescription written for each one. It is pretty wordy. Most often youwill want to use the option --oneline to condense the output:% git log --onelinea893df6 (HEAD - master) Text added to README214ff25 Files README and prog1.py createdThis shows only a hash identifier for the commit and the descriptionof the change. In this case there were two commits to this repository.
So farThis diagram indicates how to think of git. Use addto stage changes, use commit to save snapshots.status tells you about the working directory and thestage. log tells you about the committed snapshots.
git showYou can see more information on some committed snapshot usingthe git show command% git log --onelinea893df6 (HEAD - master) Text added to README214ff25 The files README and prog1.py arecreated% git show a893df6Notice you use the hash number to identify the snapshot you wantinformation on.
git checkoutYou can use the git show checkout command to view what theproject looked like at a particular snapshot% git log --onelinef2e80fc (HEAD - master) added code to prog1a893df6 Text added to README214ff25 The files README and prog1.py arecreated% git checkout a893df6This command changes all the tracked files in your directory to beas they were at the time of the snapshot. Even the log will look as itdid at the time.
git checkoutSo have we lost all the changes since that snapshot? No, we havejust changed our view. Look at the following diagram:This represents our snapshot history. The red circle is thecurrent view, and the faded circle is the latest version.We can always restore back to the latest version.
git checkoutYou can return to the latest snapshot by using the command:% git checkout mastermaster is the name of the main branch of your repository. Wewon’t talk much about branches, but you can read about themif you’re interested.
undoing a committed changeYou can undo a committed changed using the revert command:% git log --oneline9d4df69 Add an experimentc40879b add some stuff8a9e200 Added two file stubsbae3928 First draft% git revert 9d4df69This command will bring you in an editor to record a comment andthen undo the changes done in the commit identified. It doesn’tdelete the history, but adds a new snapshot that has the changesundone.
undoing a committed changeHere is what the log looks like after the commands of the lastslide:% git log --oneline2e136b2 Revert "Add an experiment"9d4df69 Add an experimentc40879b add some stuff8a9e200 Added two file stubsbae3928 First draftA graph of the snapshots:
undoing uncommitted changesThe command:% git reset --hardChanges all tracked files to match the most recent commit. Thischange cannot be undone, so use it with care. Without the --hardoption the reset command clears the staged snapshot.The command:% git clean -fdeletes all untracked files from the directory. This is another changethat cannot be undone. The clean command does have options thatcheck what will be deleted that you might want to use.
working with GitHubMost of the time in this class you will not create a repository withthe git init command, but instead copy a repository from the web.GitHub lets you create a repository using a web browser, and thencopy it to your local machine using:% git clone url given by GitHub For example% git clone https://github.com/csc210dev/sampleCloning into 'sample'.Unpacking objects: 100% (10/10), done.Checking connectivity. done.
working with GitHubAside from clone, there are only two more commandsyou have to learn for working with GitHub. Thecommand:% git pushWill push your repository up to the one stored on GitHub.This is a method of backing up your work to the cloud andalso turning in your assignments. You must push yourwork to GitHub by the due date.
working with GitHubThe command:% git pullPulls changes from the repository on GitHub to your localmachine. This can be useful if you want to work on differentmachines. If you do this, just be careful to always start yoursessions with a pull and end them with a push to make sureyou’re working with the latest copy.If you are not working on different machines, you probablywon’t use this command unless we use it as a way to give youfeedback.
working with GitHubNote that unless you pay for it, the repositories youcreate for on GitHub are public, meaning anyone canclone them. This is not true for our assignments. Whenyou accept an assignment for this class, GitHub willcreate a private repository for you on its server andgive you it’s url. You then use that url in a clonecommand on your own machine.
Assignments with GitHub ClassroomAn invitation link will be posted on Piazza and the class web gIHitting the link will take you to a page where you can "Accept thisassignment". Accepting it creates a repository with a URL likehttps://github.com/csc210f17/aNUMBER-GITHUB IDDo a git clone:git clone https://github.com/csc210f17/a2-jsmith a2Use git add and git commit commands to commit your work to theassignment-specific repo on your machine and then git push to copythose commits into your assignment-specific repo on GitHub.
ConclusionThese slides only scratch the surface of all you cando with git. I encourage you to read some of thedocumentation, look at man pages to see differentoptions, talk to peers about git, and most importantlyEXPERIMENT.