Git Guidelines: the Basics

A cheatsheet designed to help you navigate Git and be sure that you never lose your hard work.

Meagan Rossi
3 min readSep 9, 2020
Continuous contributions are key for success
Continuous contributions help to keep you fresh.

Part 1 in a series outlining the basic tenets of using Github through your terminal. This blog post assumes that the software environments for Github have been installed. It also assumes that the user has active repositories in their Github account along with associated text editors/notebooks for coding.

The Basics

Using Github for data science projects is a fundamental method to safely store and share your work. It can be used for both solo and group projects and is shared publicly unless the user has a paid account and changes the settings for a repository to private. Here is a terminal git guideline for users who may be new to the platform or are simply looking for a refresher. In this document angle brackets will be used to encapsulate custom content created or defined by the user and should not be included in the terminal command.

Git Status

Check to see if text modifications have been made in your environment. If your branch is up to date and there is nothing to commit (working tree clean), no further action is needed.

# View active branch of the repository along with update status
$ git status

Git Add

If changes have been made, it is time for an add. By adding to git, the user is staging modifications for Github. Local changes on your computer should be saved regularly and automatically if possible.

# Add changes
$ git add .
# Add specific files
$ git add <filename>
# Update all
$ git add -A
# Users can add a .gitignore through the terminal if they are using textmate. This can save time rather than entering in the .gitignore in the README file. If you prefer to add a .gitinore in the README, type .gitignore *.<filename extension> at the end of the document and commit. Some files, such as csv, tend to be large and therefore take up a lot of memory space. Free accounts with Github have limited storage capabilities and therefore need to be ignored.
$ mate .gitignore *.<filename extension>

Git Commit

A commit is simply the method used to save your work, both within Github and remotely on your local computer. The standard git commit is a two step process where the user first adds the commit and then defines the content of the saved changes so that they can reference the modifications at a later date. Please see steps to enter into the terminal below. Be sure to include the quotes though!

# Step 1: Save the add through a commit
$ git commit -a
# Step 2: Assign a message to the commit. Be sure to include a description here that will remind you of the changes that you are saving. This could be useful in the future if you would like to revert to a previous version.
$ git commit -m ‘<message>’
# A simplified express commit saves time by combining ‘add’ and ‘message’ into one line of text for the terminal. Be sure to push changes once complete.
$ git commit -am ‘<message>’

Undo Changes, Remove & Rename

Take a deep breath! It is possible to undo changes made to the file/folder with surgical precision if you are committing often enough. You can also completely remove files through the terminal if that is preferred.

# View commit history
$ git log
# View the full commit log as single lines
$ git log — oneline — graph — decorate — all
# View unstaged changes
$ git diff
# Undo changes made in <commit>
$ git revert <commit>
# Remove file
$ git rm <filename>
# View list of files to be removed from working directory
$ git clean -n
# Execute the file clean
$ git clean -f
# Rename a file or a folder name
$ git mv <old filename> <new filename>
# Revert to previous file version
$ git checkout — <filename>

Next week will be all about branches. Please email me with any questions or follow-ups, meaganrossi@gmail.com.

--

--