Git Primer

Git cheatsheet #

A collection of some essential git commands.

Command Description Example
git init Initialize a git repository -
git add -A Add everything in the directory for tracking -
git commit -am "<message>" Stage and commit with a message git commit -am "some useful message"
git checkout -b <branch name> Check out a new branch git checkout -b new-work
git checkout <branch> Switch to an existing branch git checkout new-work
git branch -d <branch> Delete an existing branch git branch -d new-work
git branch Get a listing of local branches -
git branch -r Get a listing of local branches, possibly coupled with remote update -
git remote update Update local listing of remote branches -
git switch <remote branch> Switch to remote branch -
git status Check for uncommitted changes -
git push Push local changes to the remote repository -

Workflow #

My workflow with a brand new project that I want to commit usually looks something like this:

  1. Create new working directory
  2. Initialize a repository
  3. Define the set of things I don’t want tracked by git
  4. Commit the current structure of the repository
  5. Push the repository to Github

After I create the repository, my workflow with an existing project looks something like this:

  1. Create a branch to do some work
  2. Do some work
  3. Commit the work
  4. Push the work to a remote branch
  5. Make a pull request if it’s collaborative work. Otherwise, if it’s individual work, unilaterally merge the work to the main branch

Initialize a repository #

Set up a folder or directory to be tracked by git by navigating to the directory first, then running the following:

git init

This will create a .git directory with the directory or folder you want to be tracked by git.

This .git directory contains all the content to handle the git interactions.

Ignore #

Oftentimes, there will be specific files in a git-tracked directory that you don’t want tracked. This is where a .gitignore file comese into play.

You can selectively specify which files or file types or directories should not be tracked by git.

First, create the file at the top level of your directory (i.e., where the .git directory is located). I usually do this at the command line.

> touch .gitignore

This creates a completely empty file.

Then using the text editor of your choosing, go in and modify that file to include the things that you don’t want git to track. Each thing goes on a separate line.

For instance, if I don’t want files with .swp filetypes to be tracked, I would add the following to my .gitignore.

.swp

If I don’t want the contents of a data/ directory to be tracked, I would add the following:

data/

Excluding the data and any outputs is actually a very good practice. Git should primarily be used for tracking code, not data. This is especially true if the data is at all sensitive.

Data should be stored in another medium that is purpose-built to retain data.

That being said, it’s merely a guideline in many cases.

Commit #

Changes to directories that you want to track somehow are referred to as commits.

If I make changes in a directory that I want to commit, I usually add everything.

git add -A

Then I’ll commit the added changes with the following, along with a message:

git commit -am "some useful message"

The some useful message should actually be a useful, descriptive message about what was committed. This saves reviewers (that could potentially be you a few months down the line when you have to refresh some old work, so be thoughtful) time.

Instead of having to review the code changes to understand what changed, the message itself should convey a pretty solid idea of what the changes were.

Rename remote repository #

If the remote repository name changes, it’s necessary to change the local pointer:

git remote set-url origin git@github.com:<Github user or org>/<repo name>.git

To verify the remote path has changed:

git remote -v

List remote branches #

git remote update # to update the local listing of what's on the remote
git branch -r

Resources #

  • Pro Git – I consider this the book on git. The volume of material might be overwhelming, but the content is excellent.

  • Learn Enough Git to Be Dangerous – A pretty awesome crash course on git.