Version Control with Git, HTML Basics
Welcome to CS-5630 / CS-6630 - Visualization. In this class we will use a mix of traditional slides and interactive documents where we ill go through HTML, JavaScript, etc. These notes are meant for classroom use but also for reading them offline. This is our first coding lecture. We will cover the basics of version control with git, as well as the basics of the web stack.
Git
We will be using a version control tool called git to track changes to our code. We’ll also be using Github, an online tool for hosting git repositories.
You should already have git installed, if not see the official documentation on how to install git on your operating system.
Why version Control?
- Keep copies of multiple states of files By committing you record a state of the file to which you can go back any time.
- Create alternative states Imagine you just want to try out something, but you realize you have to modify multiple files. You’re not sure whether it works or is worth it. With version control you can just create a branch where you can experiment or develop new features without changing the main or other branches.
- Collaborate in teams Nobody wants to send code via e-mail or share via Dropbox. If two people work on a file at the same time it’s unclear how to merge the code. Version control lets you keep your code in a shared central location and has dedicated ways to merge and deal with conflicts.
- Keep your work safe Your hard drive breaks. Your computer is stolen. But your code is safe because you store it not only on your computer but also on a remote server.
- Share You developed something awesome and want to share it. But not only do you want to make it available, you’re also happy about contributions from others!
Types of Version Control: Central Repository
- Everybody needs to write to one server
- All operations (history, commit, branches) require server connection
- The traditional model: CVS, SVN, etc.
- Pros:
- Simple
- Cons:
- Complex for larger and community projects
- Who is allowed to write?
- How do you apply changes that someone outside your team made?
- Complex for larger and community projects
Types of Version Control: Distributed Version Control
- Everybody has a full history of the repository locally
- No dedicated server - every node is equal.
- In practice: often server is used for one “official” copy of code. But: server by convention only, no technical difference.
- Pros:
- No access issues
- Make a copy and hack away
- Ask if partner wants to accept your changes
- Everything is local
- Fast!
- No internet connection required
- Commit often model (once per feature) - don’t sync all the time.
- No access issues
- Cons:
- Extra effort to distinguish between committing and pushing/pulling (synchronizing).
Implementations
- Centralized
- CVS
- SVN
- Team Foundation Server
- …
- Distributed
- git
- Mercurial
- …
- We will be using git in this lecture.
git
- Created by Linus Torvalds, 2005
- Meaning: British English slang roughly equivalent to “unpleasant person”.
- git – the stupid content tracker.
I’m an egotistical bastard, and I name all my projects after myself. First ‘Linux’, now ‘git’. – Linus Torvalds
Why git?
- Popular (~50% of open source projects)
- Truly distributed
- Very fast
- Everything is local
- Free
- Safe against corruptions
- GitHub!
git model
Whiteboard sketch of git with a server. A git repository is essentially a large graph.
git tutorial
This is a quick intro to git, used in combination with GitHub. This is not a complete tutorial, but will use the most important git features.
We start by configuring git
Make sure that his is set to your official school address and your correct name!
Create a folder for your project
Initalize the git repository
What does git do to your file system?
Now let’s create a file
Let’s add it to version control
Let’s look at what is going on with the repository
That means: git knows that it’s supposed to track this file, but it’s not yet versioned.
Let’s commit the file. Once a file is committed, it’s state is recorded and you can go back to previous versions any time.
That means that now the file is tracked and committed to git. But it’s still only stored on this one computer!
Next, we change a file and commit it again.
Through this cycle of editing, adding and committing, you can develop software in a linear fashion. Now let’s see how we can create alternate versions.
Branching
Now let’s create a branch
We have two branches, draft and master. The * tells us the active branch (the HEAD).
The files in your folders are in the state as they are stored in the active branch. When you change the branch the files are changed, removed or added to the state of the target branch.
Let’s switch the branch.
We have now written changes to the new branch, draft. The master branch should remain unchanged. Let’s see if that’s true.
The text we added isn’t here, as expected! Next we’re going to change something in the main branch and thus cause a conflict.
At this point we have changed the file in two different branches of the repository. This is great for working on new features without breaking a stable codebase, but it can result in conflicts. Let’s try to merge those two branches.
These are the basics of git on a local server. Now we’ll learn how to sync with other people. This can be done with just git, but we’ll be using GitHub as we’re also using GitHub in the homeworks.
Working with GitHub
First, we’ll create a new repository on github by going to https://github.com/new.
Now let’s clone the repository from GitHub.
This creates a local copy of the (empty) GitHub repository. We will just start working with that and commit and push the code to the server. If you’d like to add an existing repository to GitHub, follow these instructions.
We have now committed a file locally and pushed it to the server, i.e., our local copy is in sync with the server copy.
Note that the git push
command uses the origin defined in the config file. You can also push to other repositories!
Next, we will make changes at another place. We’ll use the GitHub web interface to do that.
Once these changes are done, our local repository is out of sync with the remote repository. To get these changes locally, we have to pull from the repository:
Other GitHub Features
- GitHub Issues Github Issues are a great way to keep track of open tasks and problems. Issues can be references and closed from commits.
- Forking Forking is essentially making use of the distributed nature of git, while having the benefits of a server. When you fork a repository you make a clone of someone else’s code that you are not allowed to read. The repository appears in your github account and you can start editing the code. If you think you improved the code, you can send a “pull request” to the original owner. The owner can then review your code and merge your modifications into his main repository. Forking is hence virtually the same as branching, with the exception that it resolves issues with write permissions.
GUI Clients
- GitHub Desktop Good option if you want a GUI client. Download here
- Integrated in IDEs Many operations can be done out of a IDE such as WebStorm
Getting updates to the homeworks
The homeworks are hosted in a git repository. Every time we release a homework we will just update the repository. You can then pull from that repository to get the latest homework on your computer.
To get the homework repository, run the following:
Note that by using the -o homework
option we’re not using the default remote origin
but a user-defined remote called homework
.
Next, create a new repository on the Github.
Ensure your new repository is private and don’t click the option to “Initialize the repository with a README”.
Run the two commands described on GitHub under the heading “Push an existing repository from the command line”. For my repository these are:
Now your homework repository is all set!
Committing
While working on homework assignments, periodically run the following:
Remember, the git commit
operation takes a snapshot of your code at that point in time but doesn’t write to the server.
The git push
operation pushes your local commits to the remote repository.
You should do this frequently: as often as you have an incremental, standalone improvement.
Getting new homework assignments
When we release a new assignment we will simply add it to the homework github repository.
To get the latest homework assignments and potential updates or corrections to the assignment, run the following.
Make sure to have all your changes committed before you do that.