Quick git notes

For using git to fork/clone/submit assignments, see the git student submission notes.

The git executable is located at /usr/bin/git To try out the basics of version control:

  1. Create the base directory and initial contents for your project

  2. In the directory you just created:
    (i) initialize a git project (git init)
    (ii) add the current directory to it (git add .)
    (iii) save your changes to the git project (git commit) Add appropriate commentary in the log opened by the commit command

  3. If you haven't already, create a makefile for the project and add it to your git repository (git add makefile) and commit your changes (git commit), providing a suitable comment during the commit.

  4. Create a new branch for a commented version of the project (git branch my_new_version) and switch to work on that new branch (git checkout my_new_version)

    You can switch between branches at any point using checkout, but it is strongly recommended you add/commit any changes before switching branches.

  5. Renaming main: "main" is now the preferred choice for naming your core branch (formerly was "main"). After you have done an initial commit in a repository you can change the name from main to main using
    git branch -M main
    Most of the documentation below assumes you have renamed master to main.

  6. Plow along on the cycle of edits and commits .
    If you need to move, rename, or remove files or directories be sure to use the git mv or git rm commands rather than the standard linux mv, rm (to ensure the changes are accurately included in the repository updates).

  7. Run gitk while in the git repository and see how it lets you explore your project history.
Note that if you have a git repository as a subdirectory of another git repository the "parent" one will not track the files in the subrepository unless you force it to with the -A option in add (not generally a recommended approach).

Quick list of git commands


Misc. notes

You can run a git command from a different directory using the --git-dir option, e.g.
git --git-dir someRepo/.git status

Note that if you want to find an old commit and go back to it, first we have to find the right commit #: use git log, which will show your commit numbers (big ugly numbers) and associated messages.

Pick the command number, create a new branch for that commit's code, and check it out:
git checkout -b CallMyNewBranchSomething 123456789
(replacing the 123456789 with your chosen commit's number)

You can create a list of files and file types you specifically want git to ignore when tracking: simply list them in the .gitignore file in the top level of the repository.