6.005 — Software Construction
**This document is a work in progress.** Use Piazza to suggest further Git tips and tricks we might include.
## Basic workflow **If you haven't already, you will want to [configure Git and your commit message editor](./config.html).** The basic building block of data in Git is called a "commit". A commit represents some change to one or more files (or the creation of one or more files). When you first create a file or change a file, that data is unknown. To add it, run: `git add file.txt` (where `file.txt` is the file you want to add) You'll either need to run that command from the same directory as the file, or include directory names in the file path. This "stages" the file. Once you've staged all your changes, run: `git commit` This will pop up an editor that will give you a chance to write a _commit message_. When you save and close the editor, the commit will be created. ## Getting the status of your repository Git has some nice commands for seeing the status of your repository. The most basic of these is `git status`. You can run this at any point to see which files Git sees have been modified and are still unstaged and which files have been modified and staged (so that if you `git commit` those changes will be included in the commit). Note that the same file might have both staged and unstaged changes, if you changed the file more after running `git add`. When you have unstaged changes, you can see what the changes were (relative to the last commit) by running `git diff`. Note that this will _not_ include changes that were staged (but not committed). You can see those if you run `git diff --staged`. You can see what the last commit actually was with `git show`. This will show you the commit message as well as all the modifications (as if you had run `git diff`). You can see the list of all the commits you made (along with their commit messages) with `git log`. If you do `git log -p`, it will show you the full commit history, including the changes each commit made. In other words, this is as if you ran `git show` on each commit in your history. Note that `git show` and `git log` might place your command-line in a state where you can't type more commands. Instead, there will be a little colon (:) symbol at the bottom. This indicates that there is more data than there was room on your screen and that you can scroll with the arrow keys. You can leave this mode by pressing `q`. ### LOL Want a quick review of the history of your Git repository, and where you are now? Create an alias using the command: git config --global alias.lol "log --graph --oneline --decorate --color --all" Now, in any repository use: git lol to see an ASCII-art graph of commits. ## Commit IDs Every Git commit has a unique ID, which is the long string of letters and numbers that you see when you type `git log` or `git show`. This is what's called a "hash" of the contents of your commit. One neat feature is that this ID is unique not just within your repository, but actually within the _universe_ of Git commits. In other words, if your commit ID is something like `ab1312313febc241...`, that commit is (extremely likely) to be the _only_ commit in the world with that name. You can reference a commit by its ID (or frequently just by the first 8 characters). This is most useful with something like `git show`, where you can look at a particular commit, rather than just the most recent one.