Git is one of the most commonly used systems for version control which allows us to manage changes in our code and collaborate with others. This document will serve as a reference. For a more detailed guide, check out the Git documentation here.

Installing Git

Before installing Git, you first want to check if it is already installed on your machine. To do so, open terminal and type git --version. If terminal echoes out the version, then Git is installed.

Otherwise, you can download the latest version of Git here. For more detailed installation instructions, check out this guide.

Essential Workflow

Essential Commands
git clone URL-or-path-to-repo Clone a repository. This makes a new folder in the current directory containing the files in the repository in which you can work on the code.
git add Add the given file to the repository so that it will be tracked by git. Use this when you author a new code file and want to include it in a commit. Alternatively, include -a to add all files from your local repo
git commit Finalize the current changes to your code as a commit to your current branch and repo on your local machine.

Include -a to automatically add changed files that git is already tracking and -m <message> to include a message about the commit (otherwise you will be kicked to an editor in which to type out your message).

git pull Pull any changes that have happened on the remote server you initially cloned and bring them into your local repository. You will need to have a clean repo so you will probably want to commit first if you have changes (or stash them).
(Note: A git pull is a combination of a git fetch and a git merge which will lead to extra commits of the form Merge branch 'master' of .... For a cleaner history, use git pull --rebase)
(Note 2: If you need to merge, feel free to checkout our Git Merge Conflicts documentation)
git push Push whatever commits you have made locally to the remote repository that you cloned previously. You may need to pull first to ensure that you are in sync with the repo.
git log View history of commits that you have made. They will be displayed with their unique identifier (a big hex mess like 766f98f32fa...) and their commit message.

Clone some repo of existing code and enter the directory
$ git clone /some/path/fake-project
... a bunch of git output about copying files down ...
$ cd fake-project
Now make a new file, add some stuff to it and commit
$ gedit my-favorite-file.txt
... do some editing ...
$ git add my-favorite-file.txt
$ git commit -m "Worked on my favorite file"
[master (root-commit) 3c909e0] Worked on my favorite file
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 my-favorite-file.txt
Now we have finalized these changes we can push them to the repo we cloned (if we want to, sometimes we just want to work on this code locally if we aren't collaborating)
$ git push
Counting objects: 25, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (25/25), done.
Writing objects: 100% (25/25), 2.43 KiB, done.
Total 25 (delta 4), reused 0 (delta 0)
To /some/path/fake-project
* [new branch]      master -> master

Other Useful Features

We don't necessarily expect you know these but it wouldn't hurt to be aware of them because you will find them useful.

git diffView the difference between the current code and the latest commit. You can also enter two commit IDs to see the difference between the code at each of those commits.
git reset <ID>Reset the state of your repo to that of a specific commit from the past.
git stashStash away the latest uncommitted changes. Later, you can run git stash apply to restore these changes.
git config --global core.editor $editor Change the default editor to one of your choice. By default git uses vim as an editor, but if you're uncomfortable with it you can use this command to switch to something like emacs
git branch List all of the current branches.
git checkout <branch> Switch to a different branch called <branch> or create a new branch called <branch> if it does not exist yet.
git merge <branch> From your current branch, merge in the changes from <branch>.