Basic git

Git is a very powerful, very useful, and complex tool. I strongly encourage you to use it to track all assignments you ever do, if only to become more familiar with it.

Commands we discussed

Creating repositories

  • git init - New repository
  • git clone $SOURCE- clone an existing repository, can be local, https, ssh, etc

Creating and managing commits

  • git add $FILE(s)- adds a new file to staged changes, or changes to a tracked file to staged changes
  • git commit - batches up the stanged changes into a commit, we discussed the -a and -m options
  • git push - send your new commits back to the remote repository you cloned from (by default)
  • git pull - get changes from the remote you cloned from (by default), can use –rebase

    Checking history and changes

  • git log - read the log of all commits
  • git blame $FILE - see what commit last changed what line in the file
  • git diff $REVISION [$REVISION2] - see what changed between two revisions
  • git checkout $REVISION - switch your working files to some previous version
  • git checkout $BRANCH - switch to the HEAD of some branch
  • git checkout $FILE - revert a file to the state it is in your current branch’s HEAD
  • git branch - see existing branches or create one

Other git notes


Each revision is specified by a unique hash, or by a shortened prefix of that hash. You can see these in git log. The current most recent version in the branch is called HEAD.

You can refer to the ‘parent’ of a revision (the previous one) with $REVISION~1 or just $REVISION~ for short. $REVISION^1 works similarly, but has some differences that don’t matter for this course yet.


Git will look for a special file in the base of your repository called .gitignore to determine what files to not track changes for by default. This is useful to avoid git telling you about all the temporary or intermediate files generated by the project you are working on. (Think of the temporary files vim generates that end in .swp).

This file takes a format specified here. As an example, to ignore all temporary files vim tends to generate, you would have: