Advanced section: This concept will be useful for the rest of your studies and possibly at work. We recommend to keep these notes handy in the future.
Third part
Learning Objectives
- Learn about version control and why it is important.
- Learn the basics of the Git version control system together with the GitHub hosting service.
- Learn the basic Git commands, such as git init, git add, git commit, git push, git pull.
- Intended as an introduction for students who haven't used Git before.
Why Learn Git?
A common problem when dealing with information stored on a computer is handling changes. For example, after adding, modifying or deleting text you may want to undo that action (and perhaps redo it later). At the simplest level this might be done by clicking undo in a text editor (which reverts a previous action).
A naïve method for handling multiple file versions is simply creating duplicate files with differing filenames and contents (Important Document V4 FINAL FINAL.doc may sound sadly familiar). At a more advanced level, you may be sharing a document with other people and, rather than just undoing and redoing changes, wish to know who made a change, why they made it, when they made it, what the change was and perhaps even store multiple versions of the document in parallel. A version control system (such as Git) allows all these operations and more.
Version control systems can therefore solve the problem of reviewing and retrieving previous changes, allow single files to be used rather than duplicated, and allows for collabortation with several people. For these reasons, many organizations and companies use a version control system designed to track changes in the source code and other text files during the development of a software. Version control systems have functionalities such as storing, updating, and recording any changes made.
There are two main models (or types) of version control software: the Client-Server model and the Distributed model.
- Client-Server model: Developers use a shared single repository. Some examples include:
- Open source: CVS, Subversion, Vesta, etc.
- Proprietary: Microsoft Team Founday, Helix, etc.
- Distributed mode: Each developer works directly with his or her own local repository, and changes are shared between repositories as a separate step. Some examples include:
- Open source: Git, SVK, Fossil, etc.
- Proprietary: Visual Studio Team Services, Plastic SCM, etc.
You may have heard of or used some of these version control systems, but in these set of notes we'll be focusing on the Git version control system.
Git is a particularly popular version control system to use and learn because it is open source, is easy to learn and has a tiny footprint with lightning fast performance. It outclasses many other tools with features like cheap local branching, convenient staging areas, and multiple workflows.
Using a version control system such as Git is also worthwhile when working on a group project, where you have the option to show case your projects publicly, such as providing a link to your LinkedIn profile. This makes it very easy and practical for organizations and companies that are looking to hire to get to know how you approach problems and how you code, based off your coding history. In adition, knowledge of how to use a version control system is one of the top things organizations and companies look for when hiring.
Introduction to Git
Git is a version control system so that you can record all your changes to your code and collaborate with others. A Git Repository is basically a folder that is managed by Git, where all changes to its contents can be recorded. After installing Git, you can turn any folder on your computer into a Git Repository by typing the following command:
git init
The best way to think of Git is that you record snapshots of your code.
- You make change to your files.
- You mark the files that you want to be part of the next snapshot.
- You create the snapshot with a message. We call these snapshots “commits”.
Git Local
Let’s say we have a folder of files on our local computer in "my_folder".
- index.html
- about.html
- home.html
- contact.html
- style.css
- main.js
- README.txt
And let’s say we make some changes to files in "my_folder"...
- index.html
- about.html
- home.html
- contact.html
- style.css
- main.js
- README.txt
Then the typical workflow for commiting these changes as shown in Figure 1 would be:
- Make changes
- git status
- git add <files>
- git commit -m “Message”
Note: "git status" command displays the state of the working directory and the staging area. Before adding and then commiting changes, it's a good idea to first check which changes have been staged, and which haven't.
Hint: "git add ." is a very common way to just add all modified files to the
staging area.
Figure 1 : Typical Workflow.
Git Remote
After a while, you’ll end up with a bunch of snapshots or commits on your Local Git Repository. How do you share this with your teammates? With Git Remote, you agree upon a central place to synchronize your repository with. Then your teammates can synchronize their repositories with that central place. This central place is called a “Git Remote Repository”.
Remote Repositories are just Git Repositories that you synchronize with. After you synchronize, your local repository and the remote repository will contain the exact same information (all the snapshots, commits, etc.). You can set up your Git Remote repository anywhere, but we’re going to use a service called GitHub to host our git remote repository, as shown in Figure 2.
Figure 2 : GitHub Remote Repository.
To start synchronizing with a Remote repository, you
- Create a Remote Repository on Github.
- Add its address to your computer (local).
git add remote <name> <git address>
For example:
git add remote origin git@github.com:myusername/myRepoName.git
git push
How do you synchronize your local repository with the remote one? First you
can push up your changes to the remote respository.
push <name of remote> <name of branch>
For example:
git push origin master
git pull
You can grab the latest changes from your remote repository using
git pull
Your teammates can also get the latest snapshots using the command. It is recommended to git pull first to obtain the latest snapshot, before using git push.
Recommended way to proceed for personal practice
- Install Git (https://git-scm.com/downloads).
- Try out Git locally on your computer.
- Sign up for GitHub Account (https://github.com/)
(Use your student email address to get the advanced version for free!). WARNING: GitHub uses private and public repositories. If you use GitHub when working on a class assignment, make sure that the repository is PRIVATE to avoid nasty surprises!
- Setup a personal remote repository on GitHub.
- Practice pushing from your local repository (on your computer) to the remote repository (on GitHub).
References
Introduction to Git was taken from Casey Li's slides. For more information on GIT we highly recommend the following videos "Gitting to Know You".