git: useful commands

Examples of running git from the command line.

In the lab, we use the version control software git to keep track of our code.  All the code is hosted on a server called GitHub.


To run commands on a repository, first cd (change directory) to the folder on the local machine where that repository lives.  You'll see the prompt change to add the word (master) after the directory name.
To check the status of the repository, e.g. whether there are any additions or changes to files in the directory, use git status:
git status

This will display all the local changes -- that is, all the additions or edits to files that were made on the local machine you are working from. Even if git status says "Your branch is up to date with 'origin/master'", there may be changes upstream (i.e., on the GitHub server) that your local machine doesn't know about. To pull in any changes that may have been made to the repository from other machines, use git pull:
git pull
This will pull in any changes. If no changes were made to the central repository since the last pull, git will say "Already up to date."
It's a good idea to get to this state before you start editing code, since it can help you avoid conflicts with existing edits upstream.

Now, let's say you've made edits to the code. To take a snapshot (a "commit") of these changes, you'll first need to stage each file whose changes you want to include by using git add:
git add <filename>

To add all files with changes at once, use:
git add .
(This is not recommended unless you double-check to make sure you want to add ALL files with changes and ALL new files -- i.e., everything that shows up in red text when you run git status!)

To commit the files you've added, use git commit:
git commit -m "Example commit message"
The commit message should be something descriptive of the changes you are committing. For example, "Add vowel space density plotting function" or "Bug fix for randomization procedure".

You can make several commits in a single session; they will all be stored locally as individual snapshots of the code.  When you're ready to make the changes available to all other users/computers, use git push:
git push
This will incorporate all the local commits into the remote "origin" which lives on

What if I try to do a git push, and I get an error?

This is likely caused by changes upstream that you haven't yet incorporated into your local copy. You need to incorporate these changes so that your own edits can be layered on top of them. First use git pull:
git pull
This will incorporate the remote changes. Then do a push and accept the default commit message ("Merge branch 'master' of").

What if I do a git pull, and I pull in changes I didn't want?

You can undo a git pull with git reset:
git reset --hard HEAD@{1}
This will put your working directory in the state it was in before the pull.

Git Etiquette

Since we are working with a common codebase, we need to follow some simple conventions to keep things running smoothly.

Don't leave uncommitted changes on shared computers

When you finish working on a lab machine, the repositories should be in a clean state.  Make sure you commit (or trash) any changes so the next person to use the machine doesn't run into conflicts when they try to pull in changes.

Repositories are for code (or code-like files) only

A git commit is a snapshot of all the files in the repository. This means that commits including large files continue to take up space on all lab computers (even if the files themselves have been deleted from the repository!). Avoid adding the following types of files to a git repo:

  1. data (.mat files)
  2. images (figures, pictures)
  3. sound files
  4. other files such as Powerpoint slides or Word documents
  5. Matlab Live Scripts (.mlx) -- these are actually binary files. If you want to keep track of a Live Script, save it as a regular .m file and add that to the repo.

Keywords:git, github   Doc ID:90966
Owner:Carrie N.Group:Speech Motor Neuroscience Group
Created:2019-04-10 17:02 CSTUpdated:2020-09-04 09:46 CST
Sites:Speech Motor Neuroscience Group
Feedback:  0   0