Version Control System

Version Control System, or VCS, allows you to save a snapshot of your project anytime you want. It's like making a copy of your project for backup and safekeeping, except that VCS typically does this more efficiently. It also comes with facilities to restore to an earlier copy (version).

Git is the world's most popular VCS. It can keep a complete history of the changes made to code and revert to old versions when needed. This feature comes in handy when you want to make changes to code without losing the original.

Git also facilitates synchronizing code between different people, making collaboration in a team easier. This feature leads to increase productivity, in particular in a large software project that involves many developers.

Every time you save a new version of your project, Git requires you to provide a short description of what was changed. This process helps to understand how the project evolved between versions.

I use Git for all my coding projects, even when it is small, and I am the only one working on it. I do this because the history of changes helps me understand what happened when I visit the project later.

Jargons

  • Repository: A collection of files tracked by Git. For example, the files that make up the content of this website are kept in a Git repository.

  • Remote: Any version of a repository that is not stored locally on a device is called a "remote". (So, GitHub is a service for you to host remote repositories). "Origin" is used to refer to the "remote" from which the local repository was originally downloaded from.

  • Commit: Git does not save any changes made to the files within your repository until you "commit" it. So, as a verb, it is the action of storing a new snapshot of the repository's state in the Git history. When "commit" is used as a noun, it refers to a single point in Git history.

  • Staging: Let's explain this one with an example; assume you made changes to 4 files within your repository, but you only want to commit 2 of them because the other 2 are buggy or not complete yet. How do you commit only 2? Well, you put them in the "staging area" after which you commit. So, staging a file means that you have marked it for a commit.

Useful commands

  • git init: create an empty Git repository in a directory.
  • git add <filename(s)>: add files to the staging area to be included in the next commit
    • git add . : adds all files
  • git commit -m "message": take a snapshot of the repository and save it with a message about the changes
    • git commit -am "message": add files and commit changes all in one
  • git status : print what is currently going on with the repository
  • git log: print a history of all the commits that have been made
    • git log --pretty=oneline: list commit history in a compact format
  • git diff <commit> <commit>: show the changes made between the two commits (identified by their IDs)
  • git checkout <commit>: revert the repository back to a given commit. Use it if you want to discard changes to un-staged file/s.
  • git reset --hard <commit>: reset the repository to a given commit. Use it if you want to undo staging of a modified file.
  • git clone <url>: take a repository stored on a server (like GitHub) and downloads it
    • git clone <url> folder-name: clone to folder-name
    • git clone -b <branch> <url>: clone a specific branch
  • git push: push any local changes (commits) to a remote server
    • push only after you staged and committed the changes
  • git fetch: download all of the latest commits from a remote to a local device
  • git pull: pull any remote changes from a remote server to a local computer
  • git branch: list all the branches currently in a repository
  • git branch <name>: create a new branch called name
  • git checkout <name>: switch current working branch to name
  • git merge <name>: merge branch name into current working branch (normally master)
  • git merge origin/master: merge origin/master, which is the remote version of a repository normally downloaded with git fetch, into the local, preexisting master branch
  • git remote set-url origin https://github.com/USERNAME/REPOSITORY.git: changing a remote's URL

Here is a two-page PDF containing the most useful Git commands.

Git vs. GitHub

Git is a version control system, whereas GitHub is a repository hosting service and a collaboration platform. Moreover, GitHub provides a lot of valuable services for hosting software projects. We will explore some of these features in this course.